Implementing a Queue in Python Using Lists


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




Proud UCSCian | Proud Devan | Computer Geek | Superman | Love Chess & Programming

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.