Queue Class program How to make this C++ program and functions ? Make a menu: 1:
ID: 3677220 • Letter: Q
Question
Queue Class program
How to make this C++ program and functions?
Make a menu:
1: add a new element (testing add(), isFull())
2: remove an element (testing remove(), isEmpty())
3. check the front element (testing getFront(), isEmpty())
4. go back to the rear (testing goToBack())
5. get the number of elements in the queue (testing getSize())
6. display all the elements in the queue (testing displayAll())
7. quit program
**When trying to add new element and queue is full program should not terminate Instead show a message “Cannot add because the queue is full”
**try to remove an element when the queue is empty
**The program should NOT terminate if the queue is empty. Instead show a message “Cannot remove because the queue is empty” try to check the front element when the queue is empty.
**The program should NOT terminate if the queue is empty. Instead show a message “The queue is empty”
Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<process.h>
using namespace std;
template<class T>
class Queue
{
private:
int front,rear;
T *queue;
int maxsize;
public:
Queue(int maxqueuesize)
{
front=0;
rear=-1;
maxsize=maxqueuesize;
queue=new T[maxsize];
}
~Queue()
{
delete[] queue;
}
int isempty();
int isfull();
void insert();
void deletion();
void atfront();
void atrear();
void display();
};
template<class T>
int Queue<T>::isempty()
{
if(front==0&&rear==-1||front==rear)
return 1;
else
return 0;
}
template<class T>
int Queue<T>::isfull()
{
if(rear==maxsize-1)
return 1;
else
return 0;
}
template<class T>
void Queue<T>::atfront()
{
if(isempty())
cout<<" Sorry the queue is empty!";
else
cout<<" Front element of the queue is : "<<queue[front];
}
template<class T>
void Queue<T>::atrear()
{
if(isempty())
cout<<" Sorry the queue is empty!";
else
cout<<" Rear element of the queue is : "<<queue[rear];
}
template<class T>
void Queue<T>::insert()
{
T ele;
if(isfull())
cout<<" Sorry the queue is full!";
else
{
cout<<" Enter the element to insert : ";
cin>>ele;
queue[++rear]=ele;
}
}
template<class T>
void Queue<T>::deletion()
{
if(isempty())
cout<<" Sorry the queue is empty!";
else
cout<<" Deleted element of the queue is : "<<queue[front++];
}
template<class T>
void Queue<T>::display()
{
if(isempty())
cout<<" Sorry the queue is empty!";
else
{
cout<<" Queue elements are : ";
for(int i=front;i<=rear;i++)
{
cout<<" "<<queue[i];
}
}
}
int main()
{
int ch;
Queue<int> q(10);
do
{
cout<<" 1.Insertion 2.Deletion 3.Display Front Element 4.Display Rear Element 5.Display Queue 6.Exit ";
cout<<"Enter your Choice:";
cin>>ch;
switch(ch)
{
case 1:
q.insert();
break;
case 2:
q.deletion();
break;
case 3:
q.atfront();
break;
case 4:
q.atrear();
break;
case 5:
q.display();
break;
case 6:
exit(0);
break;
default: cout<<" Wrong Choice Entered!";
}
}while(ch<=6);
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.