Python 使用单链表实现堆栈 (基于class, 包含迭代器)

来源:互联网 发布:mil是哪里的域名 编辑:程序博客网 时间:2024/05/17 01:17
#!/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 pop'    for element in smith:        print 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 stack 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 is empty after pop 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 is 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.isEmpty(), "Cannot peek at an empty stack"        return self._top.item    # Removes and returns the top item on the stack    def pop(self):        assert not self.isEmpty(), "Cannot pop from an empty stack"        node = self._top        self._top = 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 stack iter  class _LinkStackIterator:      def __init__(self, listHead):          self._curNode = listHead        def __iter__(self):          return self        def next(self):          if self._curNode is None:              raise StopIteration          else:              item = self._curNode.item              self._curNode = self._curNode.next              return item  

#Init a stack named smith using push#output smith stackECON-101HIST-340MATH-121CSCI-112#pop one itempop item: ECON-101#output smith stack after popHIST-340MATH-121CSCI-112#get the peek itempeek item is  HIST-340#get the length of stackthe lenght of stack is  3#check wheter the stack is emptystack is not empty!#pop all itemspop item: HIST-340pop item: MATH-121pop item: CSCI-112#check wheter the stack is empty after pop all itemsstack is empty!

0 0
原创粉丝点击