I do understand how Stack()
and Stack
works, but I really can\'t see any scenarios where an array, List
or IEnumer
I'd say if you were modeling something that is conceptually a stack. Say you're modeling a small, but deep drawer and you're putting books in the drawer. This will follow a "first in, last out" paradigm, which is the point of a stack in general.
You don't have a list of books or some kind of enumeration - you have a specific ordering of them... namely, the opposite of the order in which they were added.
Stack<T>
's functionality really seems like a subset of List<T>
(with a few renamed methods), so I agree it doesn't seem like the most useful collection by itself. When used internally in an algorithm, List<T>
can easily substitute for it, even if that may be slightly less idiomatic.
Enforcing stack behavior is only necessary if it is exposed publicly. But it that case it's usually a better idea to expose some kind of wrapper over the internal collection, so it doesn't really seem very useful for that either.I'd certainly see use for a IStack<T>
interface, but not so much for a plain collection class Stack<T>
.
My conclusion is that I wouldn't have included a Stack<T>
class in the framework, just a IStack<T>
interface. The BCL collections generally don't look very well thought out to me.
ConcurrentStack<T>
on the other hand seems much more useful.
Depth First Search (DFS) is a good real-world example of the use of a stack.
http://www.cs.toronto.edu/~heap/270F02/node36.html
Heres one way I've used Stack:
I have a wizard like page where there are 5 user controls that need to be dispalyed and processed in a specific sequence, and the user should be able at any point to return to the last page in order.
I use a stack based state machine to track the users progress through the wizard.
Each time they move forward, I push the state of the state machine into a session stored stack, and if they request to go back, I pop off the top value and set the machine to the new top stack value. (Which in turn, displays and loads the proper control)
One more:
I had to build an install application for some very custom server applications. What I ended up doing was breaking each step of the install into its own generic component, ie, a component to copy a file, a component to write a registry value, ect. Each compontent had to methods: Do the install action, Undo the install action.
Each step of the install, the component is pushed into a stack.
Now, at any time I have an error, we just pop each component back off the stack, and run the undo action giving us fine grained install transactions.
Stack's are your friend!
Stacks are frequently used in text parsing algorithms, such as the evaluation of "4+5+6". For a real-world example of an application that uses stacks to parse text, see HTMLAgilityPack. This component is used to parse HTML, and it includes the source code, so you can see how and where stacks are used...
Keeping track of Browser History is a use for the stack. Also, "undo" for edits, etc. Similarly, some warehouse control systems use stacks and queues for managing the order in which items need to be selected for shipments.