All pastes #1608561 Raw Edit

LLQueue

public python v1 · immutable
#1608561 ·published 2009-10-10 00:29 UTC
rendered paste body
import nodeclass LLQueue(object):        """An implementation of the Queue ADT using a linked list."""        def __init__(self):        """Create a new, empty LLQueue."""        #Empty LLQueue is created with a head & tail as None & it's size as 0.        self._head = None        self._tail = None        self._size = 0            def enqueue(self, item):        """Add item to the back of the LLQueue."""                      #Create a new node with the given item.               temp_node = node.Node(item)           if self._head == None:        #If there's no head then the new node is assigned to the head & tail.            self._head = temp_node            self._tail = temp_node        else:        #Otherwise the new node is linked to the current tail. Then the        #tail is reassigned as the new node.            self._tail.next = temp_node            self._tail = temp_node                    #Size is increased by 1.        self._size += 1            def dequeue(self):        """Remove and return the item at the front of the LLQueue."""        #If size is not 0, the data in the head of the queue is returned and         #the head is reassigned as the node linked to the old head. Size is         #decreased by 1.        if self._size != 0:            first_item = self._head            self._head = self._head.next            self._size -= 1            return first_item.data            def front(self):        """Return the item at the front of the LLQueue without removing it."""        #Return the data from the head if size is not 0.        if self._size != 0:            return self._head.data            def empty(self):        """Return whether the LLQueue is empty."""        #Return True if the head is None. Otherwise return False.        return self._head == None        def size(self):        """Return the number of items currently in the LLQueue."""        #Return the size of the queue.         return self._size