Instructions: SLLQueue (13 pts) ● Using the three properties below, implement the queue interface using the SLLNode class...

50.1K

Verified Solution

Question

Programming

Instructions:

SLLQueue (13 pts)

● Using the three properties below, implement the

queue interface using the SLLNode class fromLab 2:

○ head_node → SLLNode object or None

○ tail_node → SLLNode object or None

○ size → int, keep track of queue size

● Implement enqueue( ),dequeue( ), front( )

● Use SLLNode methods:

get_item( ), set_item( ), get_next(), set_next( )

● (5 pts) Inenqueue(item):

â—‹ Add new SLLNode (with item) after tail_node

â—‹ Update tail_node and size properties

â—‹ If first item, update head_node property too

● (6 pts) In dequeue( ):

â—‹ If empty, raise Exception('Empty queue: cannot dequeue')

â—‹ Remove head node and return its item

â—‹ Remove any links from old head node

â—‹ Update head_node and size properties

â—‹ If queue becomes empty, reset head_node, tail_node

● (2 pts) In front( ):

â—‹ If empty, raise Exception(Empty queue: no front')

○ Return head node’s item

Code:

class SLLQueue:
   def __init__(self):
       # NOTE: DO NOT EDIT THIS CODE
       # constructor: set propertieshead_node, tail_node, size
       self.head_node = None
       self.tail_node = None
       self.size = 0

   def __repr__(self):
       # NOTE: DO NOT EDIT THIS CODE
       # string representation of SLLQueueobject
       display = []
       node = self.head_node
       while node != None:
          display.append(str(node.get_item()))
           node =node.get_next()
       display = ', '.join(display)
       return 'front [' + display +']'

   def is_empty(self):
       # NOTE: DO NOT EDIT THIS CODE
       # check if queue is empty
       return self.size == 0

   def enqueue(self,item):
       # TODO: Write your codehere...
       # TODO: add new node (with item)after tail_node
       # TODO: update tail_node and sizeproperties
       # TODO: if this is the first nodeadded, update head_node too
      
       pass # TODO: remove this line
      
   def dequeue(self):
       # TODO: Write your codehere...
       # TODO: if empty, raiseException('Empty queue: cannot dequeue')
       # TODO: remove head_node and returnits item
       # TODO: remove any links from oldhead_node (Hint: set to None)
       # TODO: update head_node and sizeproperties
       # TODO: if queue becomes empty,reset head_node and tail_node
      
       pass # TODO: remove this line

   def front(self):
       # TODO: Write your codehere...
       # TODO: if empty, raiseException('Empty queue: no front')
       # TODO: return head_node's item

       pass # TODO: remove thisline

Answer & Explanation Solved by verified expert
4.2 Ratings (607 Votes)
SLLNode class class SLLNode def initselfitemNonenextnodeNone selfitem item selfnextnode nextnode def setitemselfitem selfitem item0 def getitemself return selfitem def getnextself return selfnextnode def setnextselfnewnode selfnextnode newnode SLLQueue class class SLLQueue def initself constructor set properties headnode tailnode size    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students