[Tutor] Can someone please explain a Stack Trace
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Thu, 4 Oct 2001 00:19:15 -0700 (PDT)
On Wed, 3 Oct 2001, Sean 'Shaleh' Perry wrote:
> A "stack" is a LIFO (last in, first out) data structure. As such, it
> "pushes" items down and returns and removes the topmost when "popped".
It's actually very easy to play around with something "stacklike": Python
lists can be treated as stacks, if we slightly rename some things:
###
>>> def makeStack():
... return []
...
>>> def push(stack, element):
... """Push an element onto the stack."""
... stack.append(element)
...
>>> def pop(stack):
... """Pop off the first element of the stack."""
... return stack.pop()
...
>>> s = makeStack()
>>> push(s, 1)
>>> push(s, 2)
>>> push(s, 'three')
>>> pop(s)
'three'
>>> pop(s)
2
>>> pop(s)
1
###
> The best real world example I have seen explained uses dinner plates.
> You start with a plate. The next night you decide not to do dishes
> and place that plate on the previous one. This continues until it is
> time to do dishes. You then remove the plates in reverse order (as
> Ignacio says, Last In, First out).
The book "Godel Escher Bach", by Douglas Hofstadler, has a pretty neat
example of stacks in his "Little Harmonic Labyrinth" and Chapter 5
(Recursive Structures and Processes". Very fun reading, especially for
budding programmers. *grin*