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 »
can I get help with this c++ code pls?


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 array. • You are NOT allowed to use std::vector or any other built-in array-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 array of size (Qsize+1) and copy everything from the old array 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 override 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. 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="">
Answered Same DayJul 04, 2021

Answer To: In this assignment, we want to implement a class for Queue data structure. You should be familiar...

J Anitha answered on Jul 04 2021
155 Votes
#include
#include
using namespace std;
// define default capacity of the queue
#define SIZE 20
// Cla
ss for queue
template
class queue
{
    T *arr;         // array 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;     // current size of the queue
public:
    // constructor to create a queue of size 0
queue() {
    capacity = 0;
    front = 0;
    rear = 0;
    count = 0;
    };

//Constructor
    queue(int size = SIZE);        
    
    //Destructor
    ~queue()
    {
        
    }
    void dequeue();
    void enqueue(T x);
    T peek();
    int getSize();
    bool isEmpty();
    bool isFull();
    
// Assignment Operator
queue operator=(queue& q)
{
    arr = q.arr;
    capacity = q.capacity;
    front = q.front;
    rear = q.rear;
    count = q.count;
}
//Subscript operator overloading
T operator[](int i)
{
    return arr[i];
}
// << operator overloading
ostream &operator<<(queue& x)
{
    int i= x.getSize();
    cout << endl << "Elements in the queue";
    
    while(i >= 0)
    {
cout << x.arr[i] << endl;
i = i - 1;
    }
}
//Copy Constructor
    queue(queue& q)
    {
        arr =...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here