我正在创建一个具有以下功能的堆栈演示:
( Method ) __init__(self, n) => How can I initial the fix n len of stack. ?
( Method ) IsEmpty => Done by using lists as stacks
( Method ) IsFull => Done by using lists as stacks
( Method ) Push => Done by using lists as stacks
( Method ) Pop => Done by using lists as stacks
我正在做的代码
class Stack(object) :
def __init__(self) :
self.items = []
def push(self, item) :
self.items.append(item)
def pop(self) :
return self.items.pop()
def isEmpty(self) :
return (self.items == [])
if __name__ == "__main__":
demoStack = Stack()
demoStack.push(1)
print demoStack.items
有人知道怎么做吗?
谢谢
在python中查看Using lists as stacks。
Python内置了它,请参见Using Lists as Stacks。听起来你可能也会从Python Tutorial中受益。
Python的列表可能已经有了您想要的一切。如果您想要一些额外的功能,比如限制对象的最大数量,您应该将其子类化,或者用另一个类将其包装起来。
相关问题 更多 >
编程相关推荐