Question

In: Computer Science

please complete the header file that contains a class template for ADT Queue and complete all...

please complete the header file that contains a class template for ADT Queue and complete all the member functions in the class template. Submit the header file only, but please write a source file that tests all the member functions to make sure they are working correctly.

queue.h


#ifndef _QUEUE
#define _QUEUE

#include"Node.h"

template<class ItemType>
class Queue
{
private:
   Node<ItemType> *backPtr;
   Node<ItemType> *frontPtr;

public:
   Queue(); //Default constructor
   Queue(const Queue<ItemType> &aQueue);

   bool isEmpty() const;
   bool enqueue(const ItemType &newEntry);
   bool dequeue();
   ItemType peekFront() const;
   void display();
};

template<class ItemType>
Queue<ItemType>::Queue()
{
  
}
template<class ItemType>
Queue<ItemType>::Queue(const Queue<ItemType>& aQueue)
{
   Node<ItemType> *originalNodePtr=aQueue.frontPtr;
   if(originalNodePtr==NULL){
       frontPtr=NULL;
       backPtr=NULL;
   }
   else{
       frontPtr=new Node<ItemType>;
       frontPtr->setItem(originalNodePtr->getItem());
       backPtr=frontPtr;
       while(originalNodePtr->getNext()!=NULL){
           originalNodePtr=originalNodePtr->getNext();
           ItemType nextItem=originalNodePtr->getItem();
           Node<ItemType> *newNode=new Node<ItemType>(nextItem);
           backPtr->setNext(newNode);
           backPtr=backPtr->getNext();
       }
       backPtr->setNext(NULL);
   }
}
template<class ItemType>
bool Queue<ItemType>::isEmpty() const
{
  
}
template<class ItemType>
bool Queue<ItemType>::enqueue(const ItemType &newEntry)
{
  
}
template<class ItemType>
bool Queue<ItemType>::dequeue()
{
  
}
template<class ItemType>
ItemType Queue<ItemType>::peekFront() const
{
  
}
template<class ItemType>
void Queue<ItemType>::display() const
{

}
#endif

Solutions

Expert Solution

The queue can be implemented as follows in C++ using templates:

#include <iostream>
#include <cstdlib>
using namespace std;

// define default capacity of the queue
#define SIZE 10

// Class for queue
template <class X>
class queue
{
   X *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:
   queue(int size = SIZE);       // constructor

   void dequeue();
   void enqueue(X x);
   X peek();
   int size();
   bool isEmpty();
   bool isFull();
};

// Constructor to initialize queue
template <class X>
queue<X>::queue(int size)
{
   arr = new X[size];
   capacity = size;
   front = 0;
   rear = -1;
   count = 0;
}

// Utility function to remove front element from the queue
template <class X>
void queue<X>::dequeue()
{
   // check for queue underflow
   if (isEmpty())
   {
       cout << "UnderFlow\nProgram Terminated\n";
       exit(EXIT_FAILURE);
   }

   cout << "Removing " << arr[front] << '\n';

   front = (front + 1) % capacity;
   count--;
}

// Utility function to add an item to the queue
template <class X>
void queue<X>::enqueue(X item)
{
   // check for queue overflow
   if (isFull())
   {
       cout << "OverFlow\nProgram Terminated\n";
       exit(EXIT_FAILURE);
   }

   cout << "Inserting " << item << '\n';

   rear = (rear + 1) % capacity;
   arr[rear] = item;
   count++;
}

// Utility function to return front element in the queue
template <class X>
X queue<X>::peek()
{
   if (isEmpty())
   {
       cout << "UnderFlow\nProgram Terminated\n";
       exit(EXIT_FAILURE);
   }
   return arr[front];
}

// Utility function to return the size of the queue
template <class X>
int queue<X>::size()
{
   return count;
}

// Utility function to check if the queue is empty or not
template <class X>
bool queue<X>::isEmpty()
{
   return (size() == 0);
}

// Utility function to check if the queue is full or not
template <class X>
bool queue<X>::isFull()
{
   return (size() == capacity);
}

int main()
{
   // create a queue of capacity 4
   queue<string> q(4);

   q.enqueue("a");
   q.enqueue("b");
   q.enqueue("c");
  
   cout << "Front element is: " << q.peek() << endl;
   q.dequeue();
  
   q.enqueue("d");

   cout << "Queue size is " << q.size() << endl;

   q.dequeue();
   q.dequeue();
   q.dequeue();
  
   if (q.isEmpty())
       cout << "Queue Is Empty\n";
   else
       cout << "Queue Is Not Empty\n";

   return 0;
}


Related Solutions

A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ polygon.h file- #ifndef POLY_RVC_H #define POLY_RVC_H #include <iostream> using namespace std; class Polygon { public:    Polygon();    Polygon(int n, double l);    //accessors - all accessors should be declared "const"    // usually, accessors are also inline functions    int getSides() const { return sides; }    double getLength()...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ Write a polygon.h file with given instructions for the polygon.cpp file #include <iostream> #include <math.h> using namespace std; #ifndef M_PI # define M_PI 3.14159265358979323846 #endif int main() {    float areaP, length, sides;    cout << "\n\n Polygon area program.\n";    cout << "---------------------------------\n";    cout << " Enter the...
Create a dynamic array-based Queue ADT class in C++ that contains enqueue(Inserts newElement at the back...
Create a dynamic array-based Queue ADT class in C++ that contains enqueue(Inserts newElement at the back ) and dequeue(Removes the frontmost element ). If the size of the array is equal to the capacity a new array of twice the capacity must be made. The interface is shown: class Queue { private: int* elements; unsigned elementCount; // number of elements in the queue unsigned capacity; // number of cells in the array unsigned frontindex; // index the topmost element unsigned...
Complete the task below C++ This header file declares the Comp315Array Class **************************/ //Include the libraries...
Complete the task below C++ This header file declares the Comp315Array Class **************************/ //Include the libraries #include <iostream> //Allow the use of cin and cout //Declare namespace std for using cin and cout using namespace std; //Class declaration class Comp315Array{ //Public members public: /**************** Default Constructor Task: assign 0 to the array size *********/ Comp315Array(); /**************** Constructor Task: Define an int array of size n *********/ Comp315Array(int n); /***************** getSize method Task: return the array size ******************/ int getSize(); /*****************...
Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
please answer my question... Write a template class that implements an extended queue (use Linked List)....
please answer my question... Write a template class that implements an extended queue (use Linked List). Ex: ExtendedQueue<int> int_queue; ExtendedQueue<double> double_queue; ExtendedQueue<char> char_queue; –Write a program to test this template class in c++. the details of the ExtendedQueue: insert delete front rear queue size. the output will be: Inserting 2 Inserting 4 Inserting 6 Front element is: 2 Removing 2 Inserting 8 Queue size is 3 Removing 4 Removing 6 Removing 8 Queue Is Empty
Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called...
Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called Stack. Use a static array to hold stack elements. Instantiate the Stack class in the main function and provide a user loop and a menu so that all the Stack class member-functions, push, pop, etc., are available so that the user can thoroughly exercise the member-functions of the Stack class. Also, implement a ReversePrint() for the stack. My StackProject, whose exposition I have given...
i need the whole html code Make a layout template that contains a header and two...
i need the whole html code Make a layout template that contains a header and two paragraphs. Use float to line up the two paragraphs as columns side by side. Give the header and two paragraphs a border and/or a background color so you can see where they are.
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT