Question

Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user if they want to add a customer to the queue, serve the next customer in the queue, or exit. When a customer is served or added to the queue, the program should print out the name of that customer and the remaining customers in the queue.

The store has two queues: one is for normal customers, another is for VIP customers. Normal customers can only wait in the normal customers’ queue, and VIP customers can only wait in the VIP customers’ queue.

Each time a new customer comes; the user should input the customer name and whether he/she is VIP, then put the user in the corresponding queue. You should always serve VIP customers first! When add/ serve customers, you also need to print out the current customer name, and names of all customers in each queue. If you want to add one customer but the corresponding queue is full, your program should print: “Error: Normal customers queue is full” or “Error: VIP customers queue is full”. When you want to serve one customer, if both queues are empty, your program should print: “Error: Both queues are empty”. • You may restrict the capacity of each queue to 3 for testing simplicity. Each queue (normal or VIP) should be an instance of the CircularQueue class. Therefore, this class should be included in your program. Following is the output from a sample run of the program
Add, Serve, or Exit: add Enter the name of the person to add: Alice Is the customer VIP? False add Alice to the line people in the line JAlice] VIP customers queue: ]] Add, Serve, or Exit: add Enter the name of the person to add: Bob Is the customer VIP? False add Bob to the line. people in the line JAlice, Bob] VIP customers queue: ] Add, Serve, or Exit: add Enter the name of the person to add: John Is the customer VIP? False add John to the line. people in the Line: JAI1ce, Bob, JohnJ VIP customers queue: ] Add, Serve, or Exit: add Enter the name of the person to add: Mike

   Q2 The goal of this exercise is to create a doubly linked list. You have to use the file d_linked_node.py and the d_linked_list.py .The d_linked_node is ready. You need to
complete the d_linked_list.py. Please note that item is the value in the d_linked_node and not the next and previous.
Methods given in the d_linked_list.py file
• search(self, item)
   – Returns true if the item is inside the list false
otherwise
• index(self,item)
– Returns the index of the item or -1 if the item is not in the list
   Methods to be completed in the d_linked_list.py file
• add(self, item) – Adds a new item to the front of the list
  • remove(self,item)
   – Removes the first element that is equal to item from the list. if that doesn't exist it changes nothing
   • append(self, item)
   – Adds item to the end of the list
   • insert(self, pos, item)
   – Add item to the given postition in the list
  (i.e if pos is 0 then item goes to the beginning of the list)
   • pop1(self)
   – Removes and returns the last item in the list
   • pop(self, pos)
   – Removes and returns the item in the given postion
   • search_larger(self, item)
   – Returns the position of the first item that is larger than item, -­?1 if there is no item larger
   • get_size(self)
   – Returns the size of the list
   • get_item(self, pos)
   
   – Returns the item that exists at pos, Raises exception if pos doesn’t exist
– pos can be negative which means the position from the end of the list. (-­? 1 is the last item, -­?2 the second from last etc.)
   
   • __str__(self)
   
   – Returns a string that is the elements of the DLinkedList with spaces in between
   Once you have completed the method d_linked_list.py file, you can run the test function in provided in the d_linked_list.py file to determine if the methods have been implemented correctly or not.

d_linked_node.py

class d_linked_node:
   
    def __init__(self, initData, initNext, initPrevious):
        # constructs a new node and initializes it to contain
        # the given object (initData) and links to the given next
        # and previous nodes.
        self.__data = initData
        self.__next = initNext
        self.__previous = initPrevious
        if (initPrevious != None):
            initPrevious.__next = self
        if (initNext != None):
            initNext.__previous = self
   
    def getData(self):
        return self.__data
   
    def getNext(self):
        return self.__next
   
    def getPrevious(self):
        return self.__previous
   
    def setData(self, newData):
        self.__data = newData
   
    def setNext(self, newNext):
        self.__next= newNext
   
    def setPrevious(self, newPrevious):
        self.__previous= newPrevious   

d_linked_list.py

from d_linked_node import d_linked_node

class d_linked_list:
   
    def __init__(self):
        self.__head=None
        self.__tail=None
        self.__size=0       

           
    def search(self, item):
        current = self.__head
        found = False
        while current != None and not found:
            if current.getData() == item:
                found= True
            else:
                current = current.getNext()
        return found
       
    def index(self, item):
        current = self.__head
        found = False
        index = 0
        while current != None and not found:
            if current.getData() == item:
                found= True
            else:
                current = current.getNext()
                index = index + 1
        if not found:
                index = -1
        return index       
        
    def add(self, item):
        # TODO:
       
    def remove(self, item):
        # TODO:
       
    def append(self, item):
        # TODO:
       
    def insert(self, pos, item):
        # TODO:
       
    def pop1(self):
        # TODO:
       
    def pop(self, pos):
        # TODO:
       
    def search_larger(self, item):
        # TODO:
       
    def get_size(self):
        # TODO:   
   
    def get_item(self, pos):
        # TODO:  
       
    def __str__(self):
        # TODO:  
       

def test():
                 
    linked_list = d_linked_list()
                   
    is_pass = (linked_list.get_size() == 0)
    assert is_pass == True, "fail the test"
           
    linked_list.add("World")
    linked_list.add("Hello")   
       
    is_pass = (str(linked_list) == "Hello World")
    assert is_pass == True, "fail the test"
             
    is_pass = (linked_list.get_size() == 2)
    assert is_pass == True, "fail the test"
           
    is_pass = (linked_list.get_item(0) == "Hello")
    assert is_pass == True, "fail the test"
       
       
    is_pass = (linked_list.get_item(1) == "World")
    assert is_pass == True, "fail the test"   
           
    is_pass = (linked_list.get_item(0) == "Hello" and linked_list.get_size() == 2)
    assert is_pass == True, "fail the test"
           
    is_pass = (linked_list.pop(1) == "World")
    assert is_pass == True, "fail the test"    
           
    is_pass = (linked_list.pop1() == "Hello")
    assert is_pass == True, "fail the test"    
           
    is_pass = (linked_list.get_size() == 0)
    assert is_pass == True, "fail the test"
                   
    int_list2 = d_linked_list()
                   
    for i in range(0, 10):
        int_list2.add(i)     
    int_list2.remove(1)
    int_list2.remove(3)
    int_list2.remove(2)
    int_list2.remove(0)
    is_pass = (str(int_list2) == "9 8 7 6 5 4")
    assert is_pass == True, "fail the test"
               
    for i in range(11, 13):
        int_list2.append(i)
    is_pass = (str(int_list2) == "9 8 7 6 5 4 11 12")
    assert is_pass == True, "fail the test"
               
    for i in range(21, 23):
        int_list2.insert(0,i)
    is_pass = (str(int_list2) == "22 21 9 8 7 6 5 4 11 12")
    assert is_pass == True, "fail the test"
               
    is_pass = (int_list2.get_size() == 10)
    assert is_pass == True, "fail the test"   
                   
    int_list = d_linked_list()
                   
    is_pass = (int_list.get_size() == 0)
    assert is_pass == True, "fail the test"
                   
       
                   
    for i in range(0, 1000):
        int_list.append(i)     
    correctOrder = True
           
    is_pass = (int_list.get_size() == 1000)
    assert is_pass == True, "fail the test"           
           
       
    for i in range(0, 200):
        if int_list.pop1() != 999 - i:
            correctOrder = False
                           
    is_pass = correctOrder
    assert is_pass == True, "fail the test"
           
    is_pass = (int_list.search_larger(200) == 201)
    assert is_pass == True, "fail the test"
           
    int_list.insert(7,801)  
       
    is_pass = (int_list.search_larger(800) == 7)
    assert is_pass == True, "fail the test"
           
           
    is_pass = (int_list.get_item(-1) == 799)
    assert is_pass == True, "fail the test"
           
    is_pass = (int_list.get_item(-4) == 796)
    assert is_pass == True, "fail the test"
                   
    if is_pass == True:
        print ("=========== Congratulations! Your have finished exercise 1! ============")
               
if __name__ == '__main__':
    test()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solving Q.2 . Please post Q.1 Saperatly

class d_linked_node:

              

               def __init__(self, initData, initNext, initPrevious):

                              # constructs a new node and initializes it to contain

        # the given object (initData) and links to the given next

        # and previous nodes.

                              self.__data = initData

                              self.__next = initNext

                              self.__previous = initPrevious

                              if (initPrevious != None):

                                             initPrevious.__next = self

                              if (initNext != None):

                                             initNext.__previous = self

   

               def getData(self):

                              return self.__data

                             

               def getNext(self):

                              return self.__next

   

               def getPrevious(self):

                              return self.__previous

   

               def setData(self, newData):

                              self.__data = newData

   

               def setNext(self, newNext):

                              self.__next= newNext

   

               def setPrevious(self, newPrevious):

                              self.__previous= newPrevious

# end of d_linked_node

from d_linked_node import d_linked_node

class d_linked_list:

              

               def __init__(self):

                              self.__head=None

                              self.__tail=None

                              self.__size=0       

               def search(self, item):

                              current = self.__head

                              found = False

                              while current != None and not found:

                                             if current.getData() == item:

                                                            found= True

                                             else:

                                                            current = current.getNext()

                              return found

               def index(self, item):

                              current = self.__head

                              found = False

                              index = 0

                              while current != None and not found:

                                             if current.getData() == item:

                                                            found= True

                                             else:

                                                            current = current.getNext()

                                                            index = index + 1

                              if not found:

                                             index = -1

                              return index   

               def add(self, item):

        # adds an item to list at the beginning

                              temp = d_linked_node(item, self.__head, None)

                              if self.__head != None:

                                             self.__head.setPrevious(temp)

                              else:

                                             self.__tail=temp

                              self.__head = temp

                              self.__size += 1

                             

               def remove(self, item):

        # search for the item and remove it

        # the method assumes the item exists

                              current = self.__head

                              previous=None

                              found = False

                              while not found:

                                             if current.getData() == item:

                                                            found = True

                                             else:

                                                            previous = current

                                                            current = current.getNext()

                              if previous == None:

                                             self.__head = current.getNext()

                              else:

                                             previous.setNext(current.getNext())

                              if (current.getNext() != None):

                                             current.getNext().setPrevious(previous)

                              else:

                                             self.__tail=previous

                              self.__size -= 1

               def append(self, item):

        # adds the item to the end of the list

        # must traverse the list to the end and add item

                              temp = d_linked_node(item, None, None)

                              if (self.__head == None):

                                             self.__head=temp

                              else:

                                             self.__tail.setNext(temp)

                                             temp.setPrevious(self.__tail)

                              self.__tail=temp

                              self.__size +=1

                             

               def insert(self, pos, item):

                              # insert an element in index pos

                              current = self.__head

                              temp = d_linked_node(item, None, None)

                              index = 0

                             

                              while current != None:

                                             if index == pos:

                                                           

                                                            if(current.getPrevious() != None):

                                                                           temp.setPrevious(current.getPrevious())

                                                                           current.getPrevious().setNext(temp)

                                                                           temp.setNext(current)

                                                                           current.setPrevious(temp)

                                                                           self.__size = self.__size + 1

                                                                           return

                                                            else:

                                                                           self.add(item)

                                                                           return

                                             index = index + 1                             

                                             current = current.getNext()

                              if index == pos:

                                             self.append(item)

                                             self.__size = self.__size + 1

                                            

              

               def pop1(self):

                              # pop the last element from the list

                              if(self.get_size() == 0):

                                             return None

                              if(self.get_size() == 1):

                                             element = self.__head.getData()

                                             self.__head = self.__tail = None

                                             self.__size = self.__size - 1

                                             return element

                              temp = self.__head

                              while(temp.getNext() != None):  

                                             temp = temp.getNext()

                             

                              element = temp.getData()

                              temp.getPrevious().setNext(None)

                              self.__tail = temp.getPrevious()  

                              self.__size = self.__size - 1

                              return element

               def pop(self, pos):

                              # pop the element at pos

                              current = self.__head

                              index = 0

                              found = False

                              if pos == 0:

                                             return pop1()

                              while current != None and not found:      

                                             if index == pos:

                                                            element = current.getData()

                                                            current.getPrevious().setNext(current.getNext())

                                                            if current.getNext() != None:

                                                                           current.getNext().setPrevious(current.getPrevious())

                                                            self.__size = self.__size - 1           

                                                            return element

                                             current = current.getNext()

                                             index = index + 1

                              return None

               def search_larger(self, item):

               # search and return the first item larger than the input item

                              current = self.__head

                              index = 0

                              while current != None:

                                             if current.getData() > item:

                                                            return index

                                             current = current.getNext()

                                             index = index + 1

                              return None

                             

               def get_size(self):

               # return the size of the lined list  

                              return self.__size

              

               def get_item(self, pos):

               # get the item at pos

                                                           

                              if pos >= 0:

                                             index = 0

                                             current = self.__head

                                             while current != None:

                                                            if index == pos:

                                                                           return current.getData()

                                                            current = current.getNext()

                                                            index = index + 1

                              else:

                                             index = -1

                                             current = self.__tail

                                             while current != None:

                                                            if index == pos:

                                                                           return current.getData()

                                                            current = current.getPrevious()

                                                            index = index - 1

                              return None

              

               def __str__(self):

               # return string representation of linked list

                              if(self.get_size() == 0):

                                             return "Empty list"

                              current = self.__head

                              nodeList = ""

                              while(current.getNext() != None):

                                             nodeList = nodeList + str(current.getData())+" "

                                             current = current.getNext()

                              nodeList = nodeList + str(current.getData())

                              return nodeList

                             

                             

              

def test():

               linked_list = d_linked_list()

               is_pass = (linked_list.get_size() == 0)

               assert is_pass == True, "fail the test"

           

               linked_list.add("World")

               linked_list.add("Hello")   

              

               is_pass = (str(linked_list) == "Hello World")

   

               assert is_pass == True, "fail the test"

             

               is_pass = (linked_list.get_size() == 2)

               assert is_pass == True, "fail the test"

               is_pass = (linked_list.get_item(0) == "Hello")

               assert is_pass == True, "fail the test"

               is_pass = (linked_list.get_item(1) == "World")

              

               assert is_pass == True, "fail the test"   

               is_pass = (linked_list.get_item(0) == "Hello" and linked_list.get_size() == 2)

               assert is_pass == True, "fail the test"

           

               is_pass = (linked_list.pop(1) == "World")

               assert is_pass == True, "fail the test"    

           

               is_pass = (linked_list.pop1() == "Hello")

               assert is_pass == True, "fail the test"    

          

               is_pass = (linked_list.get_size() == 0)

               assert is_pass == True, "fail the test"

              

               int_list2 = d_linked_list()

                   

               for i in range(0, 10):

                              int_list2.add(i)     

               int_list2.remove(1)

               int_list2.remove(3)

               int_list2.remove(2)

               int_list2.remove(0)

               is_pass = (str(int_list2) == "9 8 7 6 5 4")

               assert is_pass == True, "fail the test"

               for i in range(11, 13):

                              int_list2.append(i)

               is_pass = (str(int_list2) == "9 8 7 6 5 4 11 12")

               assert is_pass == True, "fail the test"

               

               for i in range(21, 23):

                              int_list2.insert(0,i)

               is_pass = (str(int_list2) == "22 21 9 8 7 6 5 4 11 12")

               assert is_pass == True, "fail the test"

               

               is_pass = (int_list2.get_size() == 10)

               assert is_pass == True, "fail the test"   

                   

               int_list = d_linked_list()

                   

               is_pass = (int_list.get_size() == 0)

               assert is_pass == True, "fail the test"

              

               for i in range(0, 1000):

                              int_list.append(i)     

               correctOrder = True

           

               is_pass = (int_list.get_size() == 1000)

               assert is_pass == True, "fail the test"           

           

       

               for i in range(0, 200):

                              if int_list.pop1() != 999 - i:

                                             correctOrder = False

                           

               is_pass = correctOrder

               assert is_pass == True, "fail the test"

           

               is_pass = (int_list.search_larger(200) == 201)

               assert is_pass == True, "fail the test"

           

               int_list.insert(7,801)  

      

               is_pass = (int_list.search_larger(800) == 7)

               assert is_pass == True, "fail the test"

   

               is_pass = (int_list.get_item(-1) == 799)

               assert is_pass == True, "fail the test"

           

               is_pass = (int_list.get_item(-4) == 796)

               assert is_pass == True, "fail the test"

                   

               if is_pass == True:

                              print ("=========== Congratulations! Your have finished exercise 1! ============")

if __name__ == '__main__' :

               test()                    

              

# end of d_linked_list

Add a comment
Know the answer?
Add Answer to:
Q1. Write a program to simulate a grocery waiting queue. Your program should ask the user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of...

    Task 2: SecretWord class: Download and save a copy of LinkedLists.py (found at the bottom of this assignment page on eClass). In this file, you have been given the complete code for a LinkedList class. Familiarize yourself with this class, noticing that it uses the Node class from Task 1 and is almost identical to the SLinkedList class given in the lectures. However, it also has a complete insert(pos, item) method (which should be similar to the insert method you...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

  • PYTHON - Write and test the following program that meets the following conditions ------------------------------------------------------- write a...

    PYTHON - Write and test the following program that meets the following conditions ------------------------------------------------------- write a program that Tests the methods of stack for empty and non-empty stacks using the data in is_empty, push, pop, peek *Testing pop and peek while empty throws exception ------------------------------------------------------- CONDITIONS source - list of data (list of ?) RETURNS None ------------------------------------------------------- CODE FOR : Empty, Push, Pop & Peek def is_empty(self):    result = True if len(self._values) > 0: result = False return result...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...

    I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...

  • Python 3: Write a LinkedList method named contains, that takes a value as a parameter and...

    Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...

  • starter code To write a program using the starter code which is TestLinkedList to see if...

    starter code To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures. LinkedList.java /** * @author someone * * Implements a double-linked list with four errors */ public class LinkedList<E> { // The first and last nodes in the list private Node<E> head, tail; // Number of items stored in the list private int size; //...

  • Write a simple todo list application. This will require use of classes, objects, and possibly structs....

    Write a simple todo list application. This will require use of classes, objects, and possibly structs. Here are the requirements: 1. Menu-driven - upon running the program, the user should be prompted to press one of the following: 1. View all Todos in list 2. Remove a todo item from the list * if there are not any todos, the user should be prompted and sent back to the menu * choose a todo from list based on title to...

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT