0% found this document useful (0 votes)
12 views183 pages

CS23312 - DS - Unit - 1

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)
12 views183 pages

CS23312 - DS - Unit - 1

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/ 183

Department of

Computer Science and Engineering

CS23312-DATA STRUCTURES
Unit-1

Faculty Name : Mr.V.Madhan


Assistant Professor

Department of CSE
Rajalakshmi Institute of Technology
DATA STRUCTURES
Prerequisite Courses :
GE23121 Problem Solving using C
CO Course Outcomes Knowl
Nos. edge
Level

CO1 Design modular algorithms to find solution for computational K3


problem with time and space complexities using suitable linear
data structure..
CO2 Demonstrate the familiarity with tree data structure, rule to K3
manipulate those, and their canonical applications

CO3 Solve unstructured problem using balanced search tree algorithm, K3


hashing function.
CO4 Implement an and
appropriate
Project algorithm using graph ADT for an K3
application. Management
(SEPM)
CO5 Apply sorting and searching techniques for real world problems. K3

Department of Computer Science and Engineering 2


DATA STRUCTURES CONTENTS
UNIT I LINEAR DATA STRUCTURE L – 9
Abstract Data Types (ADTs) — List ADT — Array-based implementation — Linked list
implementation —-Singly linked lists- Circularly linked lists- Doubly-linked lists —
Applications of lists –Polynomial ADT — Radix Sort—Multi lists.
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES– 9
Stack ADT: Operations — Applications — Evaluating arithmetic expressions- Conversion of
Infix to postfix and prefix expressions — Function Calls—Queue ADT: Operations — Circular
Queue— dequeue — Applications of queues.
UNIT III SPECIAL TREES & HASHING L – 9
Tree ADT — Tree traversals — Binary Tree ADT — Expression trees — Binary Search Tree
ADT –AVL Trees — Priority Queue (Heaps) – Binary Heap—Applications of trees.
UNIT IV GRAPH L – 9
B Tree — B+ Tree— Graph Definition and Representation — Types of graph — Breadth First
Traversal — Depth First Traversal —Bi-connectivity — Cut vertex — Euler circuits —
Topological Sort — Shortest Path Algorithm: Dijkstra’s Algorithm, Floyd - Warshall
algorithm— Minimum Spanning Tree: Prim's algorithm, Kruskal's algorithm- Applications of
graphs.
and Project
UNIT V SORTING& SEARCHING L–9
Management
Searching- Linear Search — Binary
(SEPM)Search. Sorting — Bubble sort — Selection sort —
Insertion sort — Shell sort — Quick Sort —Merge Sort. Hashing: Hash Functions — Separate
Chaining — Open Addressing — Rehashing — Extendible Hashing.
Department of Computer Science and Engineering 3
DATA STRUCTURES

M. A. Weiss, “Data Structures and Algorithm Analysis in C”, Second


Edition, Pearson Education, 2007.

ii. Reference:
1. A. V. Aho, J. E. Hopcroft, and J. D. Ullman, “Data Structures and Algorithms”,
Pearson Education, First Editionand Reprint 2003.
Project
2. R. F. Gilberg, B. A. Forouzan, “Data Structures”, Second Edition, Thomson India Edition, 2005.
Management
3. Ellis Horowitz, SartajSahni, Dinesh Mehta, “Fundamentals of Data Structure”,
(SEPM)
Computer Science Press, 1995.

Department of Computer Science and Engineering 4


LINEAR DATA STRUCTURES

Introduction: Dynamic aspects of operations on data, Characteristics


of data, Creation, Manipulation and Operations on data, Data
Structure and its types, Abstract Data Types (ADTs), Analysis of
algorithms and its Types, Asymptotic Notation. Arrays: Allocation,
Operations and Storage with one-dimensional arrays and
multidimensional arrays. Stacks: Operations on Stacks, Applications
of Stacks, Queues: Operations and its types. Linked lists: Operations
and Types of linked list (singly, doubly and circularly linked list),
Implementation of Stack and Queue using linked list, Applications of
and Project
Management
Linked List. Case Study: Tower
(SEPM) of Hanoi, Sparse matrix.

Department of Computer Science and Engineering 5


WHY DATA STRUCTURES ?

❖It has a great practical importance to each students to solve the


real life problem in every moment knowing or unknowingly.
❖Knowledge of data structures is an extremely important part of
any programmer’s arsenal
❖Data structure helps the students to understand the logic of
computer and its related branch
❖Data structure tells us to make our programs efficient and fast to
execute.
❖Most of the top ITand
Company
Project
requirement is to be good in Data
Management
Structure and algorithms so why not today onwards to start learning
(SEPM)

the subject in best possible way with minimum efforts.


Department of Computer Science and Engineering 6
Compare these two pictures?

and Project
Management
(SEPM)

Department of Computer Science and Engineering 7


Compare these two pictures?

Class room Space efficiently used?


Take a particular chair or desk?

Department of Computer Science and Engineering 8


Our computer

Class room Space efficiently used?


Take a particular chair or desk?

Computer memory or storage


Computation speed

Department of Computer Science and Engineering 9


WHY DATA STRUCTURES ?

and Project
Management
(SEPM)

Department of Computer Science and Engineering 10


PROGRAM

A Set of Instructions
Data Structures + Algorithms
Data Structure = A Container stores Data
Algoirthm = Logic + Control

and Project
Management
(SEPM)

Department of Computer Science and Engineering 11


DATA

and Project
Management
(SEPM)

Department of Computer Science and Engineering 12


Analysis of Algorithms

How good is the algorithm?


• Correctness
• Time efficiency
• Space efficiency

and Project
Management
(SEPM)

Department of Computer Science and Engineering 13


TIME AND SPACE COMPLEXITY
•Time complexity : Time Complexity of an algorithm is the
representation of the amount of time required by the algorithm to
execute to completion.
•Best case - performs the minimum number of steps on input

•Worst case - performs the maximum number of steps on input

•Average case - performs an average number of steps on input

• Space complexity : Space complexity of an algorithm represents


and Project
Management
the amount of memory space needed the algorithm in its life cycle.
(SEPM)

Department of Computer Science and Engineering 14


ASYMPTOTIC NOTATION
•Mathematical notations used to describe the running time of an algorithm when
the input tends towards a particular value or a limiting value.
•Theta Notation (Θ-notation)
•represents the upper and the lower bound
•average case complexity
•Big-O Notation (O-notation)
•represents the upper bound
•worst case complexity
•Omega Notation (Ω-notation)
•represents the lower bound
•best case complexity

and Project
Management
(SEPM)

Department of Computer Science and Engineering 15


ANALYSIS
• Non Recursive: Summative method

and Project
Management
(SEPM)

Department of Computer Science and Engineering 16


ASYMPTOTIC NOTATION

and Project
Management
(SEPM)

Department of Computer Science and Engineering 17


Analysis of Nonrecursive (contd.)
ALGORITHM Mystery(n)
S <- 0
for i <- 1 to n do
S <- S + i×i
return S What does this algorithm compute?
What is the basic operation?

How many times is the basic operation executed?

What’s its efficiency class?


and Project
Management Can you improve it further, or
(SEPM)
Can you prove that no improvement
is possible?
Department of Computer Science and Engineering 18
Analysis of Nonrecursive (contd.)

and Project
Management
(SEPM)

Department of Computer Science and Engineering 19


Analysis of Nonrecursive (contd.)

and Project
Management
(SEPM)

Department of Computer Science and Engineering 20


O(1)

• It doesn’t contain loop, recursion

int temp = x;
x = y;
y = temp;

21
O(n)

• loop variables is incremented / decremented by


a constant amount
for (int i = 1; i <= n; i += c)
{
// some expressions
}

22
O(nc)
• nested loops is equal to the number of times the
innermost statement is executed
for (int i = 1; i <=n; i += c)
{
for (int j = 1; j <=n; j += c)
{
// some expressions
}
}
Selection sort and Insertion Sort have O(n2) time
complexity

23
O(Logn)
• The loop variables is divided / multiplied by a
constant amount
for (int i = 1; i <=n; i *= c)
{
// some expressions
}
for (int i = n; i > 0; i /= c)
{
// some expressions
}
Binary Search has O(Logn) time complexity.

24
DATA STRUCTURES

• Data structure is a particular way of storing and


organizing the data in systematic manner, so
that it can be retrieved efficiently.

• Logical or mathematical model of particular


organization of data
and Project
Management
(SEPM)

Department of Computer Science and Engineering 25


DATA STRUCTURES

and Project
Management
(SEPM)

Department of Computer Science and Engineering 26


CLASSIFICATION

DATA STRUCTURES

PRIMITIVE DATA STRUCTURES NON PRIMITIVE DATA STRUCTURES

INTEGER FLOAT CHAR LINEAR NON LINEAR

ARRAYS LINKED LIST STACK QUEUE TREES GRAPHS


and Project
Management
(SEPM)

Department of Computer Science and Engineering 27


DATA STRUCTURES

array

Linked list

and Project
Management
(SEPM)
queue
tree stack

Department of Computer Science and Engineering 28


Primitive Data Structure

predefined types of data

•Integer
•Float
•Character

and Project
Management
(SEPM)

Department of Computer Science and Engineering 29


Non Primitive Data Structure
not defined by the programming language

Linear Data Structures


•organizes the data in sequential order
•values are arrange in linear fashion.
Array: Fixed-size
Linked-list: Variable-size
Stack: Add to top and remove from top
Queue: Add to back and remove from front

Non - Linear Data Structures


•organizes the data in random order
Hash tables: Unordered lists which use a ‘hash function’ to insert
and search
Tree: Data is organized in branches.
and Project
Graph: A more general
Managementbranching structure, with less strict
connection conditions than for a tree
(SEPM)

Department of Computer Science and Engineering 30


ADT

•Abstract Data Types (ADTs) stores data and allow various


operations on the data to access and change it.

•A mathematical model, together with various operations


defined on the model

•An ADT is a collection of data and associated operations for


manipulating that data

and Project
Management
(SEPM)

Department of Computer Science and Engineering 31


The Core Operations of ADT
 Every Collection ADT should provide a
way to:
 add an item
 remove an item
 find, retrieve, or access an item

 Many, many more possibilities


 is the collection empty
 make the collection empty
and Project
 give me a sub set of the collection
Management
(SEPM)

Department of Computer Science and Engineering 32


ARRAYS
collection of items stored at contiguous memory locations

and Project
Management
(SEPM)

Department of Computer Science and Engineering 33


ARRAYS -ADVANTAGES

•allow random access of elements

•better cache locality

and Project
Management
(SEPM)

Department of Computer Science and Engineering 34


ARRAY - ADT
Insert Particular Position

and Project
Management
(SEPM)

Department of Computer Science and Engineering 35


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
a[i]=a[i-1];
}
a[p]=x;
n=n+1;
}

and Project
Management
(SEPM)

Department of Computer Science and Engineering 36


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 100 22 33 44 55

How?????

and Project
Management
(SEPM)

Department of Computer Science and Engineering 37


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 33 44 55

X=100
and Project
Management
(SEPM) p

Department of Computer Science and Engineering 38


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 33 44 55

X=100
and Project
Management
(SEPM) p i

Department of Computer Science and Engineering 39


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 33 44 55

X=100
and Project
Management
(SEPM) p i

Department of Computer Science and Engineering 40


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 33 44 55 55

X=100
and Project
Management
(SEPM) p i

Department of Computer Science and Engineering 41


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 33 44 55 55

X=100
and Project
Management
(SEPM) p i

Department of Computer Science and Engineering 42


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 33 44 44 55

X=100
and Project
Management
(SEPM) p i

Department of Computer Science and Engineering 43


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 33 44 44 55

X=100
and Project
Management
(SEPM) p i

Department of Computer Science and Engineering 44


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 33 44 44 55

X=100
and Project
Management
(SEPM) p i

Department of Computer Science and Engineering 45


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 33 33 44 55

X=100
and Project
Management
(SEPM) p i

Department of Computer Science and Engineering 46


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 33 33 44 55

X=100
and Project
Management
(SEPM) p i

Department of Computer Science and Engineering 47


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 22 33 44 55

X=100
and Project
Management
(SEPM) p i

Department of Computer Science and Engineering 48


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 22 33 44 55

X=100
and Project
Management
(SEPM) p i Next iteration i=1
Is not greater than 1

Department of Computer Science and Engineering 49


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 22 22 33 44 55

X=100
and Project
Management
(SEPM) p i

Department of Computer Science and Engineering 50


ARRAY - ADT
Insert Particular Position Input array a[6]
void insert(int a[],int x,int p) 0 1 2 3 4 5
{ 11 22 33 44 55
int i;
for(i=n;i>p;i--)
{
Insert(a,100,1)
a[i]=a[i-1];
}
a[p]=x;
0 1 2 3 4 5
n=n+1;
} 11 100 22 33 44 55

X=100
and Project
Management
(SEPM) p i

Department of Computer Science and Engineering 51


ARRAY - ADT

and Project
Management
(SEPM)

Department of Computer Science and Engineering 52


ARRAY - ADT

Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 22 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
n=n-1;
}

and Project
Management
(SEPM)

Department of Computer Science and Engineering 53


ARRAY - ADT

Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 22 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}

and Project
Management
(SEPM)

Department of Computer Science and Engineering 54


ARRAY - ADT

Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 22 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
} i

and Project
Management
(SEPM)

Department of Computer Science and Engineering 55


ARRAY - ADT

Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 22 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
} i

and Project
Management
(SEPM)

Department of Computer Science and Engineering 56


ARRAY - ADT

Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 33 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
} i

and Project
Management
(SEPM)

Department of Computer Science and Engineering 57


ARRAY - ADT

Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 33 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}
i

and Project
Management
(SEPM)

Department of Computer Science and Engineering 58


ARRAY - ADT

Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 33 44 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}
i

and Project
Management
(SEPM)

Department of Computer Science and Engineering 59


ARRAY - ADT

Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 33 44 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}
i

and Project
Management
(SEPM)

Department of Computer Science and Engineering 60


ARRAY - ADT

Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 33 44 55 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}
i

and Project
Management
(SEPM)

Department of Computer Science and Engineering 61


ARRAY - ADT

Delete Element
void delet(int a[],int p) 0 1 2 3 4 5
{ 11 100 33 44 55 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}
i

Next iteration i becomes 5


Which is not less than 5

and Project
Management
(SEPM)

Department of Computer Science and Engineering 62


ARRAY - ADT

Delete Element
void delet(int a[],int p) 0 1 2 3 4
{ 11 100 33 44 55
int i;
for(i=p;i<n-1;i++)
a[i]=a[i+1];
delet(a,2)
p
n=n-1;
}

Decrease size

and Project
Management
(SEPM)

Department of Computer Science and Engineering 63


Searching in an Unordered
Collection
• Let’s determine if the value 12 is in the
collection:

35 42 12 5 \\

12 Found!

64
Searching in an Unordered
Collection
• Let’s determine if the value 13 is in the
collection:

35 42 12 5

13 Not Found!

65
ARRAY - ADT

Search API
void search(int a[],int x)
{
int i;
for(i=0;i<n;i++)
{
if(x==a[i])
{
printf("Value is present in:%d\n\n\n",i);
break;
}
}
if(i==n)
printf(“Value not present”);
and Project
} Management
(SEPM)

Department of Computer Science and Engineering 66


LINKED LIST – WHY?
slow insertion in ordered array & Fixed size

and Project
Management
(SEPM)

Department of Computer Science and Engineering 67


ARRAY Vs LINKEDLIST

Array Linked list


•Element access is fast if •Element access is slow.
Index is known.
•Insertion and deletion •Insertion and
operations are slow. deletion operations are
•Element search is slow. fast.

and Project
•Element search is slow.
Management
(SEPM)

Department of Computer Science and Engineering 68


LINKEDLIST
• connected together via links.
• link contains a connection to another link.
• Linked list is the second most-used data structure after array.
Following are the important terms to understand the concept of
Linked List.
• Data − store a data called an element.
• Next − contains a link to the next link called Next.

and Project
Management
(SEPM)

Department of Computer Science and Engineering 69


Node Representation

• A node can be represented as


struct node Variable for
Pointer {
data part
Data Part * Part int data;
node *next;
}
Variable for pointer part
• Nodes with link can be represented as, (A pointer that will point next node)

8/17/2020 Department of Computer Science and Engineering 70


Types of Linked List

Singly Linked List

Doubly Linked List

Circular Linked List- Singly

Circular Linked List - Doubly

8/17/2020 Department of Computer Science and Engineering 71


Singly Linked List Representation

A singly Linked List can be represented as,

➢Each node contains a value and link (pointer or


reference) to next node
➢The last node contains a null link
➢The list will have a header at starting point
➢Reverse traversal is not possible

8/17/2020 Department of Computer Science and Engineering 72


Singly Linked List - FIND

Position find ( element_type x, LIST L )


{
position p;
p = L->next; Find(59,L)
while( (p != NULL) && (p->element != x) )
p = p->next;
L
return p; 40 48 55 59 63

}
p
Found !!!

8/17/2020 Department of Computer Science and Engineering 73


Singly Linked List - FIND

Position find ( element_type x, LIST L )


{
position p;
p = L->next; Find(99,L)
while( (p != NULL) && (p->element != x) )
p = p->next;
L
return p;
40 48 55 59 63
}
p

Reached End

Not found!!!
8/17/2020 Department of Computer Science and Engineering 74
Singly Linked List - FINDPREVIOUS
Position find_previous( element_type x, LIST L )
{
position p;
p = L;
while( (p->next != NULL) && (p->next->element != x) )
p = p->next; Findprevious(63,L)
return p; List

} p 40 48 55 59 63

p p p
p

Found !!!will return position of 59

8/17/2020 Department of Computer Science and Engineering 75


Insert the node into a list

List

40 48 59 63

p
X

newnode

Making link between the new node and the node after the tptr.
List

40 48 59 63

p
X

newnode

Making link between tptr and the new node


List

40 48 X 59 63

(f): Updated List


8/17/2020 Department of Computer Science and Engineering 76
Insert the node into a list

Void insert( element_type x, LIST L, position p )


{
position tmp_cell;
tmp_cell = (position) malloc( sizeof (struct node) );
if( tmp_cell == NULL ) insert(60,L,3)
fatal_error("Out of space!!!");
Else
{ Tmp_cell
tmp_cell->element = x;
tmp_cell->next = p->next; 60
p->next = tmp_cell;
List p
}
} 40 48 55 59 63

8/17/2020 Department of Computer Science and Engineering 77


Deletion of a particular node
List

40 48 55 59 63
An existing Linked list
Node that is to be deleted
List

40 48 55 59 63

p tempcell
Searching the target element
1. making link between previous and next
node of the node to be deleted
List

40 48 55 59 63

p tempcell 2. deleting the target node


Deleting the target node
List

40 48 59 63

Updated List
8/17/2020 Department of Computer Science and Engineering 78
Deletion of a particular node

Void delete( element_type x, LIST L )


{
position p, tmp_cell;
p = find_previous( x, L ); delete(55,L)
if( p->next != NULL )
{
tmp_cell = p->next;
p->next = tmp_cell->next;
free( tmp_cell );
} List p
} 40 48 55 59 63

Tmp_cell

8/17/2020 Department of Computer Science and Engineering 79


Doubly Linked List

8/17/2020 Department of Computer Science and Engineering 80


Doubly Linked List Representation

• A doubly Linked List can be represented as,

➢Each node contains a value and two link


(pointers or references) to next and previous
node
➢The last node contains a null link
➢The list will have a header at starting point
8/17/2020 Department of Computer Science and Engineering 81
Doubly Linked List - Advantages

• Allows traversal of nodes in both direction


which is not possible in singly linked list.
• Deletion of nodes is easy when compared
to singly linked list
• Reversing the list is simple and straightforward.

8/17/2020 Department of Computer Science and Engineering 82


Applications- in real life

• navigation systems where both front and back


navigation is required.
• implement backward and forward navigation of
visited web pages
• implement Undo and Redo functionality.
• to represent various states of a game.

8/17/2020 Department of Computer Science and Engineering 83


CREATING A NEW NODE
•Create a head node and assign some data to its data
field.
•Make sure that the previous and next address field of
the head node must point to NULL.
•Make the head node as last node.
struct Node {
int data;
struct Node* next; // Pointer to next node in DLL
struct Node* prev; // Pointer to previous node in DLL
};

Department of Computer Science and Engineering 84


INSERTION

Department of Computer Science and Engineering 85


INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL)
{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P Flink;
P →Flink →Blink = Newnode;
P →Flink = Newnode ;
Newnode →Blink = P;
}}
Department of Computer Science and Engineering 86
INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Insert(25,L,2)
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL) P

{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P → Flink;
P →Flink →Blink = Newnode;
P →Flink = Newnode ; newnode

Newnode →Blink = P;
}}
Department of Computer Science and Engineering 87
INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Insert(25,L,2)
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL) P

{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P → Flink;
25
P →Flink →Blink = Newnode;
P →Flink = Newnode ; newnode

Newnode →Blink = P;
}}
Department of Computer Science and Engineering 88
INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Insert(25,L,2)
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL) P

{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P → Flink;
25
P →Flink →Blink = Newnode;
P →Flink = Newnode ; newnode

Newnode →Blink = P;
}}
Department of Computer Science and Engineering 89
INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Insert(25,L,2)
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL) P

{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P → Flink;
25
P →Flink →Blink = Newnode;
P →Flink = Newnode ; newnode

Newnode →Blink = P;
}}
Department of Computer Science and Engineering 90
INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Insert(25,L,2)
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL) P

{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P → Flink;
25
P →Flink →Blink = Newnode;
P →Flink = Newnode ; newnode

Newnode →Blink = P;
}}
Department of Computer Science and Engineering 91
INSERTION
void Insert (int X, list L, position P)
{
Struct Node * Newnode;
Insert(25,L,2)
Newnode = malloc (size of (Struct Node));
If (Newnode ! = NULL) P

{ L
11 22 33 X
Newnode →Element = X;
Newnode →Flink = P → Flink;
25
P →Flink →Blink = Newnode;
P →Flink = Newnode ; newnode

Newnode →Blink = P;
}}
Department of Computer Science and Engineering 92
DELETION AT SPECIFIED POSITION

Department of Computer Science and Engineering 93


DELETION
void Delete (int X, List L)
{
position P;
P = Find (X, L);
If ( IsLast (P, L))
{
Temp = P;
P →Blink →Flink = NULL;
free (Temp);
}

Department of Computer Science and Engineering 94


DELETION
void Delete (int X, List L)
{
position P; Delete(33,L)

P = Find (X, L);


If ( IsLast (P, L))
{
Temp = P; P

P →Blink →Flink = NULL;


free (Temp);
}

Department of Computer Science and Engineering 95


DELETION
void Delete (int X, List L)
{
Delete(33,L)
position P;
P = Find (X, L);
If ( IsLast (P, L))
{
P
Temp = P;
Temp
P →Blink →Flink = NULL;
free (Temp);
}

Department of Computer Science and Engineering 96


DELETION
void Delete (int X, List L)
{
Delete(33,L)
position P;
P = Find (X, L);
If ( IsLast (P, L)) X
{
P
Temp = P;
Temp
P →Blink →Flink = NULL;
free (Temp);
}

Department of Computer Science and Engineering 97


DELETION
void Delete (int X, List L)
{
Delete(33,L)
position P;
P = Find (X, L);
If ( IsLast (P, L))
{
Temp = P;
P →Blink →Flink = NULL;
free (Temp);
}

Department of Computer Science and Engineering 98


DELETION
else
{
Delete(22,L)
Temp = P;
P →Blink→ Flink = P→Flink;
P →Flink →Blink = P→Blink;
free (Temp);
}
}

Department of Computer Science and Engineering 99


DELETION
else
{
Delete(22,L)
Temp = P;
P →Blink→ Flink = P→Flink;
P →Flink →Blink = P→Blink;
free (Temp);
}
}
P

Temp

Department of Computer Science and Engineering 10


DELETION
else
{
Delete(22,L)
Temp = P;
P →Blink→ Flink = P→Flink;
P →Flink →Blink = P→Blink;
free (Temp);
}
}
P

Temp

Department of Computer Science and Engineering 10


DELETION
else
{
Delete(22,L)
Temp = P;
P →Blink→ Flink = P→Flink;
P →Flink →Blink = P→Blink;
free (Temp);
}
}

Temp

Department of Computer Science and Engineering 10


DELETION
else
{
Delete(22,L)
Temp = P;
P →Blink→ Flink = P→Flink;
P →Flink →Blink = P→Blink;
free (Temp);
}
}

Department of Computer Science and Engineering 10


Circular Linked List Representation
• In singly circular linked list, the next pointer of the last
node points to the first node, which can be represented
as,

• In doubly linked list, the next pointer of the last node


points to the first node and the previous pointer of the
first node points to the last node making the circular in
both directions.

8/17/2020 Department of Computer Science and Engineering 104


APPLICATIONS

❖Implementation of stacks and queues

❖Implementation of graphs : Adjacency list representation of graphs is most

popular which is uses linked list to store adjacent vertices.

❖Dynamic memory allocation : We use linked list of free blocks.

❖Maintaining directory of names

❖Performing arithmetic operations on long integers

❖Manipulation of polynomials by storing constants in the node of linked list

❖representing sparse matrices

Department of Computer Science and Engineering 105


APPLICATIONS

Image viewer – Previous and next images are linked, hence can be accessed by

next and previous button.

Previous and next page in web browser – We can access previous and next url

searched in web browser by pressing back and next button since, they are linked as

linked list.

Music Player – Songs in music player are linked to previous and next song. you can

play songs either from starting or ending of the list.

Department of Computer Science and Engineering 106


Stack ADT

Department of Computer Science and Engineering 107


Stack ADT

• Principle-LIFO
•Top pointer
•Operations
•Push()
•Pop()
•Conditions
•Underflow-Empty stack
•Overflow – Full stack
and Project
Management
(SEPM)

Department of Computer Science and Engineering 108


Why it is Used?

•Used for
❑ Reversing elements
❑ MS paint and Editing apps.
❑ Expression evaluation
❑ Backtracking algorithms
❑To check the string is well formed parenthesis.

and Project
Management
(SEPM)

Department of Computer Science and Engineering 109


Stack Operation

and Project
Management
(SEPM)

Department of Computer Science and Engineering 110


Stack Operation
• Push-Insertion
❑To push x:
top++;
s[top]=x;
• Pop –Deletion
❑To pop(x)
s[top]=x
top--;
• Peek() – return the top element
• isEmpty()-to check whether stack is empty or not
• isFull()- to check whether stack is full or not
and Project
Management
(SEPM)

Department of Computer Science and Engineering 111


Stack Implementation

Two types:
Array Implementation
Linked List Implementation

and Project
Management
(SEPM)

Department of Computer Science and Engineering 112


Stack Implementation

Array Implementation of Stack ADT:


•peek( )
•push( )
•pop( )
•isempty( ) Example:
•isfull( ) Stack: S[5];

int s[size];
int top=-1;

Top=-1
and Project
Management
(SEPM)

Department of Computer Science and Engineering 113


Array Implementation
To check whether stack is empty:
bool isfull()
{
if(top == MAXSIZE-1)
return true; 4 50
Top
else
return false; 3 40
Size =5
} Top=4
2 30

1 20

0 10

and Project
Management
(SEPM)

Department of Computer Science and Engineering 114


Array Implementation

To check whether stack is empty:


bool isempty()
{
if(top == -1) 4
return true;
else 3
return false; 2
}
1

and Project Top=-1


Management
(SEPM)

Department of Computer Science and Engineering 115


Array Implementation
push():

void push(int data) 3 43


{
2 33
if(!isfull())
1 Stack Full!!!
{ 22
top = top + 1; 0 11
stack[top] = data;
} Top=-1
else
{
printf(“Stack is full.\n");
}
} and Project
Management
(SEPM)

Department of Computer Science and Engineering 116


Array Implementation
pop():
55
int pop(Stack s,int data) top data
{
45
if(!isempty())
{
data = stack[top]; 35
top = top - 1;
return data;
25
}
else
{
printf("Stack is empty.\n");
} and Project Top=-1
Management
} (SEPM)
Stack Empty!!!

Department of Computer Science and Engineering 117


Linked List Implementation

Node Creation:

Step 1 - Define a 'Node' structure with two

members data and next.

Step 2 - Define a Node pointer 'top' and set it to NULL.

DATA NEXT
and Project
Management
(SEPM)

Department of Computer Science and Engineering 118


LINKED LIST IMPLEMENTATION - PUSH

and Project
Management
(SEPM)

Department of Computer Science and Engineering 119


CREATE AN EMPTY STACK
Stack CreateStack ( )
{
Stack S;
S = malloc (Sizeof (Struct Node));
if (S = = NULL)
Error (" Outof Space");
MakeEmpty (s);
return S;
}

and Project
Management
(SEPM)

Department of Computer Science and Engineering 120


MAKE EMPTY
void MakeEmpty (Stack S)
{
if (S = = NULL)
Error (" Create Stack First");
else
while (! IsEmpty (s))
pop (s);
}

and Project
Management
(SEPM)

Department of Computer Science and Engineering 121


LINKED LIST IMPLEMENTATION - PUSH
void push (int X, Stack S)
{
Struct Node * Tempcell;
Tempcell = malloc (sizeof (Struct Node));
If (Tempcell = = NULL)
Error ("Out of Space");
S 11 22 NULL
else
{
Tempcell → Element = X; X

Tempcell → Next = S →
andNext;
Project
Management
S→Next = Tempcell; (SEPM)

}}
Department of Computer Science and Engineering 122
LINKED LIST IMPLEMENTATION - POP

and Project
Management
(SEPM)

Department of Computer Science and Engineering 123


LINKED LIST IMPLEMENTATION - POP
void pop (Stack S)
{
Struct Node *Tempcell;
If (IsEmpty (S))
S 11 22 33 X
Error ("Empty Stack");
else
Tempcell
{
Tempcell = S→Next;
S→Next = S→Next→Next;
Free (Tempcell); and Project
Management
}} (SEPM)

Department of Computer Science and Engineering 124


RETURN TOP ELEMENT

int Top (Stack S)


{
If (! IsEmpty (s))
return S→Next→Element;
Error ("Empty Stack");
return 0;
}

and Project
Management
(SEPM)

Department of Computer Science and Engineering 125


Applications

❑ Function call
❑ Evaluation Arithmetic Expression
•Infix to postfix
•Evaluating postfix
❑ Balancing Parenthesis
❑Reversing a word
❑8 Queen problem
❑Towers of Hanoi
and Project
Management
(SEPM)

Department of Computer Science and Engineering 126


EVALUATING ARITHMETIC EXPRESSIONS

INFIX notation:
•Operator presents between operands: A+B
POSTFIX notation:
•Operator follows its two operands: AB+
PREFIX notation:
•Operator precedes its two operands: +AB
Operator Precedence
$, ^
*,/
+,-

Department of Computer Science and Engineering 127


INFIX TO POSTFIX -ALGORITHM

1. Scan the infix expression

2. If the scanned character is an operand, output it.

3. Else,

3.1 If (precedence of scanned operator) >(precedence

of stack operator) - push it.

3.2 Else, Pop all the operators from the stack which are

greater than or equal to in precedence than that of the

scanned operator.

Department of Computer Science and Engineering 128


INFIX TO POSTFIX –ALGORITHM

4. If the scanned character is an ‘(‘, push it to the stack.

5. If the scanned character is an ‘)’, pop the stack and and

output it until a ‘(‘ is encountered, and discard both the

parenthesis.

6. Repeat steps 2-6 until infix expression is scanned.

7. Print the output

8. Pop and output from the stack until it is not empty.

Department of Computer Science and Engineering 129


CONVERSION OF INFIX INTO POSTFIX
(2+(4-1)*3) into 241-3*+
CURRENT SYMBOL ACTION STACK STATUS POSTFIX
PERFORMED EXPRESSION

( PUSH ( ( 2
2 2
+ PUSH + (+ 2
( PUSH ( (+( 24
4 24
- PUSH - (+(- 241
1 POP 241-
) (+ 241-
* PUSH * (+* 241-
3 241-3
POP * 241-3*
POP + 241-3*+
)
130
EVALUATING POSTFIX EXPRESSION

1) Scan the expression from left to right.

2) If an operand is seen, push it onto stack.

3) If an operator is seen, pop of top two elements

(operands) [ x & y ] from stack and perform z = operand y

Push z onto stack.

4) Repeat steps (2) & (3) till scanning is over.

Department of Computer Science and Engineering 131


EVALUATING POSTFIX EXPRESSION

Department of Computer Science and Engineering 132


BALANCING PARENTHESIS

•Declare a character stack S.


•Now traverse the expression string exp.
❑If the current character is a starting bracket (‘(‘ or ‘{‘ or ‘[‘)
then push it to stack.
❑If the current character is a closing bracket (‘)’ or ‘}’ or ‘]’)
then pop from stack and if the popped character is the matching
starting bracket then fine else parenthesis are not balanced.
•After complete traversal, if there is some starting bracket left in
stack then “not balanced”

Balancing Parenthesis

Department of Computer Science and Engineering 133


BALANCING PARENTHESIS

VALID INPUTS INVALID INPUTS

{} {(}
({[]}) ([(()])
{[]()} {}[])
[{({}[]({ [{)}(]}]
})}]
Department of Computer Science and Engineering 134
TOWERS OF HANOI
void hanoi (int n, char s, char d, char i)
{
if (n = = 1)
{
print (s, d);
return;
}
else
{
hanoi (n - 1, s, i, d);
print (s, d) ;
hanoi (n-1, i, d, s);
return; and Project
Management
} (SEPM)
}

Department of Computer Science and Engineering 135


TOWERS OF HANOI

and Project
Management
(SEPM)

Department of Computer Science and Engineering 136


FUNCTION CALLS
When a call is made to a new function all the variables local to the
calling routine need to be saved, otherwise the new function will overwrite
the calling routine variables.
RECURSIVE FUNCTION TO FIND FACTORIAL : -
int fact (int n)
{
int s;
if (n = = 1)
return (1);
else
s = n * fact (n - 1);
return (s); and Project
Management
} (SEPM)

Department of Computer Science and Engineering 137


Queue ADT

• FIFO

•Front is a variable which refers to first position in queue

•Rear is a variable which refers to last position in queue.

and Project
Management
(SEPM)

Department of Computer Science and Engineering 138


Queue ADT

❑Serving requests on a single shared resource, like a printer, CPU task

scheduling etc.

❑Call Center phone systems

❑Handling of interrupts in real-time systems

and Project
Management
(SEPM)

Department of Computer Science and Engineering 139


Queue ADT

•Basic Operations
❑enqueue() – Rear end
❑dequeue() – Front end
•Conditions
❑Underflow-Empty Queue
❑Overflow - Full Queue

and Project
Management
(SEPM)

Department of Computer Science and Engineering 140


TYPES OF QUEUE

• Linear Queue

• Circular Queue

• Priority Queue

• Deque – Double Ended Queue

Department of Computer Science and Engineering 141


LINEAR QUEUE- IMPLEMENTATION

•A normal queue
insertion - Rear
deletion –Front

Department of Computer Science and Engineering 142


QUEUE-ARRAY IMPLEMENTATION

int front = -1, rear = -1;

int queue[maxsize];

Example:
int queue[5];

front = rear = -1; 0 1 2 3 4

Department of Computer Science and Engineering 143


ENQUEUE
void enqueue(item)
{
0 1 2 3 4
if (rear = = maxsize-1 )
print (“queue overflow”)
else f= r = -1;

if (front == - 1)
front = 0;
rear = rear + 1
Queue [rear] = item
}
Department of Computer Science and Engineering 144
ENQUEUE
void enqueue(item)
{
0 1 2 3 4
if (rear = = maxsize-1 )
print (“queue overflow”) f
else r = -1;

if (front == - 1)
front = 0;
rear = rear + 1
Queue [rear] = item
}
Department of Computer Science and Engineering 145
ENQUEUE
void enqueue(item)
{
0 1 2 3 4
if (rear = = maxsize-1 )
print (“queue overflow”) f

else r

if (front == - 1)
front = 0;
rear = rear + 1
Queue [rear] = item
}
Department of Computer Science and Engineering 146
ENQUEUE
void enqueue(item)
{
0 1 2 3 4
if (rear = = maxsize-1 ) item

print (“queue overflow”) f

else r

if (front == - 1)
front = 0;
rear = rear + 1
Queue [rear] = item
}
Department of Computer Science and Engineering 147
ENQUEUE
void enqueue(item)
{
0 1 2 3 4
if (rear = = maxsize-1 ) item

print (“queue overflow”) f


r r r r
else r

if (front == - 1)
front = 0;
rear = rear + 1
Queue [rear] = item
}
Department of Computer Science and Engineering 148
ENQUEUE
void enqueue(item)
{
0 1 2 3 4
if (rear = = maxsize-1 ) item item item item item

print (“queue overflow”) f


r r r r
else r

if (front == - 1)
front = 0;
rear = rear + 1
Queue [rear] = item
}
Department of Computer Science and Engineering 149
DEQUEUE
void dequeue()
{ 0 1 2 3 4
item item item item item
if (front= = -1)
print “queue empty” f r

else if(front == rear)


front=rear=-1
else
{
item = queue [front];
front = front + 1
return item;
}}
Department of Computer Science and Engineering 150
DEQUEUE
void dequeue()
{ 0 1 2 3 4
item item item item item
if (front= = -1)
print “queue empty” f r

else if(front == rear)


front=rear=-1
else
{
item = queue [front];
front = front + 1
return item;
}}
Department of Computer Science and Engineering 151
DEQUEUE
void dequeue()
{ 0 1 2 3 4
Data 1 Data 2 Data 3 Data 4 Data 5
if (front= = -1)
print “queue empty” f r

else if(front == rear) item

front=rear=-1
else
{
item = queue [front];
front = front + 1
return item;
}}
Department of Computer Science and Engineering 152
DEQUEUE
void dequeue()
{ 0 1 2 3 4
Data 1 Data 2 Data 3 Data 4 Data 5
if (front= = -1)
print “queue empty” f r

else if(front == rear) item

front=rear=-1
else
{
item = queue [front];
front = front + 1
return item;
}}
Department of Computer Science and Engineering 153
DEQUEUE
void dequeue()
{ 0 1 2 3 4
Data 2 Data 3 Data 4 Data 5
if (front= = -1)
print “queue empty” f f f r f

else if(front == rear)


front=rear=-1
else
{
item = queue [front];
front = front + 1
return item;
}}
Department of Computer Science and Engineering 154
DEQUEUE
void dequeue()
{ 0 1 2 3 4
Data 5
if (front= = -1)
print “queue empty” r f

else if(front == rear)


front=rear=-1
else
{
item = queue [front];
front = front + 1
return item;
}}
Department of Computer Science and Engineering 155
DEQUEUE
void dequeue()
{ 0 1 2 3 4

if (front= = -1)
print “queue empty” f,r=-1

else if(front == rear)


front=rear=-1
else
{
item = queue [front];
front = front + 1
return item;
}}
Department of Computer Science and Engineering 156
DEQUEUE
void dequeue()
{ 0 1 2 3 4

if (front= = -1)
print “queue empty” f,r=-1

else if(front == rear)


front=rear=-1
else
{
item = queue [front];
front = front + 1
return item;
}}
Department of Computer Science and Engineering 157
LIST IMPLEMENTATION QUEUE
enQueue() adds a new node after rear and moves rear to the next node.
deQueue() removes the front node and moves front to the next node.

struct Node {
int data;
struct Node *next;
}*front = NULL,*rear = NULL;

Department of Computer Science and Engineering 158


LIST IMPLEMENTATION QUEUE

rear

After inserting 4

Department of Computer Science and Engineering 159


LIST IMPLEMENTATION QUEUE

void Enqueue(int value) {


struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else{
rear -> next = newNode;
rear = newNode;
}
} Department of Computer Science and Engineering 160
LIST IMPLEMENTATION QUEUE

front

Department of Computer Science and Engineering 161


LIST IMPLEMENTATION QUEUE

void dequeue()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
Else
{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp); }
}

Department of Computer Science and Engineering 162


CIRCULAR QUEUE

•The last element points to the first element.

•Ring Buffer

Use:

consumes less memory than linear queue

Department of Computer Science and Engineering 163


CIRCULAR QUEUE

Department of Computer Science and Engineering 164


ENQUEUE
void CEnqueue (int X)
{
if (Front = = (rear + 1) % Maxsize)
print ("Queue is overflow");
else
{
if (front = = -1)
front = rear = 0;
else
rear = (rear + 1)% Maxsize;
CQueue [rear] = X;
Department of Computer Science and Engineering 165
}}
DEQUEUE
int CDequeue ( )
{
if (front = = -1)
print ("Queue is underflow");
else
{
X = CQueue [Front];
if (Front = = Rear)
Front = Rear = -1;
else
Front = (Front + 1)% maxsize;
}
return (X);
}

Department of Computer Science and Engineering 166


APPLICATIONS OF QUEUE
• Batch processing in an operating system
• To implement Priority Queues.
• Priority Queues can be used to sort the elements using Heap
Sort.
• Simulation.
• Mathematics user Queueing theory.
• Computer networks where the server takes the jobs of the
client as per the queue strategy.
and Project
Management
(SEPM)

Department of Computer Science and Engineering 167


RESEARCH APPLICATION
• Image processing

-Watershed transformation implementation by hierarchical queues

• The HQ is initialized by storing tokens corresponding to the pixels

labelled in image gin their respective queues.

• In the case of the watershed transform (WTS), this priority level of each

queue corresponds to the grey level of the token (pixel).

• pixels of lower grey level have the highest priority (queues are numbered

according to the grey levels, queue 0 has the highest priority).

Department of Computer Science and Engineering 168


RESEARCH APPLICATION

Department of Computer Science and Engineering 169


QUEUEING THEORY

Department of Computer Science and Engineering 170


Reverse a linked list
The following function reverse() is supposed to reverse a singly
linked list. There is one line missing at the end of the function.

static void reverse(struct node** head_ref)


{
struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL)
{ A head_ref = prev;
next = current->next; B *head_ref = current;
current->next = prev; C *head_ref = next;
prev = current; D *head_ref = NULL
current = next;
}
/*ADD A STATEMENT HERE*/
}

8/29/2024 Department of Computer Science and Engineering 171


Reverse a linked list
The following function reverse() is supposed to reverse a singly
linked list. There is one line missing at the end of the function.

static void reverse(struct node** head_ref)


{
struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL)
{ A *head_ref = prev;
next = current->next; B *head_ref = current;
current->next = prev; C *head_ref = next;
prev = current; D *head_ref = NULL
current = next;
}
/*ADD A STATEMENT HERE*/
}

8/29/2024 Department of Computer Science and Engineering 172


Reverse a linked list
What is the output of following function for start pointing to first node
of following linked list? 1->2->3->4->5->6void fun(struct node* start)
{
if(start == NULL)
return;
printf("%d ", start->data);

if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}
A146641
B135135
C1235
D135531

8/29/2024 Department of Computer Science and Engineering 173


Reverse a linked list
What is the output of following function for start pointing to first node
of following linked list? 1->2->3->4->5->6void fun(struct node* start)
{
if(start == NULL)
return;
printf("%d ", start->data);

if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}

A146641
B135135
C1235
D135531

8/29/2024 Department of Computer Science and Engineering 174


Linked List
Consider the following function to traverse a linked list.
void traverse(struct Node *head)
{
while (head->next != NULL)
{
printf("%d ", head->data);
head = head->next;
}
}
What is wrong in the above code?

8/17/2020 Department of Computer Science and Engineering 175


Linked List
What does the following function do for a given Linked List
with first node as head?
void fun1(struct node* head)
{
if(head == NULL)
return;
fun1(head->next);
printf("%d ", head->data);
}

Input: 15 25 10 55 90
8/17/2020 Department of Computer Science and Engineering 176
Linked List
The following steps in a linked list
p = getnode()
info (p) = 10
next (p) = list
list = p
result in which type of operation?
A pop operation in stack
B removal of a node
C inserting a node
D modifying an existing node

8/17/2020 Department of Computer Science and Engineering 177


Linked List
The function is called with the list containing the integers
1,2,3,4,5,6,7 in the given order. O/P?

8/17/2020 Department of Computer Science and Engineering 178


Linked List
The function is called with the list containing the integers
1,2,3,4,5,6,7 in the given order. O/P?

8/17/2020 Department of Computer Science and Engineering 179


Stack
The result evaluating the postfix expression
10 5 + 60 6 / ∗ 8 − is
A 284
B 213
C 142
D 71

8/17/2020 Department of Computer Science and Engineering 180


Stack
The following postfix expression with single digit operands
is evaluated using a stack: 8 2 3 ^ / 2 3 * + 5 1 * - Note that ^
is the exponentiation operator. The top two elements of the
stack after the first * is evaluated are:
A.6,1
B.5,7
C.3,2
D.1,5

8/17/2020 Department of Computer Science and Engineering 181


Queue
Suppose you are given an implementation of a queue of integers. The operations that can be performed
on the queue are:
i. isEmpty (Q) — returns true if the queue is empty, false otherwise.
ii. delete (Q) — deletes the element at the front of the queue and returns its value.
iii. insert (Q, i) — inserts the integer i at the rear of the queue.
Consider the following function:
void f (queue Q) {
int i ;
if (!isEmpty(Q))
{
i = delete(Q);
f(Q);
insert(Q, i);
}}
What operation is performed by the above function f ?
a.Leaves the Queue Unchanged
b. Revrese the Queue
c.Empties the Queue
8/17/2020 Department of Computer Science and Engineering 182
d.Delete the front element from the Queue
Thank You

Department of Computer Science and Engineering

You might also like