When to use the Stack collection in C#?

后端 未结 12 2288
滥情空心
滥情空心 2021-02-14 20:44

I do understand how Stack() and Stack works, but I really can\'t see any scenarios where an array, List or IEnumer

相关标签:
12条回答
  • If you do expression evaluation and syntax parsing, stack could be useful data structure.

    0 讨论(0)
  • 2021-02-14 20:49

    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 ....

    0 讨论(0)
  • 2021-02-14 20:51

    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.

    0 讨论(0)
  • 2021-02-14 20:53

    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.

    0 讨论(0)
  • 2021-02-14 20:55

    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.

    0 讨论(0)
  • 2021-02-14 20:56

    stacks are useful when converting an expression from infix notation to prefix notation. For example:

    a + b to (+ a b)

    0 讨论(0)
提交回复
热议问题