Upload
margaret-thomas
View
220
Download
2
Embed Size (px)
Citation preview
Stack ADT
Collections: Elements of some proper type T
Operations: void push(T t) void pop() T top() bool empty() unsigned int size() constructor and destructor
Uses of ADT Stack
Depth first search / backtracking
Evaluating postfix expressions
Converting infix to postfix
Function calls (runtime stack)
Recursion
Stack Model—LIFO
Empty stack S S.empty() is true S.top() not defined S.size() == 0
S.push(“mosquito”) S.empty() is false S.top() == “mosquito” S.size() == 1
S.push(“fish”) S.empty() is false S.top() == “fish” S.size() == 2
food chain stack
Stack Model—LIFO
S.push(“raccoon”) S.empty() is false S.top() == “raccoon”
S.size() == 3
S.pop() S.empty() is false S.top() == “fish” S.size() == 2
food chain stack
Queue ADT - FIFO
Collection Elements of some proper type T
Operations void push(T t) void pop() T front() bool empty() unsigned int size() Constructors and destructors
Queue Model—FIFO
Q.push( “ant” )Q.push( “bee” )Q.push( “cat” )Q.push( “dog” )
front
back
animal_parade_queue
back back back back
Queue Model—FIFO
Q.pop(); Q.pop(); Q.push( “eel” ); Q.pop(); Q.pop();
front
animal_parade_queue
back back back back
Depth First Search—Backtracking Problem
Discover a path from start to goal
Solution Go deep
If there is an unvisited neighbor, go there
Backtrack Retreat along the path to
find an unvisited neighbor Outcome
If there is a path from start to goal, DFS finds one such path
1
2 3 4
5 6 87
9 10 1211
start
goal
Depth First Search—Backtracking (5)
Stack
1
2 3 4
5 6 87
9 10 1211
start
goal
6
5
2
1Push
Push
Push
Push
Depth First Search—Backtracking (6)
Stack
1
2 3 4
5 6 87
9 10 1211
start
goal
9
6
5
2
1Push
Push
Push
Push
Push
Depth First Search—Backtracking (7)
Stack
1
2 3 4
5 6 87
9 10 1211
start
goal
6
5
2
1Push
Push
Push
Push
Pop
Depth First Search—Backtracking (8)
Stack
1
2 3 4
5 6 87
9 10 1211
start
goal
5
2
1Push
Push
Push
Pop
Depth First Search—Backtracking (13)
Stack
1
2 3 4
5 6 87
9 10 1211
start
goal
10
7
3
1Push
Push
Push
Push
DFS Implementation
DFS() {stack<location> S;
// mark the start location as visitedS.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);
}
DFS Implementation (2)
BackTrack(S) {while (!S.empty() && S.top() has no unvisited neighbors) {
S.pop();}
}
Success(S) {// print successwhile (!S.empty()) {
output(S.top());S.pop();
}}
Failure(S) {// print failurewhile (!S.empty()) {
S.pop();}
}
Breadth First Search
Problem Find a shortest path from start to goal
1
2 3 4
5 6 87
9 10 1211
start
goal
BFS Implementation
BFS {queue<location> Q;
// mark the start location as visitedQ.push(start);while (Q is not empty) {
t = Q.front();for (// each unvisited neighbor n) {
Q.push(n);if (n == goal) Success(S);
}Q.pop();
}Failure(Q);
}
Evaluating Postfix Expressions
Postfix expressions: operands precede operator
Tokens: atomics of expressions, either operator or operand
Example: z = 25 + x*(y – 5) Tokens: z, =, 25, +, x, *, (, y, -, 5, )
Evaluating Postfix Expressions (2)
Evaluation algorithm: Use stack of tokens Repeat
If operand, push onto stack If operator
• pop operands off the stack• evaluate operator on operands• push result onto stack
Until expression is read Return top of stack
Evaluating Postfix Expressions (3)
Most CPUs have hardware support for this algorithm
Translation from infix to postfix also uses a stack (software)
Evaluating Postfix Expressions (4)
Original expression: 1 + (2 + 3) * 4 + 5Evaluate: 1 2 3 + 4 * + 5 +
Postfix Evaluation Implementation
Evaluate(postfix expression) {
// use stack of tokens;while(// expression is not empty) {
t = next token;if (t is operand) {
// push onto stack} else {
// pop operands for t off stack// evaluate t on these operands// push result onto stack
}}// return top of stack
}
Runtime Stack
Runtime environment Static
Executable code Global variables
Stack Push for each function call Pop for each function return
Heap Dynamic new and delete
static
stack
heap
program memory
Recursion
Order 1: function calls itself
Order 2: f() calls g(), and g() calls f()
Facilitated by stack
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
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(); }
};
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(); }
};