C++ Implement a template class Queue as defined by the following skeleton via th
ID: 3706425 • Letter: C
Question
C++
Implement a template class Queue as defined by the following skeleton via the reserved element method in a dynamic array:
template<class ItemType>
class Queue
{
private:
NodeType<ItemType>* info; // It points to the beginning memory address of
// a dynamic array
int front, rear, maxQue; // two position indices
public:
Queue( ); // default constructor: Queue is created and the length of the dynamic
// array is determined by user input from a keyboard;
Queue(const Queue<ItemType> &x); // copy constructor: implicitly called
// for a deep copy
void MakeEmpty(); // Queue is made empty without deallocating the dynamic
// array
bool IsEmpty( ); // test if the queue is empty
bool IsFull( ); // test if the queue is full;
int length( ); // return the number of elements in the queue
void Print( ); // print the value of all elements in the queue in the sequence
// from the front to rear
void Enqueue(ItemType x); // insert x to the rear of the queue
// Precondition: the queue is not full
void Dequeue(ItemType &x); // delete the element from the front of the queue
// Precondition: the queue is not empty
~Queue(); // Destructor: memory for the dynamic array needs to be deallocated
};
In you main( ) routine, you need to test your class in the following cases:
Queue<int>IntQueue;
int x;
IntQueue.MakeEmpty();
IntQueue.Dequeue(x);
IntQueue.Enqueue(10);
IntQueue.Enqueue(20);
IntQueue.Enqueue(30);
IntQueue.Enqueue(40);
cout << "int length 3 = " << IntQueue.length() << endl;
IntQueue.Dequeue(x);
cout << "int length 4 = " << IntQueue.length() << endl;
cout << “The int queue contains: “ << endl;
IntQueue.Print();
if(IntQueue.IsFull() == false)
cout << “The int queue is not full !” << endl;
else
cout << “The int queue is full !” << endl;
Queue<float>FloatQueue;
float y;
FloatQueue.MakeEmpty();
FloatQueue.Dequeue(y);
FloatQueue.Enqueue(7.1);
cout << "float length 3 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(2.3);
cout << "float length 4 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(3.1);
FloatQueue.Dequeue(y);
cout << “The float queue contains: “ << endl;
FloatQueue.Print();
Queue<float> FloatQueue2 = FloatQueue;
cout << “The float queue 2 contains: “ << endl;
FloatQueue2.Print();
FloatQueue.MakeEmpty();
cout << “The float queue 3 contains: “ << endl;
FloatQueue2.Print();
Explanation / Answer
#include <iostream>
using namespace std;
template<class ItemType>
class Queue
{
private:
ItemType* info; // It points to the beginning memory address of
// a dynamic array
int front, rear, maxQue; // two position indices
public:
Queue(){
cout<<"Enter queue size:";
cin>>maxQue;
maxQue--;
info = new ItemType[maxQue+1];
front = 0;
rear = -1;
} // default constructor: Queue is created and the length of the dynamic
// array is determined by user input from a keyboard;
Queue(const Queue<ItemType> &x){
maxQue = x.maxQue;
info = new ItemType[maxQue+1];
int i;
for(i=x.front; i<=x.rear; i++){
info[i-x.front] = x.info[i];
}
front = 0;
rear = i-1-x.front;
} // copy constructor: implicitly called
// for a deep copy
void MakeEmpty(){
front = 0;
rear = -1;
} // Queue is made empty without deallocating the dynamic
// array
bool IsEmpty(){
return front<=rear;
} // test if the queue is empty
bool IsFull(){
return rear==maxQue && front==0;
} // test if the queue is full;
int length(){
return rear - front + 1;
} // return the number of elements in the queue
void Print(); // print the value of all elements in the queue in the sequence
// from the front to rear
void Enqueue(ItemType x); // insert x to the rear of the queue
// Precondition: the queue is not full
void Dequeue(ItemType &x); // delete the element from the front of the queue
// Precondition: the queue is not empty
~Queue(){
delete[] info;
} // Destructor: memory for the dynamic array needs to be deallocated
};
template <class T>
void Queue<T>::Print() {
int i;
for(i=front; i<=rear; i++){
cout<<info[i]<<' ';
}
}
template <class T>
void Queue<T>::Enqueue(T x) {
if(rear==maxQue){
if(IsFull()){
cout<<"Queue full ";
return;
}
int i;//space left so readjusting
for(i=front; i<=rear; i++){
info[i-front] = info[i];
}
rear = i-1-front;
front = 0;
}
info[++rear] = x;
}
template <class T>
void Queue<T>::Dequeue(T &x){
if(IsEmpty()){
cout<<"Queue Empty ";
return;
}
if (info[front]!=x) {
cout<<"Element not in front ";
return;
}
cout<<"Element deleted:"<<x<<' ';
front++;
}
int main(){
Queue<int>IntQueue;
int x;
IntQueue.MakeEmpty();
IntQueue.Dequeue(x);
IntQueue.Enqueue(10);
IntQueue.Enqueue(20);
IntQueue.Enqueue(30);
IntQueue.Enqueue(40);
cout << "int length 3 = " << IntQueue.length() << endl;
IntQueue.Dequeue(x);
cout << "int length 4 = " << IntQueue.length() << endl;
cout << "The int queue contains: " << endl;
IntQueue.Print();
if(IntQueue.IsFull() == false)
cout << "The int queue is not full !" << endl;
else
cout << "The int queue is full !" << endl;
Queue<float>FloatQueue;
float y;
FloatQueue.MakeEmpty();
FloatQueue.Dequeue(y);
FloatQueue.Enqueue(7.1);
cout << "float length 3 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(2.3);
cout << "float length 4 = " << FloatQueue.length() << endl;
FloatQueue.Enqueue(3.1);
FloatQueue.Dequeue(y);
cout << "The float queue contains: " << endl;
FloatQueue.Print();
Queue<float> FloatQueue2 = FloatQueue;
cout << "The float queue 2 contains: " << endl;
FloatQueue2.Print();
FloatQueue.MakeEmpty();
cout << "The float queue 3 contains: " << endl;
FloatQueue2.Print();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.