When to use the Stack <T> collection in C #?
I understand how Stack()
and Stack<T>
, but I really don't see any scenarios where the List<T>
or IEnumerable<T>
array is not the best and easiest choice.
Can someone provide me with a real example of using Stack<T>
?
Ideally, you use or create classes as needed that reflect how everything works in the real world, what you model in code. Such classes give us a level of abstraction, so we can code in terms of what we model / simulate. Also, when coding for some complex thing, using a familiar paradigm helps. For wit: Oh, this Fuzzinator class uses a stack. I know what a stack is and how it works.
Secondly, this class of a higher level of abstraction gives us code that works (we assume that the .NET platform has been tested) and saves time and pain when creating the wheel again.
Third, code is easier to read, easier to understand, easier to change, and so on. It is more convenient.
Using classes with more advanced functionality helps limit the use of this function.
In general, your application is simply better when it is encoded at the appropriate levels of abstraction.
The stack is one of these classes.
My HP-41X calculator does its stack arithmetic. This calculation method is called RPN - reverse Polish notation.
If I imitated a dining room, then the Stack would be ideal for this set of plates. Slabs enter and exit the stack from above. Not the middle, not the end; just on top. Stack. I can only Push () and Pop (), which makes the code simpler and more understandable.
Alternatively, imagine coding with the C # equivalent for subatomic particles - a common collection or a common IEnumerable, etc. I end up using the general methods and properties of the utility with common names with a lot of variable parameter values, which collectively hide the fact that I am laying the plates.
Depth of the first tree walk . Unlike the queue, to bypass the width of the first tree.
Controlling the presentation of various screens to the user as they move through them. Showing the screen, he pushes it onto the stack, and it goes back. Draw the top screen.
If you need a collection in which you can add things and always know when you get something from it, the latter is added.
Implementing the functionality of Undo / Redo.
If you are evaluating an expression and parsing, the stack can be a useful data structure.
This is how I used Stack:
I have a wizard page in which there are 5 user controls that need to be processed and processed in a certain sequence, and the user should be able at any time to return to the last page in order.
I use a stack-based state machine to track user progress through the wizard.
Each time they move forward, I push the state of the state machine into the stack stored in the session, and if they ask to return, I print the top value and set the machine to a new value of the top stack. (Which, in turn, displays and loads the correct control)
One more:
I had to create an installation application for some very custom server applications. What I have finished is to break each installation step into its own common component, i.e. A component for copying a file, a component for writing registry values, etc. Each component had to use methods: perform the installation action, cancel the installation action.
Each installation step, the component is pushed onto the stack.
Now that I have an error, we simply pop each component from the stack and trigger the undo action, providing us with fine-grained installation transactions.
The stack is your friend!
The stack and queue use an internal array. If you used arrays in sensible ways, then there is a good chance that you have already used them on the stack or queue. Usually you need to choose between Stack and Queue. A typical example where Stack is required is the depth of the first search . If you change the collection to a queue, you performed the first width search.
Another example is heavy multithreading, in which you pass data between producer and consumer through the stack if the processing order does not matter. The rationale for this is that if a lot of data will be processed, it is better for the processor to use the last added piece of data for further processing in another thread in order to get a better cache locality.
And the list goes on ....
Stacks are often used in text parsing algorithms, such as a rating of "4 + 5 + 6". For an example of a real application that uses text analysis stacks, see HTMLAgilityPack . This component is used for HTML analysis and includes source code, so you can see how and where stacks are used ...
stacks are useful when converting an expression from infix notation to prefix notation. For example:
a + b
to (+ ab)
I would say if you were to model something conceptually on the stack. Let's say you model a small but deep drawer, and you put books in a drawer. This will follow the “first in last” paradigm, which is the point of the stack as a whole.
You do not have a list of books or any enumeration - you have a specific order of them ... namely, the opposite of the order in which they were added.
Stack<T>
functionality really seems to be a subset of List<T>
(with a few renamed methods), so I agree that it alone does not seem to be the most useful collection. When used inside an algorithm, List<T>
can easily replace it, even if it can be a little less idiomatic.
Forced stack behavior is only necessary if it is open. But in this case, it is usually better to expose some kind of shell over the internal collection, so this is actually not very useful for this. I definitely see the use for the IStack<T>
interface, but not so much for the simple Stack<T>
collection class.
My conclusion is that I would not include the Stack<T>
class in the infrastructure, but only the IStack<T>
interface. BCL collections usually don't look very well designed.
ConcurrentStack<T>
, on the other hand, seems much more useful.
Tracking browser history is the use of the stack. In addition, “cancel” for editing, etc. Similarly, some warehouse management systems use stacks and queues to control the order in which items should be selected for delivery.