Stacks and Queues Sections 3.6 and 3.7 Abstract Data Types, Vectors

advertisement
Chapter 3 Lists, Stacks, and Queues
Abstract Data Types, Vectors
Sections 3.6 and 3.7
Stacks and Queues
1
Stack ADT - LIFO
• Collections:
– Elements of some proper type T
• Operations:
–
–
–
–
–
–
–
Feature: Last In, First Out
void push(T t)
void pop()
T top()
bool empty()
unsigned int size()
constructor and destructor
2
Stack Model—LIFO
• Empty stack S
– S.empty() is true
– S.top() not defined
– S.size() == 0
food chain stack
3
Stack Model—LIFO
• S.push(“mosquito”)
– S.empty() is false
– S.top() == “mosquito”
– S.size() == 1
mosquito
food chain stack
4
Stack Model—LIFO
• S.push(“fish”)
– S.empty() is false
– S.top() == “fish”
– S.size() == 2
fish
mosquito
food chain stack
5
Stack Model—LIFO
• S.push(“raccoon”)
– S.empty() is false
– S.top() == “raccoon”
– S.size() == 3
raccoon
fish
mosquito
food chain stack
6
Stack Model—LIFO
• S.pop()
– S.empty() is false
– S.top() == “fish”
– S.size() == 2
fish
mosquito
food chain stack
7
Implementations and Uses of Stack ADT
• Implementations
– Any list implementation
– list and vector C++ STL
– Vector/List ADTs
• push_back()/pop_back()
• push_front()/?
• Uses
–
–
–
–
–
Depth first search / backtracking
Evaluating postfix expressions
Converting infix to postfix
Function calls (runtime stack)
Recursion
8
Depth First Search—Backtracking
•
•
Problem
– Discover a path from start to
goal
Solution
start 1
– Start from
• Node start
– Stop
2
3
4
6
7
8
9
10
• If node is goal
– Go deep
• If there is an unvisited neighbor,
go there
5
– Backtrack
• Retreat along the path to find an
unvisited neighbor, if cannot go
deeper
•
Outcome
– If there is a path from start to
goal, DFS finds one such
path
11
12
goal
9
Depth First Search—Backtracking (2)
• Stack
start 1
5
2
3
4
6
7
8
9
10
11
12
goal
Push
1
10
Depth First Search—Backtracking (3)
• Stack
start 1
5
Push
2
Push
1
2
3
4
6
7
8
9
10
11
12
goal
11
Depth First Search—Backtracking (4)
• Stack
start 1
5
Push
5
Push
2
Push
1
2
3
4
6
7
8
9
10
11
12
goal
12
Depth First Search—Backtracking (5)
• Stack
start 1
Push
6
Push
5
Push
2
Push
1
5
2
3
4
6
7
8
9
10
11
12
goal
13
Depth First Search—Backtracking (6)
• Stack
start 1
Push
9
Push
6
Push
5
Push
2
Push
1
5
2
3
4
6
7
8
9
10
11
12
goal
14
Depth First Search—Backtracking (7)
• Stack
start 1
Pop
Push
6
Push
5
Push
2
Push
1
5
2
3
4
6
7
8
9
10
11
12
goal
15
Depth First Search—Backtracking (8)
• Stack
start 1
2
3
4
6
7
8
9
10
Pop
5
Push
5
Push
2
Push
1
11
12
goal
16
Depth First Search—Backtracking (9)
• Stack
start 1
5
2
3
4
6
7
8
9
10
Pop
Push
2
Push
1
11
12
goal
17
Depth First Search—Backtracking (10)
• Stack
start 1
5
Pop
Push
1
2
3
4
6
7
8
9
10
11
12
goal
18
Depth First Search—Backtracking (11)
• Stack
start 1
5
Push
3
Push
1
2
3
4
6
7
8
9
10
11
12
goal
19
Depth First Search—Backtracking (12)
• Stack
start 1
5
Push
7
Push
3
Push
1
2
3
4
6
7
8
9
10
11
12
goal
20
Depth First Search—Backtracking (13)
• Stack
start 1
Push
10
Push
7
Push
3
Push
1
5
2
3
4
6
7
8
9
10
11
12
goal
21
DFS Implementation
DFS() {
stack<location> S;
// mark the start location as visited
S.push(start);
while (S is not empty) {
t = S.top();
if (t == goal) Success(S);
if (// t has unvisited neighbors) {
// choose an unvisited neighbor n
// mark n visited;
S.push(n);
} else {
BackTrack(S);
}
}
Failure(S);
}
22
DFS Implementation (2)
BackTrack(S) {
while (!S.empty() && S.top() has no unvisited neighbors)
{
S.pop();
}
}
Success(S) {
// print success
while (!S.empty()) {
output(S.top());
S.pop();
}
}
23
Runtime Stack
• Runtime environment
– Static
heap
• Executable code
• Global variables
– Stack
• Push for each function call
• Pop for each function return
• Local variables
– Heap
• Dynamically allocated memories
• new and delete
stack
static
program memory
24
Queue ADT - FIFO
• Collection
– Elements of some proper type T
• Operations
–
–
–
–
–
–
–
Feature: First In, First Out
void push(T t)
void pop()
T front()
bool empty()
unsigned int size()
Constructors and destructors
25
Queue Model—FIFO
• Empty Q
animal parade queue
26
Queue Model—FIFO
• Q.Push(“ant”)
animal parade queue
ant
front
back
27
Queue Model—FIFO
• Q.Push(“bee”)
animal parade queue
ant
bee
front
back
28
Queue Model—FIFO
• Q.Push(“cat”)
animal parade queue
ant
bee
cat
front
back
29
Queue Model—FIFO
• Q.Push(“dog”)
animal parade queue
ant
bee
cat
dog
front
back
30
Queue Model—FIFO
• Q.Pop()
animal parade queue
bee
cat
dog
front
back
31
Queue Model—FIFO
• Q.Pop()
animal parade queue
cat
dog
front
back
32
Queue Model—FIFO
• Q.Push(“eel”)
• Q.Pop()
• Q.Pop()
animal parade queue
eel
front
back
33
Implementations and Uses of Queue ADT
• Implementations
– Any list implementation
• push_front()/pop_back()
• push_back()/?
• Uses
– Buffers
– Breadth first search
– Simulations
– Producer-Consumer Problems
34
Breadth First Search
• Problem
– Find a shortest path from
start to goal
start 1
• Solution
– Start from
• Node start
2
3
4
6
7
8
9
10
– Visit
• All neighbors of the node
– Stop
5
• If a neighbor is goal
– Otherwise
• Visit neighbors two hops
away
11
12
goal
– Repeat (Stop/Otherwise)
• Visiting neighbors N hops
away
35
Breadth First Search (2)
• Queue
1
Push
start 1
5
2
3
4
6
7
8
9
10
goal
11
12
36
Breadth First Search (3)
• Queue
Pop
start 1
5
2
3
4
6
7
8
9
10
goal
11
12
37
Breadth First Search (4)
• Queue
2
3
Push
4
Push
start 1
Push
5
2
3
4
6
7
8
9
10
goal
11
12
38
Breadth First Search (5)
• Queue
3
4
Pop
start 1
5
2
3
4
6
7
8
9
10
goal
11
12
39
Breadth First Search (6)
• Queue
3
4
5
6
Push
Push
start 1
5
2
3
4
6
7
8
9
10
goal
11
12
40
Breadth First Search (7)
• Queue
4
5
6
Pop
start 1
5
2
3
4
6
7
8
9
10
goal
11
12
41
Breadth First Search (8)
• Queue
4
5
6
7
8
Push
Push
start 1
5
2
3
4
6
7
8
9
10
goal
11
12
42
Breadth First Search (9)
• Queue
5
6
7
8
Pop
start 1
5
2
3
4
6
7
8
9
10
goal
11
12
43
Breadth First Search (10)
• Queue
6
7
8
Pop
start 1
5
2
3
4
6
7
8
9
10
goal
11
12
44
Breadth First Search (11)
• Queue
7
8
Pop
start 1
5
2
3
4
6
7
8
9
10
goal
11
12
45
Breadth First Search (12)
• Queue
7
8
9
Push
start 1
5
2
3
4
6
7
8
9
10
goal
11
12
46
Breadth First Search (13)
• Queue
8
9
Pop
start 1
5
2
3
4
6
7
8
9
10
goal
11
12
47
Breadth First Search (14)
• Queue
8
9
10
Push
start 1
5
2
3
4
6
7
8
9
10
goal
11
12
48
BFS Implementation
BFS {
queue<location> Q;
// mark the start location as visited
Q.push(start);
while (Q is not empty) {
t = Q.front();
for (// each unvisited neighbor n of node
t) {
Q.push(n);
if (n == goal) Success(S);
}
Q.pop();
}
Failure(Q);
}
49
Adaptor Class
• Adapts the public interface of another class
• Adaptee: the class being used
• Adaptor: the new class being defined
– Uses protected object of the adaptee type
– Uses the adaptee’s methods to define adaptor methods
• Stack and Queue implemented via adaptor classes
50
Stack Adaptor Requirements
• Stack
–
–
–
–
–
push()
pop()
top()
empty()
size()
• Can use List, Deque
– Push(): push_back()
– Pop(): pop_back()
51
Class Stack
template <typename T, class Container>
class Stack {
protected:
Container c;
public:
void push(const T & x) { c.push_back(x); }
void pop() { c.pop_back(); }
T top() const { return c.back(); }
int empty() const { return c.empty(); }
unsigned int size() const { return c.size(); }
void clear() { c.clear(); }
};
• Declaration
– Stack<float, List<float> > floatStack;
– Stack<int, Vector<int> > intStack;
• For STL stack container
– template <typename T, typename Container = deque<T> > class stack;
– stack<char> charStack;
52
Queue Adaptor Requirements
• Queue
–
–
–
–
–
push()
pop ()
front()
empty()
size()
• Can use List, Deque
– Push(): push_front()
– Pop(): pop_back()
53
Class Queue
template <typename T, class Container>
class Queue {
protected:
Container c;
public:
void push(const T & x) { c.push_back(x); }
void pop() { c.pop_front(); }
T front() const { return c.front(); }
int empty() const { return c.empty(); }
unsigned int size() const { return c.size(); }
void clear() { c.clear(); }
};
• Declaration
Queue<float, List<float> > floatQueue;
Queue<int, List<int> > intQueue;
• For STL stack container
template <typename T, typename Container = deque<T> > class queue;
queue<char> charQueue;
54
Circular Array
animal parade queue
back
front
55
Circular Array
• Q.Push(“ant”)
animal parade queue
ant
front
back
56
Queue Model—FIFO
• Q.Push(“bee”)
animal parade queue
ant
bee
front
back
57
Queue Model—FIFO
• Q.Push(“cat”)
animal parade queue
cat
ant
back
front
bee
58
Queue Model—FIFO
• Q.Push(“dog”)
animal parade queue
cat
dog
ant
back
front
bee
59
Queue Model—FIFO
• Q.Pop()
animal parade queue
cat
dog
bee
back
front
60
Queue Model—FIFO
• Q.Pop()
animal parade queue
cat
front
dog
back
61
Expanding the Array
a
b
c
a
b
c
Where are front and back?
Why can’t we use all four locations?
c
a
b
c
c
a
b
a
a
b
b
c
62
Download