Stacks and Queues Slides
Stacks and Queues Slides
Robert Horvick
SOFTWARE ENGINEER
@bubbafat www.roberthorvick.com
Stacks
- Last-in, First-out data structure
Overview Queues
- First-in, First-out data structure
Deque
- Double-ended queue
EMPTY
Additional plates can
be added to the stack
We can only access
the plate at the top of
the stack
The stack of plates is
now 4 deep
There is a top plate
and a bottom plate
top
bottom
Plates come off the
top of the stack
top
bottom
We “pop” items off
the stack
top
bottom
A single item is the
top and bottom
bottom top
Popping the last
item empties the
stack
EMPTY
Queue
A First-in, First-out (FIFO) container.
”Queuing” in a line
Enqueue at end of
line
First is “head”
Last is “tail”
tail head
The head of the line is
“dequeued”
Next is now “head”
tail head
A new person waits
at the end of the line
tail head
The queue gets
shorter
The queue gets
shorter
The queue gets
shorter
The queue gets
shorter
Until it is empty
Doubly Ended Queue (deque)
A queue-like container which is both First-in, First-out
and Last-in, Last-out.
Doubly Ended Queue
1 2 3 4
Remove “5” from the head
Tail Head
Doubly Ended Queue