In: Computer Science
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
She is requiring Stacks and Queues. There are various ways to do this code, but I am really unfamiliar with this one.
Hi, please find the solution. Explanations embedded inline. Thanks.
Main program to test.
package com.company; public class Main { public static void main(String[] args) { Person p1 = new Person("Name 1",5); Person p2 = new Person("Name 2",4); Person p3 = new Person("Name 3",6); Person p4 = new Person("Name 4",3); Person p5 = new Person("Name 5",7); Person p6 = new Person("Name 6",5); Person p7 = new Person("Name 7",34); Person p8 = new Person("Name 8",2); Person p9 = new Person("Name 9",1); WaitList w = new WaitList(p1); w.addPerson(p2); w.addPerson(p3); w.addPerson(p4); w.addPerson(p5); w.addPerson(p6); System.out.println("The first person waiting is "+w.showLastGuestWaiting()); System.out.println("The persn who is getting a serve now is "+w.returnFirstPerson()); System.out.println("The just arrived person is "+w.returnLastPerson()); } }
package com.company; import java.util.Objects; public class Person { String reservationName; int noOfGuests; @Override public String toString() { return reservationName; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return getReservationName().equals(person.getReservationName()); } public Person next; public Person(String reservationName, int noOfGuests) { this.reservationName = reservationName; this.noOfGuests = noOfGuests; } public String getReservationName() { return reservationName; } public void setReservationName(String reservationName) { this.reservationName = reservationName; } public int getNoOfGuests() { return noOfGuests; } public void setNoOfGuests(int noOfGuests) { this.noOfGuests = noOfGuests; } }
package com.company; public class WaitList { public static Person front=null; WaitList(Person p) { if(p==null)return; if (front == null) { front = p; } else if (front != null) { Person temp=front; while (temp.next != null) { temp = temp.next; } temp.next = p; p.next=null; } } public void addPerson(String name, int noOfGuests) { Person p = new Person(name, noOfGuests); functionalAddPerson(p); } public void addPerson(Person p){ if(p==null)return; functionalAddPerson(p); } private void functionalAddPerson(Person p) { if (front == null) { front = p; } else { p.next = front; front = p; } } public Person returnFirstPerson() { //here the first person means who came first to the restaurant, meaning last node in the linked list. return getFirstWaitingPerson(true);//true means delete the person who came first } private Person getFirstWaitingPerson(boolean toBeDeleted) { Person toReturn = null; if (front == null) { return null; } else if (front.next == null) { toReturn = front; if (toBeDeleted) { front = null; } return toReturn; } else { Person temp = front; while (temp.next != null) { if (temp.next.next == null) { toReturn = temp.next; if (toBeDeleted) { temp.next = null; } return toReturn; } temp = temp.next; } } return toReturn; } public Person returnLastPerson() {//this means who has just registered his entry Person toReturn; if (front == null) { return null; } else if (front.next == null) { toReturn = front; front = null; return toReturn; } else { toReturn = front;//returning the just inserted person front = front.next;//making the front point to the second object so that the first can be removed. toReturn.next = null;//removing assignments from the linked list. return toReturn; } } public Person showLastGuestWaiting() {//here we have to get to the end of the queue and not delete the person from the queue return getFirstWaitingPerson(false);//we return the person without deleting him. } }
Following images (output)show that the queue was implemented using Linked list.
Sample output:
You can also see in the below screenshot that the name1 is missing since he is got a serve, after the call of returnFirstPerson method.
In the above screen shot "Name 1" is missing since he got a serve and removed from the queue. He came first hence got serve. Thanks