Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

In this assignment, we want to implement a class for Queue data structure. You should be familiar with queues from your first assignment where you implemented a simple queue class. Here, we focus on a...

1 answer below »
In this assignment, we want to implement a class for Queue data structure. You should be familiar with queues from your first assignment where you implemented a simple queue class. Here, we focus on a more complex and capable implementation of queues. This assignment covers the topics of dynamic memory allocation, template classes, exception handling, and operator handling. 1- Queue A queue is an abstract data type which keeps a collection of entities. Queue is a First-In-First-Out (FIFO) data structure and the first element added to the queue would be the first to be removed. You can read more about queues in the following public link
: https:
computersciencewiki.org/index.php/Queue
2- Implementation
You need to implement a template class for the queue data type. In the following you can see a set of requirements and explanations for your implementation:
• You should implement the queue using dynamic a
ay.
• You are NOT allowed to use std::vector or any other built-in a
ay-like classes to implement the queue. • Your queue must be capable of holding different datatypes like int, double, char, or even other classes (although all the elements will have the same data type).
• The queue must always know itssize, named Qsize, and you must have a member function that returns this size, call this function getSize().
• You also need a default constructor without any input parameters which must create a queue of size zero.
• You also need to implement a copy constructor which gets a const Queue& yourQueue as its input parameter and creates a new queue object which is a copy of the input one. • You must allocate memory dynamically.
• Your queue class must be able to enqueue and dequeue objects to the queue. So, you should implement the related methods. These methods should obviously resize your queue. You are responsible for implementing this resizing. So, for each enqueue you need to allocate a new a
ay of size (Qsize+1) and copy everything from the old a
ay to the new one. For dequeue, you follow a set of similar steps with size (Qsize-1). Take care of dangling pointers and memory leak.
• You need to handle possible exceptions, especially when working with dynamic memory. For example, you obviously need such exception handling when trying to enqueue a new object to the queue. • You need to write the appropriate destructor for your queue class.
• You also need to implement a function called isEmpty() which returns true if the queue is empty and false if the queue is not empty.
• You should ove
ide the assignment operator for the objects of your class to make it possible to assign one object of your queue class to another like this: Queue myqueue1; Queue myqueue2; myqueue1 = myqueue2;
• You should also overload the
operator for printing an object of you class. This should print the value of all elements in the queue. Queue myqueue1; std::cout
myqueue1;
• You need to overload the subscript operator ([]) for objects of your class which makes it possible to access an element in the queue if it really exists. This is just for accessing (getting) an element but not for assigning a new element to the queue which is only possible with the enqueue function. Example: Queue myqueue1; std::cout
myqueue1[3];
• This is not a linked list assignment. You are not allowed to implement this queue as a linked list. • Your program must not have memory leak and dangling pointers.
• We will create objects of you queue class for different datatypes and test the required functionalities.
Answered Same Day Jul 04, 2021

Solution

J Anitha answered on Jul 04 2021
146 Votes
#include #include using namespace std;
define default capacity of the queue
#define SIZE 20
Class for queue
template class queue
{
    T *a
;         
a
ay to store queue elements
    int capacity;
maximum capacity of the queue
    int front;     
front points to front element in the queue (if any)
    int rear;     
rear points to last element in the queue
    int count;     
cu
ent size of the queue
public:
    
constructor to create a queue of size 0
queue() {
    capacity = 0;
    front = 0;
    rear = 0;
    count = 0;
    };


Constructo
    queue(int size = SIZE);        
    
    
Destructo
    ~queue()
    {
        
    }
    void dequeue();
    void enqueue(T x);
    T peek();
    int getSize();
    bool isEmpty();
    bool isFull();
    
Assignment Operato
queue operator=(queue& q)
{
    a
= q.a
;
    capacity = q.capacity;
    front = q.front;
    rear = q.rear;
    count = q.count;
}
Subscript operator overloading
T operator[](int i)
{
    return a
[i];
}

operator overloading
ostream &operato
(queue& x)
{
    int i= x.getSize();
    cout
endl
"Elements in the queue";
    
    while(i >= 0)
    {
cout
x.a
[i]
endl;
i = i - 1;
    }
}
Copy Constructo
    queue(queue& q)
    {
        a
=...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here