Site icon KSoftLabs

Implementing a Queue in Python Using Lists

Python Tutorial by KSoftLabs.com

 

class Queue:
    def __init__(self):
        self.items = [] #create a new lsit with no items to use as the queue

    def __repr__(self):
        return repr(self.items) #Define what to retun when queue name called

    def enqueue(self,item):
        self.items.append(item) # Add item to the back of list. 

    def dequeue(self):
        return self.items.pop(0)# Remove and return the item from front

    def isEmpty(self):
        return self.items==[] #check whether the stack is empty

    def size(self):
        return len(self.items) #return the size (length) of list

    def top(self):
        return self.items[0] #return front item without removing

 

 

Exit mobile version