Stacks and Queues 25
Stack In/Out LIFO: Last-in First-out Push Pop Undo/Redo Back/Forward Function call/return Stack 26
Queue HPC FIFO: First-in First-out Back Front In Out Jobs Enqueue Dequeue Queue 27
Queue and Stack ADT Queue Stack Enqueue Push Dequeue Pop Front Top Size Size Empty? Empty? 28
Stack/Queue Implementation Queue List Enqueue Push_back Dequeue Pop_back Front Size Push_front Empty Pop_front Stack Front Back Push Pop Size Top Empty Size Empty 29
Queue Implementation Queue Array Impl. Linked List Impl. Enqueue O(1) O(1) Dequeue O(n) O(1) Front O(1) O(1) Memory overhead Small Big Random access O(1) O(n) 30
Circular Array Queue Front Back 31
Circular Array Queue Front Enqueue Back 32
Circular Array Queue Dequeue Front Back 33
Circular Linked List Queue Front Back 34
Queue Implementation Queue Circular Array Circular Linked Impl. List Impl. Enqueue O(1) O(1) Dequeue O(1) O(1) Front O(1) O(1) Memory overhead Small Big 35
Standard Template Library (STL) Lists, stacks, and queues are all implemented in STL In a real program, you would better use them; why? For the sake of learning, you are not allowed to use STL during this class unless otherwise mentioned 36
Stack Applications Expression evaluation Human-friendly infix expressions The operator falls between the two operands 3 + 2 × 5 = 13 Easier to read and understand Can be easily broken into pieces Machine-friendly postfix expressions The operator is placed after the two operands 325 ×+= 13 Easier to compute in one pass No need for parentheses 37
Evaluate postfix expressions Infix: 3 × 5 + 4/2 × 2 = 34 Postfix: 35 × 42/+2 × ⨉ ⨉ 3 5 4 2 / + 2 Stack of operands 38
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Stack of operands 39
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Push 3 Stack of operands 40
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Push 5 3 Stack of operands 41
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Pop 5 3 Stack of operands 42
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Pop 5 3 Stack of operands 43
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 ⨉ 5 3 Stack of operands 44
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Push 15 Stack of operands 45
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Push 4 15 Stack of operands 46
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Push 2 4 15 Stack of operands 47
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 2 4 15 Stack of operands 48
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Pop 4 2 / Pop Push 2 15 Stack of operands 49
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Pop 15 2 + Pop Push 17 Stack of operands 50
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Push 2 17 Stack of operands 51
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 Pop 17 ⨉ 2 Pop Push 34 Stack of operands 52
Postfix Evaluation Example ⨉ ⨉ 3 5 4 2 / + 2 34 Stack of operands 53
Infix to Postfix Conversion Convert the input into a sequence of operators and operands Account for operator precedence Account for parentheses Example Infix (input): 3 × 5 + 4/2 × 2 Postfix (desired output): 35 × 42/+2 × 54
Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output ( Stack of operators 55
Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 3 ( Stack of operators 56
Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 3 ⨉ ( Stack of operators 57
Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ ( Stack of operators 58
Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ + ( Stack of operators 59
Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 4 + ( Stack of operators 60
Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 4 / + ( Stack of operators 61
Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 42 / + ( Stack of operators 62
Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 42/+ Stack of operators 63
Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 42/+ ⨉ Stack of operators 64
Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 42/+2 ⨉ Stack of operators 65
Example Input ⨉ ⨉ ( 3 5 + 4 / 2 ) 2 Output 35 ⨉ 42/+2 ⨉ Stack of operators 66
Recommend
More recommend