0% found this document useful (0 votes)
33 views280 pages

CST201 M2 Ktunotes - in

Uploaded by

Arathy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views280 pages

CST201 M2 Ktunotes - in

Uploaded by

Arathy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 280

ARRAYS & SEARCHING

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

Polynomial representation using Arrays


Sparse matrix
Stacks
Queues
Circular Queues
Priority Queues
Double Ended Queues
Evaluation of Expressions
Linear Search and Binary Search 2
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
2000 8
Introduction to Arrays
2004 9

2008 2
8 9 2 1 0 7 6 2
2012 1

2016 0

2020 7 2000 2001 2002 2003

2024 6 00000000 00000000 00000000 00001000

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.

i: Index of the element.

L: Lower bound of the array.

U: Upper bound of the array.

P: Position of element in the array.

W: Word Size of the Array 4


Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
1-Dimensional Arrays
2000

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

There are 80 Elements in Array A


7
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
(b) How much memory is required to store the entire array?
Total memory required = Size * W
Total memory required = 80 * 2
Total memory required = 160 bytes

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

Address = 459 + (50 + 15) * 2


Address = 459 + (65) * 2
Address = 459 + 130
Address = 589
9
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
d) What is the address location of 10th element?
P = 10
Index or i = L + P -1, Address = M + (i–L) * W
i = -15 + 10 -1
i = -16 + 10
i = -6
Address = 459 + (-6–(-15)) * 2
Address = 459 + (-6 + 15) * 2
Address = 459 + (9) * 2
Address = 459 + 18
10
Department of
Address =
Downloaded
477 Saintgits College of Engineering
from Ktunotes.in
Computer Science & Engineering,
Solution
e) Which element is located at address 599?

71st element with index 55 is located on address 599

11
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
2-D Arrays (Matrices)

a11 a12 a13 a14

a21 a22 a23 a24

a31 a32 a33 a34

a41 a42 a43 a44

12
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Linear Memory Representation
Row Major Order

In row major order all elements of a matrix are stored on a row-by


row basis , that is all elements in first row followed by second row and
so on.

Column Major Order

In column major order all elements of a matrix are stored on a


column-by column basis , that is all elements in first column followed
by second column and so on. 13
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Memory Representation
2000 a11 9 4000 a11

2004 a12 2 4004 a21

2008 a13 1 4008 a31

2012 a21 6 4012 a12


7
2016 a22 4016 a22
3
2020 a23 4020 a32
0
2024 a31 4024 a13
-2
2028 a32 4028 a23
8
2032 a33 4032 a33
14
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Diagrammatic Representation

a11 a12 a13 a11 a12 a13

a21 a22 a23 a21 a22 a23

a31 a32 a33 a31 a32 a33

Provides a Linear Ordering


15
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Array Operation- 9 A[L] 9
Insertion 0 0

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

5x2 + 3x +7 5x2 + 3x1 +7x0

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;
};

5x2 struct poly a[50]

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

a[0] a[1] a[2] a[3]

2x2+3x+7 0x3+2x2+3x+
7
7 3 2 0

b[0] b[1] b[2] b[3]

4x3-3x2+5x+14
14 5 -3 4

c[0] c[1] c[2] c[3] 21


Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Application of Arrays-Sparse Matrices

A sparse matrix or sparse array is a matrix in which many or most


of the elements are zero. The converse is known as a dense matrix.

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.

Instead of storing zeroes with non-zero elements, we only store non-zero


elements.

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

A 2D Array is used to represent a sparse matrix. It has 3 rows:


a) Row - Index of row where non-zero element is present
b) Column- Index of column where non-zero element is present
c) Value - Value of the non-zero element

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

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

NO! NO! NO! NO! NO! NO! YES!

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

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

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.

Uses the Divide & Conquer Approach

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.

1. Compare key with the middle element.


2. If key matches with middle element, we return the mid index.
3. Else If key is greater than the mid element, then key can only lie in right half
subarray after the mid element. So we recur for right half.
4. Else (key is smaller) recur for the left half.

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

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

LOW MID HIGH

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

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

LOW MID HIGH


Set low =mid +1
36
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] ? NO mid = ------------
2
Search to the left of mid

0 1 2 4 5 6 7 8 9

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

MID LOW MID HIGH


Set high =mid-1
37
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Binary Search

Is key=a[mid] ? YES low + high


mid = ------------
2
Return index of mid
0 1 2 4 5 6 7 8 9

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

LOW HIGH MID


MID
38
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Binary Search

Element
found at
index 6

0 1 2 4 5 6 7 8 9

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

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

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

Find the Stopping Condition

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

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

LOW MID HIGH

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

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

MID LOW HIGH

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

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

LOW MID HIGH

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

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

LOW MID HIGH

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

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

Condition: low should be less than high


HIGH
( low < high )

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

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

LOW MID HIGH

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

Stack is an abstract data type with a bounded(predefined) capacity.

It is a simple data structure that allows adding and removing elements


in a particular order.

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

1. Stack is an ordered list of similar data type.

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

to remove an element from the stack.

4. Stack is said to be in Overflow state when it is completely full and is said to be in

Underflow state if it is completely empty.

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

We always pop the element from 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

We always push an element to the top of the stack

We always pop the element from 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

Push ( ) - Push an element to the stack


Pop ( ) - Pop an element from the stack
IsEmpty ( ) - Check if the stack is empty
IsFull ( ) - Check if the stack is full
Peek ( ) - Return the Top of the Stack

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.

Check if top==N-1; where N is the size of the stack


If YES, Return True (or 1)
Else Return False (or 0)

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 :

The value of the element to be inserted is passed as the parameter.

Result :

Adds an element of specified value at the top of the stack.

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 :

No parameters are passed.

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

Using Linked Lists

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[3] -21 top=3

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.

Push (5) Pop( ) Push (8) Push(2)

Pop( ) Push (-5) Push (7) Push (9)

Pop( ) Pop( ) Push (3) Pop( )

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

Queue is an abstract data structure, somewhat similar to Stacks. Unlike


stacks, a queue is open at both its ends.

One end is always used to insert data (enqueue) and the other is used to
remove data (dequeue).

Queue follows First-In-First-Out methodology, i.e., the data item stored


first will be accessed first

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

Enqueue ( ) - Insert an element to the queue


Dequeue ( ) - Remove an element from the queue
IsEmpty ( ) - Check if the queue is empty
IsFull ( ) - Check if the queue is full
Peek ( ) - Return first element of the queue

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

We always delete the element from the front of the queue.

a[N-1] ... a[4] a[3] a[2] a[1] a[0]

Insert elements through Rear Delete elements through Front

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.

Check if rear==N-1; where N is the size of the queue


If YES, Return True (or 1)
Else Return False (or 0)

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.

Check if front==-1 or rear<front


If YES, Return True (or 1)
Else Return False (or 0)

82
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Implementing a Queue

Using Arrays

Using Linked Lists

83
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

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

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

Inserting First Element Front=0, Front


Rear++
Actual Array
Actual Queue Rear
85
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

Rear Front

Actual Array Inserting an Element


Actual Queue Rear++
86
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

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

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

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

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

8 2 7

Rear Front

Actual Array Inserting an Element


Actual Queue Rear++
89
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

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

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

5 8 2 7

Rear Front

Actual Array Inserting an Element


Actual Queue Rear++
91
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

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

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

1 5 8 2 7

Rear Front

Actual Array Inserting an Element


Actual Queue Rear++
93
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

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

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

9 1 5 8 2 7

Rear Front

Actual Array Inserting an Element


Actual Queue Rear++
95
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Enqueue( ) Function
Trying to insert new
element

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

OW
9 1 V5ERFL 8 2 7
O

Rear Given N(size of queue) = 6 Front

Actual Array If Rear>= N-1 Overflow Condition


Actual Queue
96
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Dequeue( ) Function

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

2 6 5 0 8 7

Rear Front

Actual Array Deleting an Element


Actual Queue Front++
97
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Dequeue( ) Function

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

2 6 5 0 8 7

Rear Front

Actual Array Deleting an Element


Actual Queue Front++
98
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Dequeue( ) Function

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

2 6 5 0 8 7

Rear Front

Actual Array Deleting an Element


Actual Queue Front++
99
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Dequeue( ) Function

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

2 6 5 0 8 7

Rear Front

Actual Array Deleting an Element


Actual Queue Front++
100
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Dequeue( ) Function

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

2 6 5 0 8 7

Rear Front

Actual Array Deleting an Element


Actual Queue Front++
101
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Dequeue( ) Function

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

2 6 5 0 8 7

Rear
Front

Actual Array Deleting an Element


Actual Queue Front++
102
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Queue Using an Array-
Applying Dequeue( ) Function
Trying to delete an element

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

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.

Enqueue(5) Dequeue( ) Enqueue(8) Enqueue(2)

Dequeue( ) Enqueue(-5) Enqueue(7) Enqueue(9)

Dequeue( ) Dequeue( ) Enqueue(3) Dequeue( )

104
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(5)

a[5] a[4] a[3] a[2] a[1] a[0]

Front

Rear 105
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution

a[5] a[4] a[3] a[2] a[1] a[0]

Front

Rear 106
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Dequeue( )

a[5] a[4] a[3] a[2] a[1] a[0]

Front Rear

107
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution

a[5] a[4] a[3] a[2] a[1] a[0]

Since Rear<Front; Rear


Re Initialize rear,front to -1

Front
108
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(8)

a[5] a[4] a[3] a[2] a[1] a[0]

Rear

Front
109
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(8)

a[5] a[4] a[3] a[2] a[1] a[0]

Rear

Front
110
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(2)

a[5] a[4] a[3] a[2] a[1] a[0]

2 8

Rear Front

111
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Dequeue( )

a[5] a[4] a[3] a[2] a[1] a[0]

2 8

Rear

Front 112
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(-5)

a[5] a[4] a[3] a[2] a[1] a[0]

-5 2 8

Rear Front

113
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(7)

a[5] a[4] a[3] a[2] a[1] a[0]

7 -5 2 8

Rear Front

114
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(9)

a[5] a[4] a[3] a[2] a[1] a[0]

9 7 -5 2 8

Rear Front

115
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Dequeue( )

a[5] a[4] a[3] a[2] a[1] a[0]

9 7 -5 2 8

Rear Front

116
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Dequeue( )

a[5] a[4] a[3] a[2] a[1] a[0]

9 7 -5 2 8

Rear Front

117
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Solution
Enqueue(3)

a[5] a[4] a[3] a[2] a[1] a[0]

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( )

a[5] a[4] a[3] a[2] a[1] a[0]

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

Circular Queue is a linear data structure in which the operations are


performed based on FIFO (First In First Out) principle and the last
position is connected back to the first position to make a circle.

In a normal Queue, we can insert elements until queue becomes full.


But once queue becomes full, we can not insert the next element even
if there is a space in front of queue.

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]

a[4] a[3] Rear


130
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]
-18

a[4] a[3] Rear


131
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]
-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.

1. Check whether queue is Full – Check ((rear == SIZE-1 && front == 0) ||


(rear == front-1)).
2. If it is full then display Queue is full. If queue is not full then, check if
(rear == SIZE – 1 && front != 0) if it is true then set rear=0 and insert
element.

138
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Alternate Statements- Enqueue( )

To check if circular queue is full or not


If ( front == (rear+1)%SIZE)
YOU CAN USE

If ((rear == SIZE-1 && front == 0) || (rear ==


INSTEAD OF
front-1))

To increment the rear pointer


YOU CAN USE
rear=(rear+1)%SIZE

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]

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] 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.

1.Check whether queue is Empty ie. check (front==-1).


2.If it is empty then display Queue is empty. If queue is not empty then step
3
3.Check if (front==rear) if it is true then set front=rear= -1 else check if
(front==SIZE-1), if it is true then set front=0 and return the element.

150
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Alternate Statement- Dequeue( )

To increment the front pointer


YOU CAN USE
front=(front+1)%SIZE

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(14) Enqueue(22) Enqueue(13) Enqueue(-6)

Dequeue( ) Dequeue( ) Enqueue(9) Enqueue(20)

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.

What makes a deque different is the unrestrictive nature of adding and


removing items.

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

insert_rear( ) insertion through the rear

delete_front( ) deletion from the front

delete_rear( ) deletion from the rear

is_empty( ) check if queue is empty

is_full( ) check if queue is full

164
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Front & Rear Pointers- Deque

a[N-1] ... a[4] a[3] a[2] a[1] a[0]

Insert & deletion of elements Insertion & Deletion of elements


through Rear through Front

Has both the properties of a stack and a queue

Can be implemented using a circular array


165
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Variations of Deque
Insertion
Deletion

Input Restricted Deque


a[N-1] ... a[4] a[3] a[2] a[1] a[0]

Output Restricted Deque


a[N-1] ... a[4] a[3] a[2] a[1] a[0]

166
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

Initially, front = rear =-1

167
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

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

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

5 8

Rear Front

Insert from the Rear

169
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

7 5 8

Rear Front

Insert from the Rear

170
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

7 5 8

Rear Front

Delete from the Front

171
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

-6 7 5 8

Rear Front

Insert from the Rear

172
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

-6 7 5 8

Rear Front

Insert from the Front Perform Front--

173
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

-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

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

-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

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

-6 7 5 6

Rear Front

Delete from the Rear Perform Rear--

176
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

-6 7 5 6

Rear Front

Delete from the Rear Perform Rear--

177
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

-6 7 5 6

Rear Front

Insert from the Front Perform Front=N-1

178
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Understanding the Operations
Circular Array
Actual Queue

a[6] a[5] a[4] a[3] a[2] a[1] a[0]

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]

Insert from the Rear


a[3]
a[2]
180
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Visualization of Deque
Rear
a[5] a[0]

Front
8

a[4] a[1]

Insert from the Rear


a[3]
a[2]
181
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Visualization of Deque
a[5] a[0]
Front

Rear

5
a[4] a[1]

Insert from the Rear


a[3]
a[2]
182
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Circular Visualization of Deque
a[5] a[0]
Front
Insert from the Rear
8

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.

Insert_Rear(5) Insert_Rear(8) Insert_Rear(2)

Insert_Front(5) Delete_Rear( ) Delete_Front( )

Insert_Rear(4) Delete_Rear( ) Insert_Front(2)

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.

a[N-1] ... a[5] a[4] a[3] a[2] a[1] a[0]

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

You are required to identify the data structure X.

What is the size of X?

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

An expression is called the prefix expression if the operator appears in


the expression before the operands. Simply of the form (operator
operand1 operand2).

Example : *+AB-CD

197
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Infix Notation

Infix notation is the notation commonly used in arithmetical and logical


formulae and statements. It is characterized by the placement of
operators between operands—"infixed operators”.

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

INFIX PREFIX POSTFIX

A+B +AB AB+

A+B–C –+ABC AB+C–

(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

Print operands as they arrive

If the stack is empty or contains a left parenthesis on the top,push the


incoming operator into the stack.

If the incoming symbol is’(‘ then push it onto the stack.

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.

If incoming operator has same precedence as of the top of the stack


then use associativity rules.
202
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Procedure

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.

If associativity is R to L 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

Step 1: Reverse the infix expression.

While reversing, each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘.

Step 2: Obtain the postfix expression of the modified expression


Step 3: Reverse the postfix expression

246
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1

Infix Expression: A * B + C / D

Reversed Infix Expression: D / C + B * A

Postfix Expression: CB*A+

Prefix Expression: +A*BC


247
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-2

Infix Expression: (A-B/C) * (A/D-E)

Reversed Infix Expression: (E-D/A)*(C/B-A)

Postfix Expression: EDA/-CB/A-*

Prefix Expression: *-A/BC-/ADE


248
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Evaluation of Postfix Expression
Using Stacks

249
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
The Procedure
Begin

for each character in postfix expression, do

if operand is encountered, push it onto stack

else if operator is encountered, pop 2 elements

A Top Element

B Next Top Element

Result = B Operator A

Push Result on to Stack

End Downloaded from Ktunotes.in


250
Department of Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +

Enter Postfix Expression


53+

251
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +

Enter Postfix Expression


53+

5
252
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +

Enter Postfix Expression


53+

3
5
253
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +

Enter Postfix Expression


53+

A=3

5
254
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +

Enter Postfix Expression


53+

A=3

B=5

255
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +

Enter Postfix Expression


53+

RESULT = B + A

256
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +

Enter Postfix Expression


53+

RESULT = 8

8
257
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Simple Example
53+
5 3 +

Enter Postfix Expression


53+
Postfix Evaluation Result
8

258
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -

Enter Postfix Expression


231*+9-

259
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering
Practice Problem-1
231*+9-
2 3 1 * + 9 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+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 -

Enter Postfix Expression


231*+9-
Postfix Evaluation Result
-4

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

Assume that the operators +, -, × are left associative and ^ is right


associative. The order of precedence (from highest to lowest) is ^, x ,
+, -. The postfix expression corresponding to the infix expression a +
b × c - d ^ e ^ f is :

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

The result evaluating the postfix expression


10 5 + 60 6 / * 8 – is

A. 284

B. 213

C. 142

D. 71

279
Downloaded
Department of from Ktunotes.in
Computer Science & Engineering, Saintgits College of Engineering

You might also like