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

PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue will call on a linkedlist class class Queue: def __init__(self): self.items = LinkedList() #has to be O(1) def enqueue(self, item): #has to be O(1) def dequeue(self): def is_empty(self): def __len__(self):
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...
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).
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 POST A COMMENT ON BEFORE AS SOON AS POSSIBLE It is important to consider how...
PLEASE POST A COMMENT ON BEFORE AS SOON AS POSSIBLE It is important to consider how tax rates affect the value of deferred tax assets. If the tax rate goes up, it works to the company’s favor because the assets’ values also go up, therefore providing a bigger cushion for a larger income. But if the tax rate drops, the tax asset value also declines. As head of the Accountant department the person in this role has a responsibility to...
Please answer as soon as possible, I will upvote if it is answered in 40 minutes....
Please answer as soon as possible, I will upvote if it is answered in 40 minutes. Suppose that a bond portfolio with a duration of 5 years is hedged using a futures contract in which the underlying asset has a duration of 14 years. What is likely to be the impact on the hedge of the fact that the 14-year rate is less volatile than the 5-year rate?
Please Reply with an answer as soon as possible, will appreciate it so much. Thanks in...
Please Reply with an answer as soon as possible, will appreciate it so much. Thanks in advance. Case Problem: You are a HR assistant manager in a company employing many expatriates from around the world. Your company has selected ‘FutureFunds Pension Planners’ to provide your employees with an international pension plan that takes advantage of tax laws. The plan allows the company to make tax free contributions to the employee pension funds. It also allows Individuals enrolling in the plan...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT