Mark As Completed Discussion

Stack

Stack
  • Quick summary: a sequential collection where elements are added to and removed from the same end.
  • Important facts:
    • First-in, last-out (FILO) data structure.
    • Equivalent of a real-life pile of papers on desk.
    • In stack terms, to insert is to push, and to remove is to pop.
    • Often implemented on top of a linked list where the head is used for both insertion and removal. Can also be implemented using dynamic arrays.
  • Pros:
    • Fast insertions and deletions: O(1).
  • Cons:
    • Access and search are O(n).
  • Notable uses:
    • Maintaining undo history.
    • Tracking execution of program functions via a call stack.
    • Reversing order of items.
  • Time complexity (worst case):
    • Access: O(n)
    • Search: O(n)
    • Insertion (pushing): O(1)
    • Deletion (popping): O(1)
  • See also:
JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment