Download presentation
Presentation is loading. Please wait.
Published by수연 소봉 Modified over 6 years ago
1
Andy Wang Data Structures, Algorithms, and Generic Programming
Stacks and Queues Andy Wang Data Structures, Algorithms, and Generic Programming
2
Abstract Data Type A collection of data
A set of operations on the data or subsets of the data A set of axioms, or rules of behavior governing the interaction of operators Examples: stack, queue, list, vector, deque, priority queue, table (map), associative array, set, graph, digraph
3
Stack ADT Collections: Operations: Elements of some proper type T
void push(T t) void pop() T top() bool empty() unsigned int size() constructor and destructor
4
Stack ADT (2) Axioms (for any stack S)
S.size, S.empty(), and S.push(t) are always defined S.pop() and S.top() are defined iff S.empty() is false S.empty(), S.size(), S.top() do not change S S.empty() is true iff S.size() == 0 S.push(t) followed by S.pop() leaves S unchanged
5
Stack ADT (3) Axioms (for any stack S)
After S.push(t), S.top() returns t S.push(t) increases S.size() by 1 S.pop() decreases S.size() by 1
6
Stack Model—LIFO Empty stack S S.empty() is true S.top() not defined
S.size() == 0 food chain stack
7
Stack Model—LIFO S.push(“mosquito”) mosquito S.empty() is false
S.top() == “mosquito” S.size() == 1 mosquito food chain stack
8
Stack Model—LIFO S.push(“fish”) fish mosquito S.empty() is false
S.top() == “fish” S.size() == 2 fish mosquito food chain stack
9
Stack Model—LIFO S.push(“raccoon”) raccoon fish mosquito
S.empty() is false S.top() == “raccoon” S.size() == 3 raccoon fish mosquito food chain stack
10
Stack Model—LIFO S.pop() fish mosquito S.empty() is false
S.top() == “fish” S.size() == 2 fish mosquito food chain stack
11
Derivable Behaviors (Theorems)
If (S.size() == n) is followed by k push operations, then S.size() == n + k If (S.size() == n) is followed by k pop operations, then S.size == n – k (k <= n) The last element of S pushed onto S is the top of S S.pop() removes the last element of S pushed onto S
12
Uses of ADT Stack Depth first search / backtracking
Evaluating postfix expressions Converting infix to postfix Function calls (runtime stack) Recursion
13
Queue ADT Collection Operations Elements of some proper type T
void push(T t) void pop() T front() bool empty() unsigned int size() Constructors and destructors
14
Queue ADT Axioms (for any Queue Q)
Q.size(), Q.empty(), Q.push(t) are always defined Q.pop() and Q.front() are defined iff Q.empty() is false Q.empty(), Q.size(), Q.front() do not change Q Q.empty() is true iff Q.size() == 0 Suppose Q.size() == n, and the next element pushed onto Q is t; then, after n elements have been popped from Q, t = Q.front()
15
Queue ADT Axioms (for any Queue Q) Q.push(t) increases Q.size() by 1
Q.pop() decreases Q.size() by 1 If t = Q.front() then Q.pop() removes t from Q
16
Queue Model—FIFO Empty Q animal parade queue
17
Queue Model—FIFO Q.Push(“ant”) animal parade queue ant front back
18
Queue Model—FIFO Q.Push(“bee”) animal parade queue ant bee front back
19
Queue Model—FIFO Q.Push(“cat”) ant bee cat animal parade queue front
back
20
Queue Model—FIFO Q.Push(“dog”) ant bee cat dog animal parade queue
front back
21
Queue Model—FIFO Q.Pop() animal parade queue bee cat dog front back
22
Queue Model—FIFO Q.Pop() animal parade queue cat dog front back
23
Queue Model—FIFO Q.Push(“eel”) Q.Pop() eel animal parade queue front
back
24
Derivable Behaviors (Theorems)
If (Q.size() == n) is followed by k push operations, then Q.size() == n + k If (Q.size() == n) is followed by k pop operations, then Q.size() == n – k (k <= n) The first element pushed onto Q is the the front of Q Q.pop() removes the front element of Q
25
Uses of ADT Queue Buffers Breadth first search Simulations
26
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 start 1 2 3 4 5 6 7 8 9 10 11 12 goal
27
Depth First Search—Backtracking (2)
Stack 1 start 1 2 3 4 5 6 7 8 9 10 11 12 goal Push
28
Depth First Search—Backtracking (3)
Stack 2 1 start 1 2 3 4 5 6 7 8 Push 9 10 11 12 goal Push
29
Depth First Search—Backtracking (4)
Stack 5 2 1 start 1 2 3 4 5 6 7 8 Push Push 9 10 11 12 goal Push
30
Depth First Search—Backtracking (5)
Stack 6 5 2 1 start 1 2 3 4 Push 5 6 7 8 Push Push 9 10 11 12 goal Push
31
Depth First Search—Backtracking (6)
Stack 9 6 5 2 1 start 1 Push 2 3 4 Push 5 6 7 8 Push Push 9 10 11 12 goal Push
32
Depth First Search—Backtracking (7)
Stack 6 5 2 1 start 1 Pop 2 3 4 Push 5 6 7 8 Push Push 9 10 11 12 goal Push
33
Depth First Search—Backtracking (8)
Stack 5 2 1 start 1 2 3 4 Pop 5 6 7 8 Push Push 9 10 11 12 goal Push
34
Depth First Search—Backtracking (9)
Stack 2 1 start 1 2 3 4 5 6 7 8 Pop Push 9 10 11 12 goal Push
35
Depth First Search—Backtracking (10)
Stack 1 start 1 2 3 4 5 6 7 8 Pop 9 10 11 12 goal Push
36
Depth First Search—Backtracking (11)
Stack 3 1 start 1 2 3 4 5 6 7 8 Push 9 10 11 12 goal Push
37
Depth First Search—Backtracking (12)
Stack 7 3 1 start 1 2 3 4 5 6 7 8 Push Push 9 10 11 12 goal Push
38
Depth First Search—Backtracking (13)
Stack 10 7 3 1 start 1 2 3 4 Push 5 6 7 8 Push Push 9 10 11 12 goal Push
39
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 // mark n visited; S.push(n); } else { BackTrack(S); } Failure(S);
40
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()); Failure(S) { // print failure
41
Breadth First Search Problem Find a shortest path from start to goal
1 2 3 4 5 6 7 8 9 10 11 12 goal
42
Breadth First Search (2)
Queue 1 Push start 1 2 3 4 5 6 7 8 9 10 11 12 goal
43
Breadth First Search (3)
Queue Pop start 1 2 3 4 5 6 7 8 9 10 11 12 goal
44
Breadth First Search (4)
Queue 2 3 4 Push Push Push start 1 2 3 4 5 6 7 8 9 10 11 12 goal
45
Breadth First Search (5)
Queue 3 4 Pop start 1 2 3 4 5 6 7 8 9 10 11 12 goal
46
Breadth First Search (6)
Queue 3 4 5 6 Push Push start 1 2 3 4 5 6 7 8 9 10 11 12 goal
47
Breadth First Search (7)
Queue 4 5 6 Pop start 1 2 3 4 5 6 7 8 9 10 11 12 goal
48
Breadth First Search (8)
Queue 4 5 6 7 8 Push Push start 1 2 3 4 5 6 7 8 9 10 11 12 goal
49
Breadth First Search (9)
Queue 5 6 7 8 Pop start 1 2 3 4 5 6 7 8 9 10 11 12 goal
50
Breadth First Search (10)
Queue 6 7 8 Pop start 1 2 3 4 5 6 7 8 9 10 11 12 goal
51
Breadth First Search (11)
Queue 7 8 Pop start 1 2 3 4 5 6 7 8 9 10 11 12 goal
52
Breadth First Search (12)
Queue 7 8 9 Push start 1 2 3 4 5 6 7 8 9 10 11 12 goal
53
Breadth First Search (13)
Queue 8 9 Pop start 1 2 3 4 5 6 7 8 9 10 11 12 goal
54
Breadth First Search (14)
Queue 8 9 10 Push start 1 2 3 4 5 6 7 8 9 10 11 12 goal
55
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) { Q.push(n); if (n == goal) Success(S); } Q.pop(); Failure(Q);
56
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, )
57
Evaluating Postfix Expressions (2)
Evaluation algorithm: Use stack of tokens Repeat If operand, push onto stack If operator pop operands off stack evaluate operator on operands push result onto stack Until expression is read Return top of stack
58
Evaluating Postfix Expressions (3)
Most CPUs have hardware support for this algorithm Translation from infix to postfix also uses a stack (software)
59
Evaluating Postfix Expressions (4)
Original expression: 1 + (2 + 3) * 4 + 5 Evaluate: * + 5 +
60
Evaluating Postfix Expressions (5)
Input: * + 5 + Push(1) 1
61
Evaluating Postfix Expressions (6)
Input: * + 5 + Push(2) 2 1
62
Evaluating Postfix Expressions (7)
Input: * + 5 + Push(3) 3 2 1
63
Evaluating Postfix Expressions (8)
Input: + 4 * + 5 + Pop() == 3 Pop() == 2 1
64
Evaluating Postfix Expressions (9)
Input: + 4 * + 5 + Push(2 + 3) 5 1
65
Evaluating Postfix Expressions (10)
Input: 4 * + 5 + Push(4) 4 5 1
66
Evaluating Postfix Expressions (11)
Input: * + 5 + Pop() == 4 Pop() == 5 1
67
Evaluating Postfix Expressions (12)
Input: * + 5 + Push(5 * 4) 20 1
68
Evaluating Postfix Expressions (13)
Input: Pop() == 20 Pop() == 1
69
Evaluating Postfix Expressions (14)
Input: Push(1 + 20) 21
70
Evaluating Postfix Expressions (15)
Input: 5 + Push(5) 5 21
71
Evaluating Postfix Expressions (16)
Input: + Pop() == 21 Pop() == 5
72
Evaluating Postfix Expressions (17)
Input: + Push(21 + 5) 26
73
Evaluating Postfix Expressions (18)
Input: Pop() == 26
74
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
75
Runtime Stack Runtime environment Static Stack Heap Executable code
Global variables Stack Push for each function call Pop for each function return Heap Dynamic new and delete heap stack static program memory
76
Recursion Order 1: function calls itself
Order 2: f() calls g(), and g() calls f() Facilitated by stack
77
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
78
Stack Adaptor Requirements
PushBack() PopBack() Back() Empty() Size() Can use TDeque, TList, TVector
79
Queue Adaptor Requirements
PushBack() PopFront() Front() Empty() Size() Can use TDeque, TList
80
Class CStack template <typename T, class Container>
protected: Container c; public: typedef T value_type; void Push(const value_type& x) { c.PushBack(x); } void Pop() { c.PopBack(); } value_type Top() const { return c.Back(); } int Empty() const { return c.Empty(); } unsigned int Size() const { return c.Size(); } void Clear() { c.Clear(); } void Display(ostream& os, char ofc) const; };
81
Class CStack (2) template <typename T, class Container>
void CStack<T, Container>::Display(std::ostream& os, char ofc) const { typename Container::Iterator I; if (ofc == ‘\0’) { for (I = c.Begin(); I != c.End(); ++I) { os << *I; } } else { os << *I << ofc; std::ostream& operator<<(std::ostream& os, const CStack<T, Container>& s) { s.Display(os); return os;
82
Declarations CStack<float, TList<float> > floatStack;
CStack<int, TDeque<int> > intStack;
83
Class CQueue template <typename T, class Container>
protected: Container c; public: typedef T value_type; void Push(const value_type& x) { c.PushBack(x); } void Pop() { c.PopFront(); } value_type Front() const { return c.Front(); } int Empty() const { return c.Empty(); } unsigned int Size() const { return c.Size(); } void Clear() { c.Clear(); } void Display(ostream& os, char ofc) const; };
84
Class CQueue (2) template <typename T, class Container>
void CQueue<T, Container>::Display(std::ostream& os, char ofc) const { typename Container::Iterator I; if (ofc == ‘\0’) { for (I = c.Begin(); I != c.End(); ++I) { os << *I; } } else { os << *I << ofc; std::ostream& operator<<(std::ostream& os, const CQueue<T, Container>& s) { s.Display(os); return os;
85
Example Usage Functionality Testing Performance Testing fcstack.cpp
fcqueue.cpp Performance Testing qrace.cpp srace.cpp dragstrp.h timer.h datetime.h xran.h
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.