Python 使用list實現堆棧 (基於class, 包含迭代器),pythonlist

來源:互聯網
上載者:User

Python 使用list實現堆棧 (基於class, 包含迭代器),pythonlist

#!/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 iterclass _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 listclass 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 stackCSCI-112MATH-121HIST-340ECON-101#pop one item#output smith stack after popCSCI-112MATH-121HIST-340#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 items#check wheter the stack is empty after pop all itemsstack is empty!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.