All pastes #1608563 Raw Edit

LLStack

public python v1 · immutable
#1608563 ·published 2009-10-10 00:31 UTC
rendered paste body
import nodeclass LLStack(object):        """An implementation of the Stack ADT using a linked list."""        def __init__(self):        """Create a new, empty LLStack."""        #Empty LLStack is created with head as None & size as 0.        self._head = None        self._size = 0            def push(self, item):        """Put item on top of the LLStack."""        #The new node is created and the current head is linked to it.        #The head is reassigned as the new node & size is increased by 1.        temp = node.Node(item)        temp.next = self._head        self._head = temp        self._size += 1               def pop(self):        """Return and remove the item on top of the LLStack."""        #If the size is not 0, the current head is stored in a temp variable         #and the head is reassigned as the node linked to the old head. Size is        #decreased by 1. The data from the temp variable is returned.        if self._size != 0:            temp_node = self._head            aelf._head = self._head.next            self._size -= 1            return temp_node.data                def peek(self):        """Return the item on top of the LLStack without removing it."""        #Data of the head is returned if size is not 0.        if self._size != 0:            return self._head.data                def empty(self):        """Whether or not the LLStack is empty."""        #Returns True when the head is None. Otherwise is returns False.        return self._head == None        def size(self):        """Return the number of items in the LLStack."""        #Returns size.        return self._size