How do you set up a stack?

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed May 9 04:31:07 EDT 2001


"Nick Perkins" <nperkins7 at home.com> wrote in 
<N65K6.97828$HF.22450728 at news4.rdc1.on.home.com>:

> any list can be used as a stack:
> 
<snip>
> 
> ( note that 'append' is used as the 'push' function )
> we just pretend that the list is a stack, with the
> 'top' of the stack at the end of the list.
> 

If you prefer to have a type called a Stack you can always do this:
>>> class Stack:
...     def __init__(self):
...         self.stack = []
...         self.push = self.stack.append
...         self.pop = self.stack.pop
...
>>> s = Stack()
>>> s.push(1)
>>> s.push(2)
>>> s.pop()
2
>>> s.pop()
1


-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list