Understanding Stacks and Queues
Stack (Last In, First Out)
A stack is like a stack of plates. You place plates on top, and when you need one, you take the one from the top.
- Start: []
- Push 3, 7, 5: [3, 7, 5]
- Pop: [3, 7]
- Push 9: [3, 7, 9]
- Pop: [3, 7]
Queue (First In, First Out)
A queue is like a line at a bank. The first person in line is the first person served, and new people join the line at the end.
- Start: []
- Enqueue 3, 7, 5: [3, 7, 5]
- Dequeue: [7, 5]
- Enqueue 9: [7, 5, 9]
- Dequeue: [5, 9]