How to implement Queue in python using list?

By Neha, 7 months ago
  • Bookmark
0

implement queue using list methods

Queue
List
Python
1 Answer
0

Implementation of Queue :

Queue Methods and Attributes :


  • Queue() creates a new queue that is empty. It needs no parameters and returns an empty queue.
  • enqueue(item) adds a new item to the rear of the queue. It needs the item and returns nothing.
  • dequeue() removes the front item from the queue. It needs no parameters and returns the item. The queue is modified.
  • isEmpty() tests to see whether the queue is empty. It needs no parameters and returns a boolean value.
  • size() returns the number of items in the queue. It needs no parameters and returns an integer.


Queue Implementation :

class Queue:
    def __init__(self):
      self.items = []
  
    def isEmpty(self):
      return self.items == []
  
    def enqueue(self, item):
      self.items.insert(0,item)
  
    def dequeue(self):
      return self.items.pop()
  
    def size(self):
      return len(self.items)

    def print(self):                     
        return self.items
  
  
q = Queue()
    
q.size() # output 0
  
q.isEmpty() # output True
  
q.enqueue(1)  

q.print() # output [1]
  
q.dequeue() # output  1

Your Answer

Webinars

More webinars

Related Discussions

Running random forest algorithm with one variable

View More