Alex Rivera | Logout

What real world uses of the "Stack" object (.Net) have you used

Asked 2010-01-13T16:05:36.740
10

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?

Edit
Report

1 Answer

7

Stacks are used whenever a stored procedure / sub-routine is called to store local variables and return address.

Stacks are used for expression evaluation (eg in a calculator, or your compiler), first the expression is converted to RPN 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.   
answered 2010-01-13T16:09:03.923

Your Answer