Reputation: 560
I do understand how Stack()
and Stack<T>
works, but I really can't see any scenarios where an array, List<T>
or IEnumerable<T>
isn't a better and easier choice.
Can anyone provide me a real world example of the use of Stack<T>
?
Upvotes: 10
Views: 6045
Reputation: 917
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.
Upvotes: 0
Reputation: 19737
stacks are useful when converting an expression from infix notation to prefix notation. For example:
a + b
to (+ a b)
Upvotes: 2
Reputation: 5101
Ideally you use, or create as needed, classes that reflect how things work in the real world, the things you are modeling in code. Such classes give us a level of abstraction so we can code in terms of what we are modeling/simulating. Additionally when coding some complex thing, using a familiar paradigm helps. To wit: Oh, this Fuzzinator class uses a Stack. I know what a stack is and how it works.
Second, that higher-level-of-abstraction class gives us code that works (we assume the .NET framework was tested) and saves us the time and pain of re-inventing the wheel.
Third, the code is easier to read, easier to understand, easier to change and so on. It is more maintainable.
Using classes with more refined functionality helps limit how we might screw up in using it.
On the whole your application is simply better when it's coded at appropriate levels of abstraction.
Stack is one of these classes.
My HP-41X calculator does its arithmetic using a stack. This way of calculation is called RPN - Reverse Polish Notation.
If I were simulating a cafeteria the Stack would be perfect for that stack of plates. Plates get on and off the stack from the top. Not the middle, not the end; just the top. A Stack. I can only Push() and Pop() plates which makes the code more simple and clear.
Alternatively, imagine coding with the C# equivalent of sub-atomic particles - generic collection or generic IEnumerable, etc. I end up using general utility methods and properties with general names with multi-variable numbers of parameters which in the aggregate obscure the fact that I'm stacking plates.
Upvotes: 16
Reputation: 108830
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.
Upvotes: 0
Reputation: 8802
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...
Upvotes: 1
Reputation: 13545
Stack and Queue use internally an array. If you were using arrays in smart ways the chances are good that you have used them in a stack or queue like fashion already. Usually you need to decide between Stack and Queue. A typical example where a Stack is needed is depth first search. If you change the collection to a queue you have implemented a breadth first search.
Another example is heavy multithreading where you pass data between a producer and consumer via a stack if the processing order is not relevant. The rationale behind this is that if much data is going to be processed it is better for the CPU to use the latest added data chunk for further processing on the other thread to get better cache locality.
And the list goes on ....
Upvotes: 1
Reputation: 20157
You can rewrite recursive methods as iterative counterparts much easier by controlling the locals on and off the stack. Check out Jon Skeet's sort using the stack here.
Upvotes: 2
Reputation: 32438
Depth first tree traversal. As opposed to a queue, for breadth first tree traversal.
Managing presenting different screens to a user as they navigate around them. Showing a screen pushes it to the stack, going 'back' pops it. Draw the top screen.
When you want a collection where you can add things and always know when you get something out it's the most recently added.
Implementing Undo/Redo functionality.
Upvotes: 6
Reputation: 5160
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
Upvotes: 4
Reputation: 6090
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.
Upvotes: 0
Reputation: 17808
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!
Upvotes: 2
Reputation: 1039080
If you do expression evaluation and syntax parsing, stack could be useful data structure.
Upvotes: 2