Python 使用list实现堆栈 (基于class, 包含迭代器)
2015-01-27 16:18
721 查看
#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-1-27 @author: beyondzhou @name: test_liststack.py ''' def test_liststack(): # import listStack from mystack import listStack print '#Init a stack named smith using push' smith = listStack() 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_liststack()
# Implementation of iter class _StackIterator: def __init__(self, theList): self._setItems = theList self._curItem = 0 def __iter__(self): return self def next(self): if self._curItem < len(self._setItems): item = self._setItems[self._curItem] self._curItem += 1 return item else: raise StopIteration # Implementation of the Stack ADT using a Python list class listStack: # Created an empty stack def __init__(self): self._theItems = list() # Returns True if the stack is empty or False otherwise def isEmpty(self): return len(self) == 0 # Returns the number of items in the stack def __len__(self): return len(self._theItems) # 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._theItems[-1] # Removes and returns the top item on the stack def pop(self): assert not self.isEmpty(), "Cannot peek at an empty stack" return self._theItems.pop() # Push an item onto the top of the stack def push(self, item): self._theItems.append(item) # Returns an iterator for traversing the list of items def __iter__(self): return _StackIterator(self._theItems)
#Init a stack named smith using push #output smith stack CSCI-112 MATH-121 HIST-340 ECON-101 #pop one item #output smith stack after pop CSCI-112 MATH-121 HIST-340 #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 #check wheter the stack is empty after pop all items stack is empty!
相关文章推荐
- Python 使用list实现队列 (基于class, 包含迭代器)
- Python 使用单链表实现堆栈 (基于class, 包含迭代器)
- Python 使用list实现无边际优先队列 (基于class, 包含迭代器)
- Python 使用由单链表构建的数组实现有边际优先队列 (基于class, 包含迭代器)
- Python 使用循环数组实现队列 (基于class, 包含迭代器)
- Python 使用单链表实现队列 (基于class, 包含迭代器)
- Python 使用list实现简单的堆栈
- Python基于list的append和pop方法实现堆栈与队列功能示例
- .NET数据库编程求索之路--6.使用ADO.NET实现(三层架构篇-使用List传递数据-基于存储过程)(2)
- List的All方法使用问题:我用List1的All方法来确保List1的所有每个数组元素中是否包含Arr1中的任意元素,这个部分很难实现。 因为,没有一个数组中是否包含另一个数组中的任意元素这个功能
- 使用模板类实现List容器&迭代器
- Python使用metaclass实现Singleton模式的方法
- .NET数据库编程求索之路--6.使用ADO.NET实现(三层架构篇-使用List传递数据-基于存储过程)(1)
- Java基础课程学习总结,使用LinkedList简单模拟队列数据结构和堆栈数据结构的实现
- Python中使用copy模块实现列表(list)拷贝
- Python使用metaclass实现Singleton模式的方法
- Python:数组、队列及堆栈的使用(list用法)--转
- python使用迭代器实现关键字检索
- python list 遍历使用enumerate,包含数字和ele
- Python 使用list实现简单的map