Question: We have all read about or heard about the stack class, but many of us have probably never found a reason to use the LIFO object. I am curious to hear of real world solutions that used this object and why. 

<http://msdn.microsoft.com/en-us/library/system.collections.stack.aspx>

I recently saw an example where a programmer used a stack to keep track of his current position while traversing a hierarchical data source. As he moved down the hierarchy, he pushed his position identifier on to the stack and as he moved back up he popped items off the stack. I thought this was a very efficent way to keep track of his current position in a mamoth hierarchy. I had never seen this before.

Anyone else have any examples?

Answer: 
[Stacks are used whenever a stored procedure / sub-routine is called to store local variables and return address.](http://en.wikipedia.org/wiki/Call_stack)

Stacks are used for expression evaluation (eg in a calculator, or your compiler), first the expression is converted to [RPN](http://en.wikipedia.org/wiki/Reverse_Polish_notation) then a simple stack machine is used to evaluate. This works as follows, when you see an operand push it on the stack. When you see an operator pop operands and evaluate.

```
example

5 6 + 3 *

steps-
 see 5 push 5
 see 6 push 6
 see + pop 2 times and apply + get 11 push 11
 see 3 push 3
 see * pop 2 times and apply get 33 push 33

result is on the top of the stack.   

```
To provide a specific example to illuminate what other people are commenting on: to implement a Z-machine interpreter three different stacks should be used. A call stack, and a couple different kinds of object stacks. (The specific requirements can be found [here.](http://www.gnelson.demon.co.uk/zspec/sect06.html)) Note that, like all of these examples, while using a stack isn't strictly *required*, it is the obvious choice.

The call stack keeps track of recursive calls to subroutines, while the object stack is used to keep track of internal items.