Question

In: Computer Science

Please post screenshots of outputs. Also make sure to use stacks and queues. You will design...

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..

Solutions

Expert Solution

// 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:


Related Solutions

OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS SCAN This...
OPERATING SYSTEMS HOMEWORK: PLEASE CODE IN JAVA with comments & POST SCREENSHOTS OF OUTPUTS SCAN This algorithm is performed by moving the R/W head back-and-forth to the innermost and outermost track. As it scans the tracks from end to end, it process all the requests found in the direction it is headed. This will ensure that all track requests, whether in the outermost, middle or innermost location, will be traversed by the access arm thereby finding all the requests. This...
Programming Project – Deques, Stacks & Queues General Description: Design and develop array based and linked...
Programming Project – Deques, Stacks & Queues General Description: Design and develop array based and linked list-based implementations for the Dequeue ADT. Your implementation must support generic data types using C++ templates. Develop Adapter Files to provide Stack and Queue functionality for the Deques. Definitions: You should implement the ADTs precisely as described in the following partial header files. Deque.h template class Deque { public:         Deque();                    //constructor         ~Deque();                 //destructor         void insertFront(const E& e);...
Stacks & Queues C++ You are given a stack of N integers such that the first...
Stacks & Queues C++ You are given a stack of N integers such that the first element represents the top of the stack and the last element represents the bottom of the stack. You need to pop at least one element from the stack. At any one moment, you can convert stack into a queue. The bottom of the stack represents the front of the queue. You cannot convert the queue back into a stack. Your task is to remove...
Please show fully functioning Java code and screenshots of outputs. Please separate by 1a and 1b....
Please show fully functioning Java code and screenshots of outputs. Please separate by 1a and 1b. #1. Design a Java JProduct class for a product which implements both cloneable and comparable interfaces The class should have the following private member variables: m_id: an integer that holds the product ID m_name: a string that holds the product name m_wholesaleprice: a double that holds the wholesale price m_retailers: a String array that holds all retailers who sell the product and the class...
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write...
C++ Progamming This lab gives you a little practice with stacks and queues ·In “stackfib.cpp”, write a non-recursive function fib() which: ·Takes a single int parameter n ·Returns the nth Fibonacci number. We will start with fib(0) == fib(1) == 1, then fib(n) = fib(n-1) + fib(n-2) ·To compute this without using recursion, we use a stack to store the numbers we need to compute (1)   Initialize your result to be 0 (2)   Push the parameter n onto the stack (3)   While the...
(C++ Programming) Practice with Stacks and Queues (PLEASE FOLLOW ALL DIRECTIONS) ·In “stackfib.cpp”, write a non-recursive...
(C++ Programming) Practice with Stacks and Queues (PLEASE FOLLOW ALL DIRECTIONS) ·In “stackfib.cpp”, write a non-recursive function fib() which: ·Takes a single int parameter n ·Returns the nth Fibonacci number. We will start with fib(0) == fib(1) == 1, then fib(n) = fib(n-1) + fib(n-2) ·To compute this without using recursion, we use a stack to store the numbers we need to compute (1)   Initialize your result to be 0 (2)   Push the parameter n onto the stack (3)   While the stack is...
Use C++ please You will be building a linked list. Make sure to keep track of...
Use C++ please You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. PlaylistNode.h - Class declaration PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt)...
projectOne (Please be sure to include screenshots of the test runs for all programs) A. write...
projectOne (Please be sure to include screenshots of the test runs for all programs) A. write a program the computes nx and store the result into y You can use y = Math.pow( Mantissa, exponent) Requirements: Besides main() your program must have one method with two parameters, one double and one int n and x are positive numbers read from the keyboard if the user makes an entry that does not meet this criteria, the user must be given to...
You have decided to open a business. What ownership method will you use? Please make sure...
You have decided to open a business. What ownership method will you use? Please make sure to include three points to support your decision.
Please Use C language to Make a calculator. Make sure calculator is able to take up...
Please Use C language to Make a calculator. Make sure calculator is able to take up to 5 values. Try to make calculator using simple coding As you can. Please create a simple calculator with only +, -,* and divide. It has to be able to enter any numbers (including decimals) and be able to do the following functions: +, -, divide and multiply. Please have the answers, always rounded to two decimal figures. The calculator will also have to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT