开发者

Implementing stack in python by using lists as stacks [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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
>>> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜