Worst Case | |
---|---|
space | |
push | |
pop | |
peek |
A stack stores items in a last-in, first-out (LIFO) order.
Picture a pile of dirty plates in your sink. As you add more plates, you bury the old ones further down. When you take a plate off the top to wash it, you're taking the last plate you put in. "Last in, first out."
You can implement a stack with either a linked list or a dynamic array—they both work pretty well:
Stack Push | Stack Pop | |
---|---|---|
Linked Lists | insert at head | remove at head |
Dynamic Arrays | append | remove last element |