In: Computer Science
Please post screenshots of outputs. Also make sure to use stacks and queues.
You will design a program to keep track of a restaurants waitlist using a queue implemented with a linked list. Make sure to read pages 1215-1217 and 1227-1251
1. Create a class named waitList that can store a name and number of guests. Use constructors to automatically initialize the member variables.
2. Add the following operations to your program:
a. Return the first person in the queue
b. Return the last person in the queue
c. Add a person to the queue
d. Delete a person from the queue
3. Create a main program to test your class. Your main program should contain the following options.
a. Add a guest (adds the reservation name and number of guests)
b. 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)
c. Show last guest waiting (return NONE if the queue is empty)
d. Show first guest waiting (return NONE if the queue is empty)
e. Exit Name your program LastName First Initial Lab9. Example. SerranoGLab9.cpp..
// C++ program to keep track of a restaurants waitlist using a queue implemented with a linked list.
#include <iostream>
using namespace std;
// waitList class to hold the guest name , number of guests and pointer to the next guest in the queue
class waitList
{
private:
string name;
int num_guests;
waitList *next;
public:
waitList(string name, int num_guests);
string getName() const;
int getNumGuests() const;
void setNext(waitList *next);
waitList *getNext() const;
};
// constructor
waitList::waitList(string name , int num_guests)
{
this->name = name;
this->num_guests = num_guests;
next = NULL;
}
// return the name of the guest
string waitList::getName() const
{
return name;
}
// return the number of guests
int waitList::getNumGuests() const
{
return num_guests;
}
// set the next guest in the queue
void waitList::setNext(waitList *next)
{
this->next = next;
}
// return the next guest in the queue
waitList* waitList::getNext() const
{
return next;
}
// Queue class to simulate the reservation list of the restaurant
class Queue
{
waitList *head, *tail; // pointers to start and end of queue
public:
Queue();
void insert(string name, int num_guests);
void remove(string name);
string getFirstPerson() const;
string getLastPerson() const;
~Queue();
};
// initialize an empty queue
Queue::Queue()
{
head = NULL;
tail = NULL;
}
// insert a guest at the end of the queue
void Queue::insert(string name, int num_guests)
{
waitList *node = new waitList(name, num_guests);
if(head == NULL)
{
head = node;
tail = head;
}else
{
tail->setNext(node);
tail = node;
}
}
// delete a guest from the queue
void Queue::remove(string name)
{
waitList *curr = head;
waitList *pre = NULL;
while(curr != NULL)
{
if(curr->getName() == name)
{
if(pre == NULL)
{
head = curr->getNext();
if(head == NULL)
tail = NULL;
}else
{
pre->setNext(curr->getNext());
if(pre->getNext() == NULL)
tail = pre;
}
delete(curr);
cout<<name<<" deleted from reservation list"<<endl;
return ;
}
pre = curr;
curr = curr->getNext();
}
cout<<name<<" not found in reservation list "<<endl;
}
// get the name of the first person in the queue, return "NONE" is queue is empty
string Queue::getFirstPerson() const
{
if(head != NULL)
return head->getName();
return "NONE";
}
// get the name of the last person in the queue, return "NONE" is queue is empty
string Queue::getLastPerson() const
{
if(tail != NULL)
return tail->getName();
return "NONE";
}
// delete the entire queue
Queue::~Queue()
{
while(head != NULL)
{
waitList *node = head;
head = head->getNext();
delete(node);
}
tail = NULL;
}
int main() {
Queue queue;
int choice, num_guests;
string name;
// loop that continues till the user wants
do
{
cout<<"\n1. Add a guest"<<endl;
cout<<"2. Delete a guest"<<endl;
cout<<"3. Show last guest waiting"<<endl;
cout<<"4. Show first guest waiting"<<endl;
cout<<"5. Exit"<<endl;
cout<<"Enter your choice(1-5) : ";
cin>>choice;// input fo choice
switch(choice)
{
case 1: // add a guest
cin.ignore(100,'\n');
cout<<"Enter the reservation name : ";
getline(cin,name);
name = name.substr(0,name.length()-1); // remove '\n' from the end
cout<<"Enter number of guests : ";
cin>>num_guests;
queue.insert(name,num_guests);
break;
case 2: // remove a guest
cin.ignore(100,'\n');
cout<<"Enter the reservation name : ";
getline(cin,name);
name = name.substr(0,name.length()-1); // remove '\n' from the end
queue.remove(name);
break;
case 3: // get and print the last guest
cout<<queue.getLastPerson()<<endl;
break;
case 4: // get and print the first guest
cout<<queue.getFirstPerson()<<endl;
break;
case 5:
break;
default:
cout<<"Wrong choice "<<endl;
}
}while(choice != 5);
cout<<"Thank you for using the application"<<endl;
return 0;
}
//end of program
Output: