#!/usr/bin/python #-*-Coding:utf-8-*-"Created on 2015-1-28@author:beyondzhou@name:test_linkliststack.py" def Test_linkliststack (): # import Linkliststack from mystack import linkliststack print ' #Init a stack named Smith using push ' Smith = Linkliststack () smith.push (' CSCI-112 ') smith.push (' MATH-121 ') smith.push (' HIST-340 ') ) Smith.push (' ECON-101 ') print ' \n#output Smith stack ' for element in Smith:print element print ' \n#pop one item ' Smith.pop () print ' \n#output Smith stack after pops ' for element in Smith:p rint element print ' \n#get the peek item ' Peek_item = Smith.peek () print ' Peek item is ', Peek_item print ' \n#get the length of stack ' print ' The lenght of Stack is ', Len (Smith) print ' \n#check wheter the STA CK is empty ' if Smith.isempty (): print ' stack is empty! ' Else:print ' stack is not empty! ' print ' \n#pop all items 'While not Smith.isempty (): Smith.pop () print ' \n#check wheter the stack was empty after POPs all items ' if Smith.isempty (): print ' stack is empty! ' Else:print ' stack is not empty! ' if __name__ = = "__main__": Test_linkliststack ()
# Implementation of the Stack ADT using a singly linked ListClass Linkliststack: # Creates an empty Stack def __init __ (self): Self._top = None self._size = 0 # Returns True If the stack is empty or False otherwise def IsEmpty (self): return self._top was None # Returns the number of items in the stack def __len__ (self): Return Self._size # Returns the top item on the stack without removing it def peek (self): Assert not Self.ise Mpty (), "cannot peek at a empty stack" return Self._top.item # Removes and returns the top item on the stack def pop (self): Assert not Self.isempty (), "cannot pops from an empty stack" node = self._top self._to p = Self._top.next Self._size-= 1 print ' Pop item: ', Node.item return Node.item # pushes an item Onto the top of the stack def push (self, item): Self._top = _stacknode (item, self._top) Self._size + = 1 def __iter__ (sELF): Return _linkstackiterator (self._top) # The private storage class for creating stack Nodesclass _stacknode: def __init__ (self, item, link): Self.item = Item Self.next = link # Implementation of linked list STA CK ITER class _linkstackiterator:def __init__ (Self, listhead): Self._curnode = Listhead def __iter_ _ (Self): return self def Next: If Self._curnode is None:raise stopiteration Else:item = Self._curnode.item Self._curnode = self._curnode.next return I Tem
#Init a stack named Smith using Push#output Smith stackecon-101hist-340math-121csci-112#pop one Itempop item:econ-101#out Put Smith stack after pophist-340math-121csci-112#get the Peek Itempeek item is hist-340#get the length of the stackthe le Nght of Stack is 3#check wheter the stack is emptystack are not empty! #pop all Itemspop item:hist-340pop item:math-12 1pop Item:csci-112#check wheter The stack is empty after pops all Itemsstack is empty!
Python uses a single-linked list to implement the Stack (class-based, including iterators)