后进先出(LIFO)

关于栈的操作

- 创建空栈			
- isEmpty      #判断是否为空
- push				       # 压入栈
- pop				         # 弹出最后压入栈的元素
- top				          # 取出最后压入栈的元素, 不删除

下例可以设置栈的大小

#!/usr/bin/env python
class Stack:
    def __init__(self, *args):
        self.stack = []
        if args:
            self.size = args[0]
        else:
            self.size = False

    def isEmpty(self):
        print self.stack == []

    def push(self, element):
        """judge the stack is full"""
        if self.size:
            if len(self.stack) < self.size:
                self.stack.append(element)
            else:
                print 'the statck is full'
        else:
            self.stack.append(element)

    def pop(self):
        print self.stack.pop()

    def top(self):
        print self.stack[-1]
posted @ 2016-10-21 21:46  qlshine  阅读(182)  评论(0编辑  收藏  举报