定义一个栈(Stack)类,用于模拟一种具有后进先出(LIFO)特性的数据结构

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Einstellung/article/details/82111230
class Stack:
    def __init__(self, stack):
        self.stack = []
        for x in stack:
            self.push(x)


    def isEmpty(self):
        return not self.stack

    def push(self, obj):
        self.stack.append(obj)

    def pop(self):
        if not self.stack:
            print("当前栈为空")

        else:
            self.stack.pop()

    def top(self):
        if not self.stack:
            print("当前栈为空")

        else:
            print(self.stack[-1])

    def bottom(self):
        if not self.stack:
            print("当前栈为空")

        else:
            print(self.stack[0])

猜你喜欢

转载自blog.csdn.net/Einstellung/article/details/82111230
今日推荐