Esempio n. 1
0
class ListStack(object):
    '''
    Pushdown Stack(linked-list implmentation).
    -------------
      Stack(): init queue
      push(item): push an item
      pop(): remove the most recently added item
      top(): get the most recently added item
      empty = isEmpty(): tell stack is empty
      stackSize = size(): get size of stack 
      clear(): reset the stack
    
    '''
    def __init__(self):
        self.list = LinkedList()

    def push(self, item):
        self.list.addHead(item)

    def isEmpty(self):
        return self.list.isEmpty()

    def pop(self):
        if self.isEmpty():
            return
        self.list.remove(0)

    def top(self):
        if self.isEmpty():
            return None
        return self.list.get(0).item

    def size(self):
        return self.list.size()

    def clear(self):
        self.list = LinkedList()