Stacks and Queues
Stacks and Queues
Stacks and Queues
1 / 18
Stack Application
Containers
2 / 18
Stack Application
Containers
3 / 18
Stack Application Abstract Stack
An ordered collection of items where the addition of new items and the
removal of existing items always takes place at the same end.
This end is commonly referred to as the "top."
The end opposite the top is known as the "base."
Sometimes called a "push-down stack".
4 / 18
Stack Application Abstract Stack
5 / 18
Stack Application Abstract Stack
6 / 18
Stack Application Abstract Stack
Examples
batch job processing - executed when resources permit
passengers entering a subway
food placed in refrigerator
recursive functions
7 / 18
Stack Application Abstract Stack
Operations.
Push. adds a new item to the top of the stack.
Pop. removes the top item from the stack.
Misc. operations.
Peek. returns the top item from the stack but does not remove it.
IsEmpty. tests to see whether the stack is empty.
IsFull. tests to see whether the stack is full.
Size. returns the number of items on the stack.
Two exceptions.
Stack overflow exception
Stack underflow exception
8 / 18
Stack Application Abstract Stack
Given the following sequence of stack operations, what is the top item
on the stack when the sequence is complete?
m := Stack()
m.push(’x’)
m.push(’y’)
m.pop()
m.push(’z’)
m.peek()
(a) ’x’
(b) ’y’
(c) ’z’
(d) The stack is empty
9 / 18
Stack Application Abstract Stack
(a) ’x’
(b) the stack is empty
(c) an error will occur
(d) ’z’
10 / 18
Stack Application Abstract Stack
11 / 18
Stack Application Queues
Queues: ordering
12 / 18
Stack Application Queues
Queues: ordering
13 / 18
Stack Application Queues
Queues: ordering
14 / 18
Stack Application Queues
Queues: operations
Operations.
Enqueue
Dequeue
15 / 18
Stack Application Queues
Queues: exceptions
Exceptions.
Queue overflow exception. adding an element to a full queue
Queue underflow exception. removing an element from an empty
queue
16 / 18
Stack Application Queues
Queues: applications
Applications.
playlist
shared resources like printer
grocery stores, banks, and airport security
customer waiting times in call centers
traffic analysis
17 / 18
Stack Application Queues
Queues: applications
18 / 18