In: Computer Science
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to teach each of the operations in the ADT. Any errors discovered during the processing should be printed as a part of the test result. Please Use C++ language.
// ---- Queue.cpp ------
/*
* Testing all the operations that can be done by Queue ADT in
cpp
*
* queue is in built implemented in cpp
* to use queue use #include<queue> to import it.
* operations are:
* enqueue() --> push() in queue adt.
* dequeue() --> pop() in queue adt.
* front() -> returns reference of the
front of the queue.
* back() -> returns the reference of
end of the queue
* size() -> returns the size of the queue
* empty() -> returns whethere queue is empty or
not.
*/
#include<iostream>
#include<queue>
#include<limits>
using namespace std;
//function that checks the cin stream for
//any invalid input format is given like giving string for number
as input.
bool isInvalidInput()
{
if(cin.fail())
{
//clear the buffer.
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
return true;
}
return false;
}
//display menu to user.
void menu()
{
cout<<"\n1. Enqueue a value"<<endl;
cout<<"2. Dequeue a value"<<endl;
cout<<"3. Front element in
queue"<<endl;
cout<<"4. Back element in
queue"<<endl;
cout<<"5. Print Queue"<<endl;
cout<<"0. Exit"<<endl;
cout<<"Enter your choice: ";
}
//print the queue and it's values.
void print(queue<int> q)
{
cout<<"\nQueue : \n";
queue<int> temp = q;
if(temp.empty())
{
cout<<"\nQueue is
Empty"<<endl;
return;
}
while(!temp.empty())
{
cout<<temp.front()<<"
";
temp.pop();
}
cout<<"\n";
}
//function that prompts the user with given message
//and returns the valid input that is an integer not text.
int getInt(string prompt)
{
bool isValid = false;
int val;
while(!isValid)
{
cout<<endl<<prompt;
cin >> val;
if(isInvalidInput())
{
cout<<"\nInvalid Input. Try Again"<<endl;
}
else
{
isValid =
true;
}
}
return val;
}
int main()
{
//create queue object. it takes a data type.
queue<int> list;
int choice = 1;
int val;
do
{
menu();
cin >> choice ;
if(isInvalidInput())
{
choice =
-1;
}
switch(choice)
{
case 1:
{
val = getInt("Enter value to Enqueue: ");
//call push to enqueue the value.
list.push(val);
break;
}
case 2:
//get size of
the queue.
if(list.size() > 0)
{
//store the front() of the
queue to display
val = list.front();
//pop the value from
queue
list.pop();
cout<<"\nValue Dequeued
from queue: "<<val<<endl;
}
else
{
cout<<"\nQueue is
Empty"<<endl;
}
break;
case 3:
if(list.size() > 0)
{
//get front() of the
queue.
val = list.front();
cout<<"\nValue at Front
of the queue: "<<val<<endl;
}
else
{
cout<<"\nQueue is
Empty"<<endl;
}
break;
case 4:
if(list.size() > 0)
{
val = list.back();
cout<<"\nValue at Back
of the queue: "<<val<<endl;
}
else
{
cout<<"\nQueue is
Empty"<<endl;
}
break;
case 5:
//call print()
method to print the queue.
print(list);
break;
case 0:
cout<<"\nQuitting...";
break;
default:
cout<<"\nInvalid Input. Try
again\n";
break;
}
}while(choice != 0);
}