Python 使用单链表实现堆栈 (基于class, 包含迭代器)
2015-01-28 14:42
295 查看
#!/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 list class 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 nodes class _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 stack ECON-101 HIST-340 MATH-121 CSCI-112 #pop one item pop item: ECON-101 #output smith stack after pop HIST-340 MATH-121 CSCI-112 #get the peek item peek item is HIST-340 #get the length of stack the lenght of stack is 3 #check wheter the stack is empty stack is not empty! #pop all items pop item: HIST-340 pop item: MATH-121 pop item: CSCI-112 #check wheter the stack is empty after pop all items stack is empty!
相关文章推荐
- Python 使用单链表实现队列 (基于class, 包含迭代器)
- Python 使用list实现堆栈 (基于class, 包含迭代器)
- Python 使用list实现队列 (基于class, 包含迭代器)
- Python 使用list实现无边际优先队列 (基于class, 包含迭代器)
- Python 使用由单链表构建的数组实现有边际优先队列 (基于class, 包含迭代器)
- Python 使用循环数组实现队列 (基于class, 包含迭代器)
- 基于java.util.logging实现轻量级日志记录库(增加根据当前类class初始化,修复线程池模型(javaEE)下的堆栈轨迹顺序与当前调用方法不一致问题)
- 使用BlazeDS实现java与flex传对象时,RemoteClass中包含自己写的类,后台无法转换的问题
- Python 使用list实现简单的堆栈
- Python使用文件锁实现进程间同步功能【基于fcntl模块】
- 基于Python使用CloudSight API实现简单的图像识别(image Recognition)
- 基于java.util.logging实现轻量级日志记录库(增加根据当前类class初始化,修复线程池模型(javaEE)下的堆栈轨迹顺序与当前调用方法不一致问题)
- Python使用metaclass实现Singleton模式的方法
- 使用递归下降算法分析数学表达式 -- 基于堆栈的计算器实现算法
- 神经网络与深度学习 1.6 使用Python实现基于梯度下降算法的神经网络和MNIST数据集的手写数字分类程序
- 基于Python使用scrapy-redis框架实现分布式爬虫 注
- python数据结构学习笔记-2016-10-27-02-使用单链表实现包ADT
- Python 使用单链表实现多项式 (Polynomial)
- (数据结构与算法分析 二)------单链表的实现,使用链表迭代器
- Python 使用单链表实现简单的稀疏矩阵