CST201 M2 Ktunotes - in
CST201 M2 Ktunotes - in
MODULE II
Jacob P Cherian
Asst.Professor
Dept.of CSE, Saintgits College of Engineering
1
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
CONTENTS
2008 2
8 9 2 1 0 7 6 2
2012 1
2016 0
2028 2
.
00000000000000000000000000001000 = 8 (Decimal)
.
3000
3
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Terminology
M: Base address of the array.
2004
2008
M A[0] L
2012 5
A[1] L+1
2016 4
A[2]
2020 8
A[3] A[i]=M+(i-L)*w
2024 9
A[4]
2028 1
A[5] U-1
2032 2
A[6] U 2036 6
Size = (U - L) + 1 Index,i = (L + P) - 1
5
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Question 1
6
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
L = -15
U = 64
M = 459
W=2
(a) How many number of elements are there in the array A?
Size = U – L + 1
Size = 64–(-15) + 1
Size = 64 + 15 + 1
Size = 80
8
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
(c) What is the address location for A[50]?
L = -15
i = 50 U = 64
Address = M + (i–L) * W M = 459
W=2
Address = 459 + (50–(-15)) * 2
11
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
2-D Arrays (Matrices)
12
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Linear Memory Representation
Row Major Order
5 5
Insert New Element
Here 4 4
3 6
2 3
Push Down One Stroke to
make room for new element . 2
. . .
. . .
. . .
. A[U] .
16
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Array Operation- 5 A[L] 5
Deletion 1 1
4 4
0 0
6 6
Delete Element Here
2 2
Push Up each element by
one position. 7 3
3 2
2 .
. .
. A[U] .
17
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
A1
A
4 Size(A1)=N1
4
5
5 N1 elements from A1
.
.
.
.
3
A2
3
Array Operation-
6
6
Merging
7
7
.
N2 elements from A2
.
.
.
0
Size(A2)=N2
0
18
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Application of Arrays-Polynomials
A polynomial p(x) is the expression in variable x which is in the form (ax n + bxn-1
+ ... + jx+ k), where a, b, c ..., k fall in the category of real numbers and 'n' is non
negative integer, which is called the degree of polynomial.
Exponent
Coefficient
19
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Representing Polynomials Using Arrays
7 struct poly
{
5x2 + 3x +7 float coeff;
int exp;
};
3x
20
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Polynomial Arithmetic-Addition
4x3-5x2+2x+7
7 2 -5 4
2x2+3x+7 0x3+2x2+3x+
7
7 3 2 0
4x3-3x2+5x+14
14 5 -3 4
1 0 5 6 4 0 0 0
1 0 0 1 0
X
0 9 8 IX 1 0 0 TRI
MA1 0 RI X
ATR AT
E M RS E 9 0 S0
R E
M 6 0
DEN
S SPA S PA
0 8 4 -8 -1 0 0 -8
0 0 0 -1 1
-11 0 2 0 0 0 0 1
22
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Sparsity & Density
1 0 5 6 4 0 0 0
0 9 8 1 0 0 1 E 0
RSE RS
SPA SPA
0 318% 4 -8 -1 602% 0 -8
-11 0 2 0 0 0 0 1
Sparsity = 5 / 16 Sparsity = 10 / 16
Density = 11 / 16 Density = 6 / 16
23
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Triangular Matrices
24
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Examples-Upper & Lower Triangular Matrices
1 2 6 7 6 2 3 9
7 3 8 0 0 7 5 4
9 1 0 0 0 0 3 -5
6 0 0 0 0 0 0 2
3 0 0 0 0 0 0 5
6 4 0 0 0 0 3 -4
-3 7 5 0 0 2 -12 14
-9 8 2 7 1 7 16 9
25
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Diagonal Matrices
26
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Representing Sparse Matrix as 2D Array
Representing a sparse matrix by a 2D array leads to wastage of lots of
memory as zeroes in the matrix are of no use in most of the cases.
This means storing non-zero elements with triples- (Row, Column, value).
27
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Memory Wastage
0 0 0 0 3 0 0 0 0 6
0 0 0 2 0 0 1 0 0 0
5 0 0 0 0 6 0 0 0 0
3 5 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 7 0 0 0 7 0
0 0 0 0 0 0 0 2 0 0
0 0 1 0 0 0 0 0 0 0
0 2 0 0 1 0 0 0 0 0
8 0 0 5 0 1 1 0 0 1
28
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Representing Sparse Matrix as 2D Array
0 1 2 3
0 0 7 0 0
Row 0 1 2 2 3
1 3 0 0 0
Colum 1 0 0 2 3
2 1 0 1 0 n
3 0 0 0 2 Value 7 3 1 1 2
29
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Linear Search
In computer science, a linear search or sequential search is a method for finding
an element within a list.
It sequentially checks each element of the list until a match is found or the whole
list has been searched.
30
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Linear Search
Element to be Searched 9
5 6 8 2 1 0 9 7 4
Is a[i]=key ? 31
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Linear Search
Element
Element to be Searched 9 found at
index 6
5 6 8 2 1 0 9 7 4
YES!
32
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Binary Search
Also known as half-interval search, logarithmic search, or binary chop.
Binary Search a search algorithm which finds the position of a search element(key or
target value) within a sorted array.
33
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Binary Search
We basically ignore half of the elements just after one comparison.
34
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Binary Search
low + high
Element to be Searched 7 mid = ------------
2
0 1 2 4 5 6 7 8 9
35
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Binary Search
Is key=a[mid] ? NO low + high
Is key>a[mid] ? YES mid = ------------
2
Search to the right of mid
0 1 2 4 5 6 7 8 9
0 1 2 4 5 6 7 8 9
Element
found at
index 6
0 1 2 4 5 6 7 8 9
39
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Try this One!
Element to be Searched 12
0 1 2 4 5 6 7 8 9
40
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Is key=a[mid] ? NO low + high
mid = ------------
Is key>a[mid] ? YES 2
Search to the right of mid
0 1 2 4 5 6 7 8 9
41
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Is key=a[mid] ? NO
Is key>a[mid] ? YES
Search to the right of mid
0 1 2 4 5 6 7 8 9
42
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Is key=a[mid] ? NO low + high
mid = ------------
Is key>a[mid] ? YES 2
Search to the right of mid
0 1 2 4 5 6 7 8 9
43
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Is key=a[mid] ? NO
Is key>a[mid] ? YES
Search to the right of mid
0 1 2 4 5 6 7 8 9
44
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
low == high indicates that we have completed
the binary search process
Is key=a[mid] ? NO
0 1 2 4 5 6 7 8 9
LOW
45
Downloaded from Ktunotes.in
MID
Department of Computer Science & Engineering, Saintgits College of Engineering
Binary Search on Unsorted Array-
What Happens?
low + high
Element to be Searched 7 mid = ------------
2
5 1 2 7 IL
FA3 6 12 8 9
46
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
STACKS
47
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Introduction to Stacks
Every time an element is added, it goes on the top of the stack and the
only element that can be removed is the element that is at the top of
the stack, just like a pile of objects.
48
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Basic Features
2. Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
3. push() function is used to insert new elements into the Stack and pop() function is used
49
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack- Real World Example
50
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack- Quick Overview
PUSH POP
9 9
5 5
F L
I 8 8
I
L 3 3 F
O O
0 0
2 2
51
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Top of the Stack
We always push an element to the top of the stack
9 top=4
5 top=3
8 top=2
top=1
3
top=0
0
top=-1 52
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Top of the Stack
Both insertion and removal are allowed at only one end of Stack called Top.
9 top=4
5 top=3
8 top=2
top=1
3
top=0
0
top=-1 53
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Basic Functions
54
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Is_Full( ) Function
To check whether the stack is full or not.
55
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Is_Empty( ) Function
To check whether the stack is empty or not.
Check if top==-1
If YES, Return True (or 1)
Else Return False (or 0)
56
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Push ( ) Function
Syntax: stackname.push(value)
Parameters :
Result :
57
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Implementing Push ( ) Function
You can only push an element to the stack, if the stack is not full.
If the Is_Full Function returns False, then Push the element to the stack &
increment top.
Else Display the stack is full
58
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Pop ( ) Function
Syntax: stackname.pop()
Parameters :
Result :
Removes the newest element in the stack or basically the top element.
59
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Implementing Pop ( ) Function
You can only pop an element from the stack, if the stack is not empty.
If the Is_Empty Function returns False, then Pop an element from the stack
and decrement top.
Else Display the stack is empty
60
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Implementing a Stack
Using Arrays
61
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack Using an Array-
Applying Pop( ) Function
a[4] 4 top
a[3] 0
a[2] 9
a[1] 12
a[0] 5
Actual Array
Actual Stack
62
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack Using an Array
a[4] 4
a[3] 0 top
a[2] 9
a[1] 12
a[0] 5
Actual Array
Actual Stack
63
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack Using an Array
a[4] 4
a[3] 0
a[2] 9 top
a[1] 12
a[0] 5
Actual Array
Actual Stack
64
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack Using an Array
a[4] 4
a[3] 0
a[2] 9
a[1] 12 top
a[0] 5
Actual Array
Actual Stack
65
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack Using an Array
a[4] 4
a[3] 0
a[2] 9
a[1] 12
a[0] 5 top
Actual Array
Actual Stack
66
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack Using an Array
Applying Push( ) Function
a[4]
a[3]
a[2]
a[1]
a[0]
top=-1
Actual Array
Actual Stack
67
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack Using an Array
Applying Push( ) Function
a[4]
a[3]
a[2]
a[1]
a[0] 5 top=0
Actual Array
Actual Stack
68
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack Using an Array
Applying Push( ) Function
a[4]
a[3]
a[2]
a[1] 3 top=1
a[0] 5
Actual Array
Actual Stack
69
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack Using an Array
Applying Push( ) Function
a[4]
a[3]
a[2] 6 top=2
a[1] 3
a[0] 5
Actual Array
Actual Stack
70
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack Using an Array
Applying Push( ) Function
a[4]
a[2] 6
a[1] 3
a[0] 5
Actual Array
Actual Stack
71
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Stack Using an Array
Applying Push( ) Function
a[4] 47 top=4
a[3] -21
a[2] 6
a[1] 3
a[0] 5
Actual Array
Actual Stack
72
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Consider the following operation performed on a stack of
size 5.
73
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Displaying Stack
a[4]
a[3] 9
a[2] 3
a[1] -5 top=1
a[0] 8
Actual Array
Actual Stack
74
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queues
75
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Introduction to Queues
One end is always used to insert data (enqueue) and the other is used to
remove data (dequeue).
76
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queues in Real Life
77
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue- Quick Overview
2 6 5 0 8 7
FIFO LILO
2 6 5 0 8 7
78
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Basic Functions
79
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Front & Rear Data Pointers
We always insert an element to the rear of the queue
80
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Is_Full( ) Function
To check whether the queue is full or not.
81
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Is_Empty( ) Function
To check whether the queue is empty or not.
82
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Implementing a Queue
Using Arrays
83
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function
Front
Initially, Front = Rear = -1
Actual Array
Actual Queue Rear
84
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function
Rear Front
2 7
Rear Front
Actual Array
Actual Queue
87
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function
2 7
Rear Front
Actual Array
Actual Queue
88
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function
8 2 7
Rear Front
8 2 7
Rear Front
Actual Array
Actual Queue
90
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function
5 8 2 7
Rear Front
5 8 2 7
Rear Front
Actual Array
Actual Queue
92
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function
1 5 8 2 7
Rear Front
1 5 8 2 7
Rear Front
Actual Array
Actual Queue
94
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function
9 1 5 8 2 7
Rear Front
OW
9 1 V5ERFL 8 2 7
O
2 6 5 0 8 7
Rear Front
2 6 5 0 8 7
Rear Front
2 6 5 0 8 7
Rear Front
2 6 5 0 8 7
Rear Front
2 6 5 0 8 7
Rear Front
2 6 5 0 8 7
Rear
Front
W
ER FLO
2 6 5
D 0 8 7
UN
Rear
Front Rear < Front
Actual Array
Actual Stack
103
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Consider the following operation performed on a queue of
size 6.
104
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(5)
Front
Rear 105
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Front
Rear 106
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Dequeue( )
Front Rear
107
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Front
108
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(8)
Rear
Front
109
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(8)
Rear
Front
110
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(2)
2 8
Rear Front
111
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Dequeue( )
2 8
Rear
Front 112
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(-5)
-5 2 8
Rear Front
113
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(7)
7 -5 2 8
Rear Front
114
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(9)
9 7 -5 2 8
Rear Front
115
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Dequeue( )
9 7 -5 2 8
Rear Front
116
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Dequeue( )
9 7 -5 2 8
Rear Front
117
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(3)
3 9 7 -5 2 8
Rear Front
118
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution-Final Queue Content
Dequeue( )
3 9 7 -5 2 8
Rear Front
119
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Queues
120
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Basic Idea
121
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Front and Rear Pointers rear front
a[7] a[0]
6 9
a[6] a[1]
-3 10
a[5] 7 1
a[2]
17 -18
a[4] a[3]
122
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
a[7] a[0]
rear front
6 9
a[6] a[1]
-3 10
Actual Array
Actual Queue
a[5] 7 1 a[2]
17 -18
a[4] a[3]
rear front
6 -3 7 17 -18 1 10 9
a[M-1] ... a[7] a[6] a[5] a[4] a[3] a[2] a[1] a[0] 123
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Queue - Enqueue()
Function-Using Arrays
a[7] a[0]
a[6] a[1]
a[5]
a[2]
Initially,
a[4] a[3]
front=rear=-1
124
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Inserting the first Rear
element
Circular Queue - Enqueue()
a[7] a[0]
Function-Using Arrays
Front
9
a[6]
a[1]
a[5]
a[2]
Set front=0,
a[4] a[3] increment rear
125
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Inserting the first Rear
element
Circular Queue - Enqueue()
a[7] a[0]
Function-Using Arrays
Front
9
a[6]
a[1]
a[5]
a[2]
a[4] a[3]
126
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Queue - Enqueue()
a[7] a[0]
Function-Using Arrays
Front
9
a[6]
Rear
a[1]
a[5]
a[2]
a[4] a[3]
127
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Inserting a new
element
Circular Queue - Enqueue()
a[7] a[0]
Function-Using Arrays
Front
9
a[6]
Rear
10
a[1]
a[5]
a[2]
a[4] a[3]
128
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Queue - Enqueue()
a[7] a[0]
Function-Using Arrays
Front
9
a[6]
10
a[1]
Rear
a[5]
a[2]
a[4] a[3]
129
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Inserting a new
element
Circular Queue - Enqueue()
a[7] a[0]
Function-Using Arrays
Front
9
a[6]
10
a[1]
a[5]
1
a[2]
10
a[1]
a[5]
1
a[2]
-18
10
a[1]
a[5]
1
a[2]
-18
a[4] a[3]
Rear 132
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Inserting a new
element
Circular Queue - Enqueue()
a[7] a[0]
Function-Using Arrays
Front
9
a[6]
10
a[1]
a[5]
1
a[2]
17 -18
a[4] a[3]
Rear 133
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Inserting a new
element
Circular Queue - Enqueue()
a[7] a[0]
Function-Using Arrays
Front
9
a[6]
10
a[1]
a[5]
7 1
a[2]
17 -18
Rear
a[4] a[3]
134
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Inserting a new
element
Circular Queue - Enqueue()
a[7] a[0]
Function-Using Arrays
Front
9
a[6]
Rear -3 10
a[1]
a[5]
7 1
a[2]
17 -18
a[4] a[3]
135
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Queue - Enqueue()
Rear
a[7] a[0]
Function-Using Arrays
Front
6 9
a[6]
-3 10
a[1]
a[5]
7 1
a[2]
17 -18
Inserting a new
a[4] a[3] element
136
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Queue - Enqueue() Rear
a[7] a[0]
Function-Using Arrays
Front
6 9
a[6]
-3 10
a[1]
a[5]
7
LOW
1
RF
OVE
a[2]
17 -18
Inserting a new
a[4] a[3] element
137
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Enqueue() Operation
enQueue(value) This function is used to insert an element into the circular
queue. In a circular queue, the new element is always inserted at Rear
position.
138
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Alternate Statements- Enqueue( )
INSTEAD OF rear ++
139
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Queue - Dequeue() Function
Using Arrays
140
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
a[7] a[0]
rear front Deleting an
6 9 element
a[6] a[1]
-3 10
Actual Array
Actual Queue
a[5] 7 1 a[2]
17 -18
a[4] a[3]
rear front
6 -3 7 17 -18 1 10 9
a[M-1] ... a[7] a[6] a[5] a[4] a[3] a[2] a[1] a[0] 141
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
a[7] a[0]
rear Deleting an
6 9 front element
a[6] a[1]
-3 10
Actual Array
Actual Queue
a[5] 7 1 a[2]
17 -18
a[4] a[3]
rear front
6 -3 7 17 -18 1 10 9
a[M-1] ... a[7] a[6] a[5] a[4] a[3] a[2] a[1] a[0] 142
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
a[7] a[0]
rear Deleting an
6 9 element
a[6] a[1]
-3 10
Actual Array
Actual Queue
front
a[5] 7 1 a[2]
17 -18
a[4] a[3]
rear front
6 -3 7 17 -18 1 10 9
a[M-1] ... a[7] a[6] a[5] a[4] a[3] a[2] a[1] a[0] 143
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
a[7] a[0]
rear Deleting an
6 9 element
a[6] a[1]
-3 10
Actual Array
Actual Queue
a[5] 7 1 a[2]
17 -18
a[4] a[3]
front
rear front
6 -3 7 17 -18 1 10 9
a[M-1] ... a[7] a[6] a[5] a[4] a[3] a[2] a[1] a[0] 144
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
a[7] a[0]
rear Deleting an
6 9 element
a[6] a[1]
-3 10
Actual Array
Actual Queue
a[5] 7 1 a[2]
17 -18
a[4] a[3]
6 -3 7 17 -18 1 10 9
a[M-1] ... a[7] a[6] a[5] a[4] a[3] a[2] a[1] a[0] 145
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
a[7] a[0]
rear Deleting an
6 9 element
a[6] a[1]
-3 10
Actual Array
Actual Queue
a[5] 7 1 a[2]
17 -18
a[4] a[3]
rear front
front
6 -3 7 17 -18 1 10 9
a[M-1] ... a[7] a[6] a[5] a[4] a[3] a[2] a[1] a[0] 146
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
a[7] a[0]
rear Deleting an
6 9 element
a[6] a[1]
-3 10
Actual Array
front Actual Queue
a[5] 7 1 a[2]
17 -18
a[4] a[3]
front
rear
6 -3 7 17 -18 1 10 9
a[M-1] ... a[7] a[6] a[5] a[4] a[3] a[2] a[1] a[0] 147
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
a[7] a[0]
front Deleting an
rear 6 9 element
a[6] a[1]
-3 10
Actual Array
Actual Queue
a[5] 7 1 a[2]
17 -18
a[4] a[3]
front
rear
6 -3 7 17 -18 1 10 9
a[M-1] ... a[7] a[6] a[5] a[4] a[3] a[2] a[1] a[0] 148
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
a[7] a[0]
Reset
6 9 front=rear=-1
a[6] a[1]
-3 10
Actual Array
Actual Queue
a[5] 7 1 a[2]
17 -18
a[4] a[3]
6 -3 7 17 -18 1 10 9
a[M-1] ... a[7] a[6] a[5] a[4] a[3] a[2] a[1] a[0] 149
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Dequeue() Operation
deQueue() This function is used to delete an element from the circular queue.
In a circular queue, the element is always deleted from front position.
150
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Alternate Statement- Dequeue( )
INSTEAD OF front++
151
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Advantage of Circular Queue
152
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Advantage of Circular
Rear
a[7] a[0]
6
a[6]
Queue
-3
a[1]
Front
a[5]
7 1
a[2]
17 -18
Inserting a new
a[4] a[3] element
153
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Advantage of Circular
Rear
a[7] a[0]
6
a[6]
Queue
-3
a[1]
Front
a[5]
7 1
a[2]
17 -18
Not Possible in a
a[4] a[3] Normal Queue
154
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Rear
Advantage of Circular
a[7] a[0]
6
a[6]
Queue
-3
a[1]
Front
a[5]
7 1
a[2]
17 -18
In Circular Queue,re-
a[4] a[3]
initialize Rear to 0
155
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Rear
Advantage of Circular
a[7] a[0]
6
a[6] 8
Queue
-3
a[1]
Front
a[5]
7 1
a[2]
17 -18
In Circular Queue,re-
a[4] a[3]
initialize Rear to 0
156
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Advantage of Circular
a[7] a[0]
6 Rear
a[6] 8
Queue
-3
a[1]
Front
a[5]
7 1
a[2]
17 -18
Rear++
a[4] a[3]
157
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Advantage of Circular Circular Queue avoids memory wastage
a[7] a[0]
6 Rear
a[6] 8
Queue
-3 5
a[1]
Front
a[5]
7 1
a[2]
17 -18
a[4] a[3]
158
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Same Scenario in a Linear Queue
rear front
6 -3 7 17 -18 1
a[M-1] ... a[7] a[6] a[5] a[4] a[3] a[2] a[1] a[0]
Actual Array
Actual Queue
Wasted Memory
159
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Consider the following operation performed on a circular
queue of size 5.
Enqueue(5) Enqueue(10)
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
a[4] a[0]
9 20
Solution
Rear
a[3] -6 5 a[1]
Queue is full 13
10 is not inserted
a[2] Front
161
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
DOUBLE ENDED QUEUE
162
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Double Ended Queue- Deque
A deque, also known as a double-ended queue, is an ordered collection of
items similar to the queue.
It has two ends, a front and a rear, and the items remain positioned in the
collection.
New items can be added at either the front or the rear. Likewise, existing
163
items can be removedDownloaded
from either end.
from Ktunotes.in
Department of Computer Science & Engineering, Saintgits College of Engineering
Deque Operations
insert_front( ) insertion through the front
164
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Front & Rear Pointers- Deque
166
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
167
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
Front
Insert from the Rear
Actual Array
Actual Queue Rear
168
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
5 8
Rear Front
169
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
7 5 8
Rear Front
170
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
7 5 8
Rear Front
171
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
-6 7 5 8
Rear Front
172
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
-6 7 5 8
Rear Front
173
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
-6 7 5 8
Rear Front
Actual Array
Actual Queue
174
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
-6 7 5 6
Rear Front
Actual Array
Actual Queue
175
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
-6 7 5 6
Rear Front
176
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
-6 7 5 6
Rear Front
177
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
-6 7 5 6
Rear Front
178
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue
8 -6 7 5 6
Front Rear
179
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Visualization of Deque
a[5] a[0]
a[4] a[1]
Front
8
a[4] a[1]
Rear
5
a[4] a[1]
5
a[4] a[1]
a[3] Rear
a[2]
183
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Visualization of Deque
a[5] a[0]
Front
5
a[4] a[1]
7
Delete from the Front
a[3] Rear
a[2]
184
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Visualization of Deque
a[5] a[0]
Front
5
a[4] a[1]
-6 7
Rear
Insert from the Rear
a[3]
a[2]
185
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Visualization of Deque
a[5] a[0]
Front
5
a[4] a[1]
-6 7
Rear
Insert from the Front
a[3]
a[2]
186
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Visualization of Deque
a[5] a[0]
Front
5
a[4] a[1]
-6 7
Delete from the Rear
a[3] Rear
a[2]
187
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Visualization of Deque
Front a[5] a[0]
6
8
5
a[4] a[1]
-6 7
Insert from the Front
a[3] Rear
a[2]
188
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Consider the following operation performed on a double ended
queue of size 6.
189
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
After performing these set of operations, what does the final
list contain?
Insert_Front(10);
Insert_Front(20);
Insert_Rear(30);
Delete_Front();
Insert_Rear(40);
Insert_Rear(10);
Delete_Rear();
Insert_Rear(15);
display(); 190
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Figure 1 shows the current state of a particular data structure X, implemented with
the help of an array of size N. The size of the data structure X is not provided.
5 0 3 2 1
Rear Front
Figure 1
191
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The following operations were performed in the given order and the final contents
of the array is as shown in Figure 2.
Add (-13)
Add (12)
Remove ( )
Remove ( )
Add (0)
The Add ( ) Function inserts an element to X and the Remove( ) function removes
an element from X. It is not mentioned whether Insertion or Removal of element
happens from the front or rear or both. It is left to you to identify ,using the final
array contents.
192
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Final Array Content
a[N-1] ... a[5] a[4] a[3] a[2] a[1] a[0]
-13 5 0 3 0 12
Front Rear
Figure 2
193
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Expression Notations- Equivalent Forms
194
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Types of Expression
Postfix Notation
Prefix Notation
Used to represent the
same arithmetic
Infix Notation expression
195
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Postfix Notation
(Reverse Polish Notation)
An expression is called the postfix expression if the operator appears in
the expression after the operands. Simply of the form (operand1
operand2 operator).
Example : AB+CD-*
AB+
AB+DE*
196
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Prefix Notation
Example : *+AB-CD
197
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Infix Notation
Example: A+B-C*D
B*C+G-F
198
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Equivalent Notations
(A + B) * C – D –*+ABCD AB+C*D–
199
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Infix to Postfix Conversion
Using Stacks
200
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Procedure
If the incoming symbol is ‘)’ then pop the stack and print the operators
until ‘(‘ is found.
201
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Procedure
If the incoming symbol has higher precedence than the top of the stack
then push the symbol onto the stack.
If incoming symbol has lower precedence than the top of the stack then
pop and print the top.Then test the incoming operator against the new
stack top.
At the end of the expression, pop and print all operators in the stack.
If associativity is L to R, then pop and print the top of the stack and then
push the incoming operator.
203
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
A Simple Example
A+B
A + B
Stack
204
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
A Simple Example
A+B
A + B
Stack
205
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
A Simple Example
A+B
A + B ...
Stack
206
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
A Simple Example
A+B
A + B
A
B
Stack
207
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
A Simple Example
A+B
A + B
AB
+
Stack
208
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
A+B*C+D
A + B * C + D
Stack
209
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
A+B*C+D
A + B * C + D
Stack
210
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
A+B*C+D
A + B * C + D
Stack
211
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
A+B*C+D
A + B * C + D
AB
Stack
212
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
A+B*C+D
A + B * C + D
AB
Stack
213
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
A+B*C+D
A + B * C + D
ABC
Stack
214
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
A+B*C+D
A + B * C + D
ABC*
Stack
215
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
A+B*C+D
A + B * C + D
ABC*+
Stack
216
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
A+B*C+D
A + B * C + D
ABC*+
D
Stack
217
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
A+B*C+D
A + B * C + D
ABC*+D
+
Stack
218
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-2
A*B+C*D A * B + C * D
Stack
219
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-2
A*B+C*D A * B + C * D
Stack
220
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-2
A*B+C*D A * B + C * D
Stack
221
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-2
A*B+C*D A * B + C * D
AB
Stack
222
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-2
A*B+C*D A * B + C * D
AB*
Stack
223
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-2
A*B+C*D A * B + C * D
AB*
Stack
224
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-2
A*B+C*D A * B + C * D
AB*C
Stack
225
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-2
A*B+C*D A * B + C * D
AB*C
Stack
226
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-2
A*B+C*D A * B + C * D
AB*CD
Stack
227
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-2
A*B+C*D A * B + C * D
AB*CD*
Stack
228
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-2
A*B+C*D A * B + C * D
AB*CD*+
Stack
229
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
Stack
230
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
Stack
231
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
Stack
232
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
Stack
233
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
AB
Stack
234
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
AB+
Stack
235
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
AB+
Stack
236
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
AB+
Stack
237
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
AB+
Stack
238
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
AB+C
Stack
239
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
+ AB+C
Stack
240
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
+ AB+CD
Stack
241
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
AB+CD+
Stack
242
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
AB+CD+
Stack
243
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-3
(A + B) * (C + D)
( A + B ) * ( C + D )
AB+CD+*
Stack
244
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Homework Questions
Convert the following infix expressions to Postfix notation.
1. a+b*c+(d*e)
2. a-b-c
3. a/b^c-d
4. a+(b*c(d/e^f)*g)*h)
245
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Infix to Prefix Conversion-
Procedure
While reversing, each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘.
246
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
Infix Expression: A * B + C / D
249
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Procedure
Begin
A Top Element
Result = B Operator A
251
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +
5
252
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +
3
5
253
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +
A=3
5
254
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +
A=3
B=5
255
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +
RESULT = B + A
256
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +
RESULT = 8
8
257
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +
258
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
259
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
2
260
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
3
2
261
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
3
2
262
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
A=1
3
2
263
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
A=1
B=3
2
264
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
RESULT= 3 * 1 = 3
2
265
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
3
2
266
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
A=3
2
267
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
A=3
B=2
268
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
RESULT= 2 + 3 = 5
269
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
5
270
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
9
5
271
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
A=9
5
272
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
A=9
B=5
273
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
RESULT= 5 - 9 = -4
274
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
-4
275
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -
276
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
From the Vault-
Cracking GATE Questions
277
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
GATE CS - 2004
A. abc × + def ^ ^ -
B. abc × + de ^ f ^ -
C. ab + c × d - e ^ f ^
D. - + a × bc ^ ^ def
278
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
GATE CS - 2015
A. 284
B. 213
C. 142
D. 71
279
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering