stacks and queues - ucr computer science and engineering · 2017-10-11 · lists, stacks, and...
TRANSCRIPT
Stacks and Queues
25
Stack
LIFO: Last-in First-out
26
In/Out
Push Pop
Stack
Undo/Redo
Back/Forward
Function call/return
In
EnqueueOut
Dequeue
FrontBack
Queue
Queue
FIFO: First-in First-out
27
HPC
Jobs
Queue and Stack ADT
Queue
Enqueue
Dequeue
Front
Size
Empty?
Stack
Push
Pop
Top
Size
Empty?
28
Stack/Queue Implementation
29
ListPush_back
Pop_back
Push_front
Pop_front
Front
Back
Size
Empty
StackPush
Pop
Top
Size
Empty
QueueEnqueue
Dequeue
Front
Size
Empty
Queue Implementation
30
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)
Circular Array Queue
31
Front
Back
Circular Array Queue
32
Front
Back
Enqueue
33
Front
Dequeue
Circular Array Queue
Back
Circular Linked List Queue
34
Back
Front
Queue Implementation
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
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 operands3 + 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 operands325 ×+= 13
Easier to compute in one pass
No need for parentheses37
Evaluate postfix expressions
Infix: 3 × 5 + 4/2 × 2 = 34
Postfix: 35 × 42/+2 ×
38
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
Postfix Evaluation Example
39
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
Postfix Evaluation Example
40
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
3Push
Postfix Evaluation Example
41
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
3
5Push
Postfix Evaluation Example
42
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
3
5Pop
Postfix Evaluation Example
43
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
35Pop
Postfix Evaluation Example
44
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
35 ⨉
Postfix Evaluation Example
45
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
15Push
Postfix Evaluation Example
46
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
15
Push
4
Postfix Evaluation Example
47
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
15
4
Push
2
Postfix Evaluation Example
48
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
15
4
2
Postfix Evaluation Example
49
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
15
4 2Pop
Pop/
2Push
Postfix Evaluation Example
50
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
15 2Pop
Pop+
17Push
Postfix Evaluation Example
51
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
17
Push
2
Postfix Evaluation Example
52
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
17 2Pop
Pop⨉
34Push
Postfix Evaluation Example
53
3 5 ⨉ 4 2 / + 2 ⨉
Stack of
operands
34
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
55
( 3 ⨉ 5 + 4 / 2 ) ⨉ 2
Stack of
operators
(
Input
Output
Example
56
( 3 ⨉ 5 + 4 / 2 ) ⨉ 2
Stack of
operators
(
Input
Output
3
Example
57
( 3 ⨉ 5 + 4 / 2 ) ⨉ 2
Stack of
operators
(
Input
Output
3
⨉
Example
58
( 3 ⨉ 5 + 4 / 2 ) ⨉ 2
Stack of
operators
(
Input
Output
35
⨉
Example
59
( 3 ⨉ 5 + 4 / 2 ) ⨉ 2
Stack of
operators
(
Input
Output
35⨉
+
Example
60
( 3 ⨉ 5 + 4 / 2 ) ⨉ 2
Stack of
operators
(
Input
Output
35⨉4
+
Example
61
( 3 ⨉ 5 + 4 / 2 ) ⨉ 2
Stack of
operators
(
Input
Output
35⨉4
+
/
Example
62
( 3 ⨉ 5 + 4 / 2 ) ⨉ 2
Stack of
operators
(
Input
Output
35⨉42
+
/
Example
63
( 3 ⨉ 5 + 4 / 2 ) ⨉ 2
Stack of
operators
Input
Output
35⨉42/+
Example
64
( 3 ⨉ 5 + 4 / 2 ) ⨉ 2
Stack of
operators
Input
Output
35⨉42/+
⨉
Example
65
( 3 ⨉ 5 + 4 / 2 ) ⨉ 2
Stack of
operators
Input
Output
35⨉42/+2
⨉
Example
66
( 3 ⨉ 5 + 4 / 2 ) ⨉ 2
Stack of
operators
Input
Output
35⨉42/+2⨉