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
- Python 使用单链表实现堆栈 (基于class, 包含迭代器)
- Python 使用list实现堆栈 (基于class, 包含迭代器)
- Python 使用单链表实现队列 (基于class, 包含迭代器)
- Python 使用list实现队列 (基于class, 包含迭代器)
- Python 使用循环数组实现队列 (基于class, 包含迭代器)
- Python 使用由单链表构建的数组实现有边际优先队列 (基于class, 包含迭代器)
- Python 使用list实现无边际优先队列 (基于class, 包含迭代器)
- Python 使用list实现简单的堆栈
- python堆栈实现
- 基于链表实现堆栈
- 基于数组的堆栈实现
- 基于堆栈的虚拟机实现
- 使用数组实现堆栈
- python实现堆栈和队列
- python实现堆栈和队列
- python 实现Graph class
- 基于堆栈的计算器实现算法
- 基于堆栈的计算器实现算法
- VC 使用ADO连接 DB2数据库
- VTK在VS2010中的配置
- js控制按钮,防止频繁点击响应
- it web
- Coderforces 508B 简单贪心
- Python 使用单链表实现堆栈 (基于class, 包含迭代器)
- 用AutoCompleteTextView实现历史记录提示
- Oracle11g基础知识2【SQL学习笔记】
- Codeforces Round #288 (Div. 2) B. Anton and currency you all know
- cocos2dx HelloWorld代码分析
- Android Fragment基础篇
- SSH+JBPM+ExtJs项目实战视频教程
- Leetcode -- Search Insert Position
- 输入两个数m,n,输出m个数,每个数都在[0,n)之间。