Please answer in C++ and include a sample output. Thank you!! Create a class nam
ID: 666825 • Letter: P
Question
Please answer in C++ and include a sample output. Thank you!!
Create a class named waitList that can store a name and number of guests. Use constructors to automatically initialize the member variables.
Add the following operations to your program:
1)Return the first person in the queue
2)Return the last person in the queue
3)Add a person to the queue
4)Delete a person from the queue
Create a main program to test your class. Your main program should contain the following options. Create a menu for your options.
1)Add a guest (adds the reservation name and number of guests)
2)Delete a guest (the user must give you the name to delete, the list may be empty or the guest may not be in the list)
3)Show last guest waiting (return NONE if the queue is empty)
4)Show first guest waiting (return NONE if the queue is empty)
5)Exit
Explanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *link;
}*front, *rear;
class queue_list // class starts here
{
public:
void insert(int);
void display();
void del();
queue_list()
{
front = NULL;
rear = NULL;
}
};
int main()
{
int choice, item;
queue_list ql;
while (1)
{
cout<<" -------------"<<endl;
cout<<"Operations on Queue"<<endl;
cout<<" -------------"<<endl;
cout<<"1.Insert guest into the Queue"<<endl;
cout<<"2.Delete guest from the Queue"<<endl;
cout<<"3.Traverse the Queue"<<endl;
cout<<"4.Quit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted into the queue: ";
cin>>item;
ql.insert(item);
break;
case 2:
ql.del();
break;
case 3:
ql.display();
break;
case 4:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
void queue_list::insert(int item) // insert item
{
node *tmp;
tmp = new (struct node);
tmp->info = item;
tmp->link = NULL;
if (front == NULL)
front = tmp;
else
rear->link = tmp;
rear = tmp;
cout<<"guest inserted"<<endl;
}
void queue_list::del() // delete element
{
node *tmp;
if (front == NULL)
cout<<"Queue Underflow/ no one is waiting or NONE"<<endl;
else
{
tmp = front;
cout<<"Element Deleted: "<<tmp->info<<endl;
front = front->link;
free(tmp);
}
}
void queue_list::display() // retreival
{
node *ptr;
ptr = front;
if (front == NULL)
cout<<"Queue is empty"<<endl;
else
{
cout<<"Queue elements :"<<endl;
while (ptr != NULL)
{
cout<<ptr->info<<" ";
ptr = ptr->link;
}
cout<<endl;
}
}