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

#!/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!

时间: 2024-11-09 05:11:23

Python 使用list实现堆栈 (基于class, 包含迭代器)的相关文章

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

#!/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' sm

Python 使用list实现队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-1-27 @author: beyondzhou @name: test_listqueue.py ''' def test_listqueue(): # import pyListQueue from myqueue import pyListQueue print '#Init a queue named smith using enqueue' smith = pyL

Python 使用list实现无边际优先队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-4 @author: beyondzhou @name: test_listpriorityqueue.py ''' def test_listpriorityqueue(): # import pyListQueue from myqueue import ListPriorityQueue print '#Init a queue named smith using

Python 使用循环数组实现队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-2 @author: beyondzhou @name: test_arryqueue.py ''' def test_arryqueue(): # import pyListQueue from myqueue import arrayQueue print '#Init a queue named smith using enqueue' smith = array

Python 使用由单链表构建的数组实现有边际优先队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-6 @author: beyondzhou @name: test_bpriorityqueue.py ''' def test_bpriorityqueue(): # import pyListQueue from myqueue import BPriorityQueue print '#Init a queue named smith using enqueue'

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

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-3 @author: beyondzhou @name: test_linklistqueue.py ''' def test_linklistqueue(): # import pyListQueue from myqueue import linkListQueue print '#Init a queue named smith using enqueue' sm

python整合连续数字的练习,包含itertools\groupby用法

#汉字数字转阿拉伯数字 1 class ConvertNum: 2 def __init__(self,cnNum): 3 self.dict = {u'零':0,u'一':1,u'二':2,u'三':3,u'四':4,u'五':5,u'六':6,u'七':7,u'八':8,u'九':9,u'十':10,u'百':100,u'千':1000,u'万':10000} 4 self.cnNum = cnNum 5 6 def convert(self): 7 count = 0 8 result =

Python学习之三大名器-装饰器、迭代器、生成器

Python学习之三大名器-装饰器.迭代器.生成器 一.装饰器     装饰,顾名思义就是在原来的基础上进行美化及完善,器这里指函数,所以说装饰器就是装饰函数,也就是在不改变原来函数的代码及调用方式的前提下对原函数进行功能上的完善.其核心原理其实是利用闭包.     格式 @关键字+装饰函数          被装饰函数()      注意:@行必须顶头写而且是在被装饰函数的正上方     按照形式可以分为:无参装饰器和有参装饰器,有参装饰器即给装饰器加上参数     以下示例是一个无参装饰器,

Python 字典items返回列表,iteritems返回迭代器

Python 字典items返回列表,iteritems返回迭代器 字典items()方法和iteritems()方法,是python字典的内建函数,分别会返回Python列表和迭代器,下面一起来看下字典items()和iteritems()的具体操作方法. 作用 python字典的items方法作用:是可以将字典中的所有项,以列表方式返回.如果对字典项的概念不理解,可以查看Python映射类型字典基础知识一文.因为字典是无序的,所以用items方法返回字典的所有项,也是没有顺序的.python