Implement a Queue data structure as an ADT that does the following: 1) It simula
ID: 3711757 • Letter: I
Question
Implement a Queue data structure as an ADT that does the following:
1) It simulates a Queue in an Ice cream Shop by
a) reading the person info (such as the name, and the type of ice cream he wants to order)
b) adds the info to the Queue
c) delete the Person info from the Queue when the ice cream is served.
2) Make sure when the program executes,
a) it must provide a Main Menu for the program user so he can choose the task and the different task must be:
1) to add a person to the Queue
2) to delete the person when the person is served
3) to print the Queue information
4) to Exit
b) the program must continue running until the user chooses to Exit.
3) The Program must
a) allow the user to choose the tasks in any order from the Main Menu
b) the Queue must reflect (display) the correct Queue information at all points of time.
Explanation / Answer
//C++
#include<iostream>
#include<string>
using namespace std;
//The Queue Data Structure using Linked List
//Linked list is collection of nodes one pointing to another
//Ecample 1->2->3->4->NULL
//Basic node of the List
struct OrderInfo
{
string CustomerName;
string IceCreamType;
OrderInfo * next_order;
OrderInfo(string _name, string _iceCreamType)
{
CustomerName=_name;
IceCreamType=_iceCreamType;
next_order=NULL;
}
};
//A class to implement the icecream shop queue
class IceCreamShopQueue
{
OrderInfo* front; //represents start of the queue
OrderInfo* end; //represents end of the queue
public:
IceCreamShopQueue()
{
front=NULL;
end=NULL;
}
void InsertInfo(string _name, string _iceCreamType)
{
//create a record
OrderInfo * _order = new OrderInfo(_name,_iceCreamType);
//Insert for an empty queue
if(end==NULL && front==NULL)
{
end=_order;
front=end;
return;
}
//Insert at the end of a non empty queue
end->next_order=_order;
end=_order;
}
void Delete()
{
//Check underflow condition(A situation when the queue is empty)
if(front==NULL && end==NULL)
{
cout<<"No Orders to Delete"<<endl;
return;
}
// Free the memory allocated in the heap using new and assign the front to the next order
OrderInfo *_order=front;
front=_order->next_order;
delete _order;
}
void Display()
{
OrderInfo * _order=front;
int serialNumber =1;
cout<<"IceCreamShopQueue"<<endl;
while(_order!=NULL)
{
//print the order
cout<<serialNumber<<") ";
cout<<"Customer Name : "<<_order->CustomerName<<" Ice Cream Type: "<<_order->IceCreamType<<endl;
//move to the next order
_order=_order->next_order;
serialNumber++;
}
//for indentation
cout<<endl;
}
};
int main()
{
bool shouldContinue=true;
IceCreamShopQueue _iceCreamQueue;
//create a menu
while(shouldContinue)
{
int choice;
cout<<"**************** MENU ******************"<<endl;
cout<<"1) To add a person to the Queue"<<endl;
cout<<"2) To delete the person when the person is served"<<endl;
cout<<"3) To print the Queue information"<<endl;
cout<<"4) To Exit"<<endl;
cout<<"Please enter your choice (Example enter 1 for adding...)"<<endl;
cin>>choice;
switch(choice)
{
case 1:
{
string _name,_iceCreamType;
cout<<"Enter the Name : ";
cin>>_name;
cout<<"Enter the IceCreamType: ";
cin>>_iceCreamType;
_iceCreamQueue.InsertInfo(_name,_iceCreamType);
_iceCreamQueue.Display();
break;
}
case 2:
_iceCreamQueue.Delete();
_iceCreamQueue.Display();
break;
case 3:
_iceCreamQueue.Display();
break;
case 4:
shouldContinue=false;
break;
default:
cout<<"Not a valid option Please try again"<<endl;
break;
};
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.