Professional Documents
Culture Documents
CHAPTER FIVE
1. Overview
2. Stacks
3. Queues
4. Deques
5. Priority queues
3/2/2024
CH-5 Contents
3
1. Overview
2. Stacks
3. Queues
4. Deques
5. Priority queues
3/2/2024
Overview
4
3/2/2024
5
3/2/2024
What are the difference?
6
3/2/2024
CH-5 Contents
7
1. Overview
2. Stacks
3. Queues
4. Deques
5. Priority queues
3/2/2024
Stack
8
3/2/2024
9
3/2/2024
10
3/2/2024
Basic Operations
11
3/2/2024
PUSH Operation
12
3/2/2024
13
3/2/2024
POP Operation
14
3/2/2024
15
3/2/2024
16
3/2/2024
Implementation of Stacks
17
3/2/2024
Array implementation of Stack
18
3/2/2024
20 3/2/2024
Algorithm for Pop
21
3/2/2024
22 3/2/2024
Linked List implementation of Stack
23
3/2/2024
The PUSH Operation
24
3/2/2024
25
3/2/2024
Setup EMPTY Stack
26
3/2/2024
Algorithm for PUSH
27
3/2/2024
The POP Operation
29
3/2/2024
30
3/2/2024
Algorithm for POP
31
3/2/2024
32 3/2/2024
Application of Stacks
33
3/2/2024
Application of Stacks: Expression Parsing
34
Infix Notation
We write expression in infix notation, e.g. a-b+c, where
operators are used in-between operands.
It is easy for us humans to read, write, and speak in
infix notation but the same does not go well with
computing devices.
An algorithm to process infix notation could be difficult
and costly in terms of time and space consumption.
3/2/2024
36
Prefix Notation
In this notation, operator is prefixed to operands, i.e.
operator is written ahead of operands.
For example, +ab. This is equivalent to its infix notation
a+b.
Prefix notation is also known as Polish Notation.
Postfix Notation
This notation style is known as Reversed Polish Notation. In
this notation style, the operator is post fixed to the
operands i.e., the operator is written after the operands.
For example, ab+. This is equivalent to its infix notation
a+b.
3/2/2024
37
3/2/2024
Parsing Expressions
38
3/2/2024
Precedence and Associativity
39
3/2/2024
Infix to Postfix Conversion
40
3/2/2024
41
3/2/2024
42
3/2/2024
Example:
43
3/2/2024
Postfix Evaluation Algorithm
44
3/2/2024
Example
45
E.g. 1
InfixExpression : 1*(2+3)
Postfix expression : 123+*
◼ Result is : 5
E.g. 2
InfixExpression : (1+2)*(3+4)
Postfix expression : 12+34+*
◼ Result is : 21
3/2/2024
CH-5 Contents
46
1. Overview
2. Stacks
3. Queues
4. Deques
5. Priority queues
3/2/2024
Queues
47
3/2/2024
Queue Representation
48
3/2/2024
Basic Operation
49
3/2/2024
Enqueue Operation
50
3/2/2024
51
3/2/2024
Dequeue Operation
52
3/2/2024
53
3/2/2024
Array implementation of queues
A queue is a first in, first out (FIFO) data structure
This is accomplished by inserting at one end (the rear) and
deleting from the other (the front)
3/2/2024
Circular arrays
57
Elements were added to this queue in the order 11, 22, 33,
44, 55, and will be removed in the same order
Use: front = (front + 1) % myQueue_length;
and: rear = (rear + 1) % myQueue_length;
3/2/2024
Circular Array
58
3/2/2024
Full and empty queues
59
3/2/2024
Full and empty queues: solutions
60
3/2/2024
Linked-list implementation of queues
61
3/2/2024
SLL implementation of queues
62
3/2/2024
Enqueueing a node
63
3/2/2024
Dequeueing a node
64
3/2/2024
Queue implementation details
65
3/2/2024
Exercise
66
1. Overview
2. Stacks
3. Queues
4. Deques
5. Priority queues
3/2/2024
Deques
68
Front Rear
1. Overview
2. Stacks
3. Queues
4. Deques
5. Priority queues
3/2/2024
Priority Queues
71
3/2/2024
Examples
72
3/2/2024
73
3/2/2024
Operations on Priority Queues
75
3/2/2024
Implementations of Priority Queues
76
Priority
Queue
Implemented as:
3/2/2024
Array Implementation of Priority
Queues
77
One implementation:
1 N Max
A 16 14 10 9 8 7 4 3 1
L 16 14 10 9 8 7 4 3 1
Demerging Queues
is the process of creating two or more queues from a
single queue.
used to give priority for some groups of data
◼ Example: The following two queues can be created from the
below priority queue.
3/2/2024
81
Merging Queues
isthe process of creating a priority queue from two or
more queues.
The ordinary dequeue implementation can be used to
delete data in the newly created priority queue.
◼ Example: The following two queues (females queue has higher
priority than the males queue) can be merged to create a priority
queue.
Aster Meron Abebe Alemu Belay Kedir Yonas
Female Female Male Mal Male Male Male
Questions?
3/2/2024
84
Thank You
3/2/2024