Given the following class representing a circular queue, write the methods Queue
ID: 3838273 • Letter: G
Question
Given the following class representing a circular queue, write the methods Queue for adding an item into the queue and DeQue for removing an item from the queue. These methods will return a Boolean for success or failure. Also write the default constructor to initialize any needed value into the data members. (25 pts)
The type used to replace T in the template contains a valid assignment operator.
template <class T, int Size>
class Que
{ public:
Que ();
bool Queue (const T &);
bool DeQue (T &);
private:
T Q [Size];
int Head;
int Tail; };
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template <class T, int Size>
class Que
{
private:
T Q [Size];
int Head;
int Tail;
public:
Que (){
Head = Tail = Size-1;
}
bool Queue (const T &a){
if((Tail+1) % Size != Head)
{
Tail=(Tail+1)%Size;
Q[Tail]= a;
rerurn true;
}
else
{
cout<<"Queue is Full. Unable to Add more"<<endl;
return false;
}
}
bool DeQue (){
if (Head == Tail)
{
cout << "Queue is empty" << endl;
return false;
}
else
{
Head = (Head + 1) % Size;
return true;
}
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.