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()...
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...
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...
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...
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...
implement the Queue ADT using the linked list approach #include "QueueLinked.h" template QueueLinked::QueueNode::QueueNode(const DataType& nodeData, QueueNode*...
implement the Queue ADT using the linked list approach #include "QueueLinked.h" template QueueLinked::QueueNode::QueueNode(const DataType& nodeData, QueueNode* nextPtr) { } template QueueLinked::QueueLinked(int maxNumber = Queue::MAX_QUEUE_SIZE) { } template QueueLinked::QueueLinked(const QueueLinked& other) { } template QueueLinked& QueueLinked::operator=(const QueueLinked& other) { } template QueueLinked::~QueueLinked() { } template void QueueLinked::enqueue(const DataType& newDataItem) throw (logic_error) { } template DataType QueueLinked::dequeue() throw (logic_error) {    DataType temp;    return temp; } template void QueueLinked::clear() { } template bool QueueLinked::isEmpty() const {    return false; } template...
Your primary task for this exercise is to complete header file by writing three functions with...
Your primary task for this exercise is to complete header file by writing three functions with its description below: removeAt function – to remove the item from the list at the position specified by location. Because the list elements are in no particular order (unsorted list), you could simple remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list. insertAt function - to insert an item...
In a header file Record.h, create a Record structure that contains the following information: recordID, firstName,...
In a header file Record.h, create a Record structure that contains the following information: recordID, firstName, lastName, startYear. recordID is an integer. In testRecord, first create a record (record1) using “normal” pointers. Set the values to 1001, Fred, Flintstone, 1995 In testRecord, create a new record (record2) using a unique smart pointer. Set the values to 1002, Barney, Rubble, 2000 Create a function (or functions) that will print the records to any output stream. Since the print function does not...
''' File: pyPatientLL.py Author: JD ''' class Node: #ADT        def __init__(self, p = None):             ...
''' File: pyPatientLL.py Author: JD ''' class Node: #ADT        def __init__(self, p = None):              self.name = ""              self.ss = self.age = int(0)              self.smoker = self.HBP = self.HFD = self.points = int(0)              self.link = None              #if list not empty              if p != None:                     p.link = self        ptrFront = ptrEnd = None choice = int(0) def menu():        print( "\n\tLL Health Clinic\n\n")        print( "1. New patient\n")        print( "2. View patient by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT