Stack and Queue

1 min read
On trails:CSDATAstep 4/6

Every operation on both is O(1). See the Big O noteBig O notationHow run time or memory grows as the input grows.Opens in a new tab for the notation.

Stack (System.Collections.Generic.Stack<T>)

A Last-In, First-Out (LIFO) collection.
  • Push (add): O(1)
  • Pop (remove): O(1)
  • Peek (access the top): O(1)

Use it for managing function call stacks, undo and redo, and parsing expressions, for example reversing order or matching parentheses.

Queue (System.Collections.Generic.Queue<T>)

A First-In, First-Out (FIFO) collection.
  • Enqueue (add): O(1)
  • Dequeue (remove): O(1)
  • Peek (access the front): O(1)

Use it for processing tasks in order, for example print queues and message queues, and for implementing breadth-first search algorithms.


Sources