Question

In: Computer Science

in python please ,, A SOON AS POSSIBLE PLEASE #Class invariant for SimpleQueue, a queue based...

in python please ,, A SOON AS POSSIBLE PLEASE
#Class invariant for SimpleQueue, a queue based on a linked list
#   1.  The queue is a linked list of ListNode objects.
#   2.  A SimpleQueue object has instance variables self.head, a reference to the node at the front of the queue, self.tail, a reference
#       to the node at the end of the queue, and self.size, the number of nodes in the queue.
#   3.  If self.size = 0, then self.head and self.tail are None.
#   4.  The ListNode object at the end (referred to by self.tail) has link None.  For all other ListNode objects,
#       the link refers to the next ListNode in the queue.
#   5.  The new item is enqueued at the tail of the SimpleQueue.
#   6.  An item is dequeued from the head or front of the SimpleQueue.

class SimpleQueue:
    '''a class for a simple queue implemented as a linked list with head and tail references'''

    def __init__(self):
        '''constructor that creates an empty queue'''


    def enqueue(self, item):
        '''post:  ListNode with item as its data has been inserted at the tail of the queue'''
      
        
    def dequeue(self):
        '''post: The ListNode at the head of the Queue has been removed from the queue and its item has been returned.'''
      

    def front(self):
        '''post: Returns the item at the front of the queue.  The ListNode at the front is not removed. '''
       

    def length(self):
        '''post:  Returns the number of items in the queue'''
      
class ListNode(object):
    
    def __init__(self, item = None, link = None):
        '''post: creates a ListNode with the specified data value and link''' 
        self.item = item
        self.link = link



            

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.




# Class invariant for SimpleQueue, a queue based on a linked list
#   1.  The queue is a linked list of ListNode objects.
#   2.  A SimpleQueue object has instance variables self.head, a reference to the node at the front of the queue, self.tail, a reference
#       to the node at the end of the queue, and self.size, the number of nodes in the queue.
#   3.  If self.size = 0, then self.head and self.tail are None.
#   4.  The ListNode object at the end (referred to by self.tail) has link None.  For all other ListNode objects,
#       the link refers to the next ListNode in the queue.
#   5.  The new item is enqueued at the tail of the SimpleQueue.
#   6.  An item is dequeued from the head or front of the SimpleQueue.

class SimpleQueue:
    """a class for a simple queue implemented as a linked list with head and tail references"""

    def __init__(self):
        """constructor that creates an empty queue"""
        self.front = None
        self.rear = None

    def enqueue(self, item):
        """post:  ListNode with item as its data has been inserted at the tail of the queue"""
        temp = ListNode(item)

        # Check if queue is empty
        if self.rear is None:
            self.front = temp
            self.rear = temp
        else:
            self.rear.link = temp
            self.rear = temp

    def dequeue(self):
        """post: The ListNode at the head of the Queue has been removed from the queue and its item has been returned."""
        if self.length() == 0:
            return None
        temp = self.front
        self.front = temp.link

        if self.front is None:
            self.rear = None
        return str(temp.item)

    def front(self):
        """post: Returns the item at the front of the queue.  The ListNode at the front is not removed. """
        return self.front

    def length(self):
        """post:  Returns the number of items in the queue"""
        temp = self.front
        count = 0
        while temp is not None:
            count += 1
            temp = temp.link
        return count


class ListNode(object):

    def __init__(self, item=None, link=None):
        """post: creates a ListNode with the specified data value and link"""
        self.item = item
        self.link = link


# TEST here
def main():
    queue = SimpleQueue()
    print('The length is:', queue.length())

    print('inserting 10,20,30..')
    queue.enqueue(10)
    queue.enqueue(20)
    queue.enqueue(30)

    print('Now, The length is:', queue.length())
    print('The Front is:', queue.front.item)

    print('\nDequeue: ', queue.dequeue())
    print('Dequeue: ', queue.dequeue())
    print('Now, The length is:', queue.length())
    print('The Front is:', queue.front.item)


main()

================

OUTPUT


Related Solutions

PLEASE ANSWER #8, 9 AND 10 AS SOON AS POSSIBLE. THANK YOU IF POSSIBLE PLEASE DO...
PLEASE ANSWER #8, 9 AND 10 AS SOON AS POSSIBLE. THANK YOU IF POSSIBLE PLEASE DO WHOLE QUESTIONS. I WANT TO DOUBLE CHECK WITH MINE Required:#1.Prepare journal entries to record the December transactions in the General Journal Tab in the excel template file "Accounting Cycle Excel Template.xlsx". Use the following accounts as appropriate: Cash, Accounts Receivable, Supplies, Prepaid Insurance, Equipment, Accumulated Depreciation, Accounts Payable, Wages Payable, Common Stock, Retained Earnings, Dividends, Service Revenue, Depreciation Expense, Wages Expense, Supplies Expense, Rent...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node will have a 4 digit job number and a priority (A, B, etc) with A highest priority In the driver Create a priority queue object Add 3 jobs of 'B' priority Add 4 jobs of 'D' priority Add 2 jobs of highest priority Print the queue
Answer as soon as possible!!! Code must be done with Python Develop a basic File Transfer...
Answer as soon as possible!!! Code must be done with Python Develop a basic File Transfer Protocol (FTP) application that transmits files between a client and a server using Python. Your programs should implement four FTP commands: (1) change directory (cd), (2) list directory content (ls), (3) copy a file from client to a server (put) and (4) copy a file from a server to a client (get). Implement two versions of the program: one that uses stock TCP for...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Please answer the questions as soon as possible. Thanks in advance. Please explain in brief in...
Please answer the questions as soon as possible. Thanks in advance. Please explain in brief in one to two paragraphs and provide graphs. 1. In two paragraphs, describe the sources of the gains from trade and why countries use import tariffs. 2b In two paragraphs, describe how the COVID-19 pandemic has impacted the global macroeconomy and multiple ways it has impacted a specific agricultural commodity market (e.g. corn, beef, wheat).
i REALLY NEED REPLY AS SOON AS POSSIBLE: PLEASE PLEASE MAKE THE ASSIGNMENT ON THIS TASK...
i REALLY NEED REPLY AS SOON AS POSSIBLE: PLEASE PLEASE MAKE THE ASSIGNMENT ON THIS TASK OF Business Ideas based on a indian restaurants Purpose: Develop and formulate an idea for your own business. Money is not an issue for this assignment. Envision that you get all the funding you need to start it up. Task: Write a short abstract (200-250 Words) describing your business idea in the discussion section of this assignment. What is the basic idea? What are...
i REALLY NEED REPLY AS SOON AS POSSIBLE: PLEASE PLEASE MAKE THE ASSIGNMENT ON THIS TASK...
i REALLY NEED REPLY AS SOON AS POSSIBLE: PLEASE PLEASE MAKE THE ASSIGNMENT ON THIS TASK OF Business Ideas based on a indian restaurants Purpose: THIS IS THE QUESTION : Develop and formulate an idea for your own business. Money is not an issue for this assignment. Envision that you get all the funding you need to start it up. Task: Write a short abstract (200-250 Words) describing your business idea in the discussion section of this assignment. What is...
i REALLY NEED REPLY AS SOON AS POSSIBLE: PLEASE PLEASE MAKE THE ASSIGNMENT ON THIS TASK...
i REALLY NEED REPLY AS SOON AS POSSIBLE: PLEASE PLEASE MAKE THE ASSIGNMENT ON THIS TASK OF Business Ideas based on a indian restaurants Purpose: Develop and formulate an idea for your own business. Money is not an issue for this assignment. Envision that you get all the funding you need to start it up. Task: Write a short abstract (200-250 Words) describing your business idea in the discussion section of this assignment. What is the basic idea? What are...
i REALLY NEED REPLY AS SOON AS POSSIBLE: PLEASE PLEASE MAKE THE ASSIGNMENT ON THIS TASK...
i REALLY NEED REPLY AS SOON AS POSSIBLE: PLEASE PLEASE MAKE THE ASSIGNMENT ON THIS TASK OF Business Ideas based on a indian restaurants Purpose: Develop and formulate an idea for your own business. Money is not an issue for this assignment. Envision that you get all the funding you need to start it up. Task: Write a short abstract (200-250 Words) describing your business idea in the discussion section of this assignment. What is the basic idea? What are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT