Stacks and Queues
25
Stacks and Queues 25 Stack In/Out LIFO: Last-in First-out Push - - PowerPoint PPT Presentation
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
25
26
In/Out Push Pop
Undo/Redo Back/Forward
Function call/return
In Enqueue Out Dequeue
Front Back
27
HPC
Jobs
Queue
Stack
28
29
Push_back Pop_back Push_front Pop_front Front Back Size Empty
Push Pop Top Size Empty
Enqueue Dequeue Front Size Empty
30
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)
31
Front Back
32
Front Back
33
Front
Back
34
Back Front
35
Queue Circular Array Impl. Circular Linked List Impl.
Enqueue O(1) O(1) Dequeue O(1) O(1) Front O(1) O(1) Memory overhead Small Big
36
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
38
Stack of
39
Stack of
40
Stack of
3
Push
41
Stack of
3 5
Push
42
Stack of
3 5
Pop
43
Stack of
3 5
Pop
44
Stack of
3 5 ⨉
45
Stack of
15
Push
46
Stack of
15
Push
4
47
Stack of
15 4
Push
2
48
Stack of
15 4 2
49
Stack of
15 4 2
Pop Pop
/ 2
Push
50
Stack of
15 2
Pop Pop
+ 17
Push
51
Stack of
17
Push
2
52
Stack of
17 2
Pop Pop
⨉ 34
Push
53
Stack of
34
Infix (input): 3 × 5 + 4/2 × 2 Postfix (desired output): 35 × 42/+2 ×
54
55
Stack of
(
Input Output
56
Stack of
(
Input Output
57
Stack of
(
Input Output
⨉
58
Stack of
(
Input Output
⨉
59
Stack of
(
Input Output
+
60
Stack of
(
Input Output
+
61
Stack of
(
Input Output
+ /
62
Stack of
(
Input Output
+ /
63
Stack of
Input Output
64
Stack of
Input Output
⨉
65
Stack of
Input Output
⨉
66
Stack of
Input Output