I am create a stack demo with some functionality:
( 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 开发者_C百科) Push => Done by using lists as stacks
( Method ) Pop => Done by using lists as stacks
The code i am doing
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
Anyboday know to do this?
thanks
Python's list probably already has everything you want. If you want some additional functionality like limiting max number of objects you should subclass it or wrap around it with another class.
Check out Using lists as stacks in python.
Python has it built-in, see Using Lists as Stacks. It sounds like you might benefit from the Python Tutorial, as well.
>>> stack = []
>>> print stack
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> val = stack.pop()
>>> print stack
[1, 2, 3, 4, 5, 6, 7, 8]
>>> val
9
>>>
精彩评论