0% found this document useful (0 votes)
349 views

Data Structures Lab Manual 2021-22

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
349 views

Data Structures Lab Manual 2021-22

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

JERUSALEM COLLEGE OF ENGINEERING

(An Autonomous Institution)


(Approved by AICTE, Affiliated to Anna University
Accredited by NBA and NAAC with ‘A’ Grade)
Velachery Main Road, Narayanapuram Pallikaranai, Chennai – 600 100

DATA STRUCTURES LABORATORY


[JCS1311]

RECORD NOTE BOOK


ACADEMIC YEAR 2021 -2022
II YEAR/ III SEMESTER
REGULATION: 2019

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING
VISION OF THE DEPARTMENT
The Department of computer science and engineering is dedicated to be a center of excellence, in
producing graduates as ethical engineers, innovative researchers, dynamic entrepreneurs and globally
competitive technocrats.
MISSION OF THE DEPARTMENT

• To craft students to be competent professionals with value based education, innovative teaching
and practices.
• To enhance student‘s soft skill, personality and ethical responsibilities by augmenting in- plant
training, value added courses and co curricular activities.
• To facilitate the student as researchers by widening their professional knowledge through continuous
learning and innovative projects.
• To produce dynamic entrepreneur through interaction with network of alumni industry and academia
and extracurricular activities.

PROGRAM EDUCATIONAL OBJECTIVES (PEOS)

• PEO1:Graduates will apply engineering basics, laboratory and job oriented experiences to devise and
unravel engineering problems in computer science engineering domain.
• PEO2:Graduates will be multi faceted researcher and experts in fields like computing, networking,
artificial intelligence, software engineering and data science.
• PEO3:Graduates will be dynamic entrepreneur and service oriented professional with ethical and social
responsibility.
• PEO4:Graduates will ingress and endure in core and other prominent organization across the globe
and will foster innovation

PROGRAM SPECIFIC OBJECTIVES (PSOS)

PSO-I: The ability to understand, analyze and to develop the design related to real-time system such as
IOT, Secured automated systems, machine vision , computer vision and cognitive computing with
various complexities , providing orientation towards green computing environment .

PSO-II: The ability to apply standard practices and strategies in software project development using
open-ended programming environments to deliver a quality product.

PSO-III: The ability to innovate, introduce and produce socially relevant products to facilitate
transformation of society into a digitally empowered knowledge economy, thereby to chart a successful
career with a new dimension to entrepreneurship.
JERUSALEM COLLEGE OF ENGINEERING
(An Autonomous Institution)
(Approved by AICTE, Affiliated to Anna University
Accredited by NBA and NAAC with ‘A’ Grade)
Velachery Main Road, Pallikaranai , Chennai – 600100

Name……………………………………………………………………………………

Year…………………………………Semester………………Branch……………….

Regulation………………..

Register No.

Certified that this is a Bonafide Record work done by the above student in the

…..……………………………………………. Laboratory during the year 20 - 20

Signature of Lab In-charge Signature of Head of the Department

EXAMINERS

DATE:

INTERNAL EXAMINER EXTERNAL EXAMINER


JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

SYLLABUS

L T P C
JCS1311 DATA STRUCTURES LABORATORY
0 0 4 2
COURSE OBJECTIVES
• To implement linear and non-linear data structures
• To identify and implement appropriate data structures for various applications
• To execute different operations of search trees
• To implement various sorting and searching algorithms
• To implement hashing techniques

LIST OF PROGRAMS
Implement the following programs

1. List ADT using Python with insert, delete, search and modify operations
2. Linked list
3. Polynomial Manipulation using list ADT
4. Stack using Python
5. a. Stack ADT using arrays
b. Stack ADT using linked list
6. a. Queue ADT using arrays
b. Queue ADT using linked list
7. Infix to Postfix conversion
8. Circular queue
9. Binary Search Trees and Tree traversals
10. AVL Trees
11. BFS and
12. DFS of a graph
13. Bubble sort,
14. Selection sort,
15. Insertion sort using Python
16. Linear search and
17. Binary search using Python
18. Hashing-Linear Probing and
19. Quadratic Probing
TOTAL 60 Periods
COURSE OUTCOMES
Upon completion of the course, the students will be able to:
1. Identify appropriate data structures for specified problem definition
2. Implement operations like searching, insertion, deletion, traversing mechanism etc. on
various data structures
3. Apply appropriate linear / non -linear data structure operations for solving a given problem
4. Implement appropriate sorting/searching technique for given problem
5. Apply appropriate hash functions that result in a collision free scenario for data storage and
retrieval

1
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

LIST OF EXPERIMENTS
CYCLE 1

S.No NAME OF EXPERIMENTS


1. List ADT using Array in Python with insert, delete, search and modify operations

2. List ADT using Linked

3. Polynomial Manipulation using List ADT

4. Stack using Array in Python


a. Stack ADT using arrays
5.
b. Stack ADT using linked list
a. Queue ADT using arrays
6.
b. Queue ADT using linked list
7. Infix to Postfix conversion

8. Circular queue

CYCLE 2

S.No NAME OF EXPERIMENTS


9. Binary Search Trees and Tree traversals
10. AVL Trees
11. Breadth First Search
12. Depth First Search
13. Bubble sort
14. Selection sort
15. Insertion sort
16. Linear search
17. Binary search
18. Hashing-Linear Probing
19. Hashing-Quadratic Probing
ADDITIONAL EXPERIMENTS
20. Doubly Linked List
21. Quick Sort

2
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

CONTENTS

S.No Date Name of the Experiment Page.No Marks Signature


with Date

3
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

CONTENTS

S.No Date Name of the Experiment Page.No Marks Signature


With Date

Average Marks :
Signature :

4
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:1
LIST ADT USING ARRAY IN PYTHON WITH INSERT, DELETE,
Date: SEARCH AND MODIFY OPERATIONS

AIM :

To implement LIST ADT using array and its operations.

ALGORITHM:

Basic Operations:
1. Is Empty(LIST)
2. Is Full(LIST)
3. Insert Element to End of the LIST.
4. Delete Element from End of the LIST.
5. Insert Element to front of the LIST.
6. Delete Element from front of the LIST.
7. Insert Element to nth Position of the LIST.
8. Delete Element from nth Position of the LIST.
9. Search Element in the LIST.
10. Print the Elements in the LIST.

1. Is Empty(LIST):
If (Current Size==0) "LIST is Empty"

else "LIST is not Empty"

2. Is Full(LIST):
If (Current Size=Max Size) "LIST is FULL"
else "LIST is not FULL"

3. Insert Element to End of the LIST.


1. Check that weather the List is full or not
2. If List is full return error message ”List is full. Can’t Insert”.
3. If List is not full.
• Get the position to insert the new element by Position=Current Size+1
• Insert the element to the Position
• Increase the Current Size by 1 i.e. Current Size=Current Size+1
4. Delete Element from End of the LIST.
1. Check that weather the List is empty or not
2. If List is empty return error message ”List is Empty. Can't Delete”.
3. If List is not Empty.
• Get the position of the element to delete by Position=Current Size
5
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

• Delete the element from the Position


• Decrease the Current Size by 1 i.e. Current Size=Current Size-1
5. Insert Element to front of the LIST.
1. Check that weather the List is full or not
2. If List is full return error message ”List is full. Can't Insert”.
3. If List is not full.
• Free the 1st Position of the list by moving all the Element to one position
forward i.eNew Position=Current Position + 1.
• Insert the element to the 1st Position
• Increase the Current Size by 1 i.e. Current Size=Current Size+1
6. Delete Element from front of the LIST.
1. Check that weather the List is empty or not
2. If List is empty return error message ”List is Empty. Can't Delete”.
3. If List is not Empty.
• Move all the elements except one in the 1st position to one position
backward i.eNew Position= Current Position -1
• After the 1st step, element in the 1st position will be automatically
deleted.
• Decrease the Current Size by 1 i.e. Current Size=Current Size-1
7. Insert Element to nth Position of the LIST.
1. Check that weather the List is full or not
2. If List is full return error message ”List is full. Can't Insert”.
3. If List is not full.
• If List is Empty, Insert element at Position 1.
• If (nth Position > Current Size)
Return message “nth Position Not available in List”
• else
✓ Free the nth Position of the list by moving all Elements
to one position forwardexcept n-1,n-2,... 1 Position i.e move only
from n to current size position Elements. i.e New
Position=Current Position + 1.
✓ Insert the element to the nth Position
✓ Increase the Current Size by 1 i.e. Current Size=Current Size+1
8. Delete Element from nth Position of the LIST.
1. Check that weather the List is Empty or not
2. If List is Empty return error message ”List is Empty.”
3. If List is not Empty.
• If (nth Position > Current Size)
✓ Return message “nth Position Not available in List”
• If (nth Position == Current Size)

6
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

✓ Delete the element from nth Position


✓ Decrease the Current Size by 1 i.e. Current Size=Current Size-1
• If (nth Position < Current Size)
✓ Move all the Elements to one position backward except n,n-1,n-
2,... 1 Position i.e move only from n+1 to current size position
Elements. i.e New Position=Current Position - 1.
✓ After the previous step, nth element will be deleted
automatically.
✓ Decrease the Current Size by 1 i.e. Current Size=Current Size-1
9. Search Element in the LIST.
1. Check that weather the list is empty or not.
2. If List is empty, return error message “List is Empty”.
3. If List is not Empty
• Find the Position where the last element available in the List by Last
Position = Current Size
• For( Position 1 to Last Position)
✓ If(Element @ Position== Search Element)//If Element matches
the search element
✓ return the Position by message “Search Element available in
Position”
• Else return message “Search Element not available in the List”
10. Print the Elements in the LIST.
1. Check that weather the list is empty or not.
2. If List is empty, return error message “List is Empty”.
3. If List is not Empty
• Find the Position where the last element available in the List by Last
Position = Current Size
• For( Position 1 to Last Position)
• Print the Position and Element available at the position of List.

7
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

8
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

9
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

10
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

11
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:2
LIST ADT USING LINKED LIST
Date:

AIM:

To write a ‘C’ program to create a singly linked list implementation.

ALGORITHM:

Create:
1. first=new node;{create the 1st node of the list pointed by first}; 2.REad(Data(first));
3. NEXT(First)=NULL;
4. Far a First; [point Far to the First]
5. For I=1 to N-1 repeat steps 6 to 10
6.X=new node;

7.Read(Data(X))
8.NEXT(X)=NULL;
9.NEXT(Far)=X; {connect the nodes} 10.Far=X;[shift the

pointer to the last node of the list] 11.END

TRAVERSING A LINKED LIST


1.IF First=NULL then {print “List empty” STOP};
2. Count=0;

3.ptr=First; {point ptr to the 1st node}


4.While ptr<> NULL repeat Steps 5 to 6
5.count=count+1;

6.ptr=NEXT(ptr) [shift ptr to the next node] 7.print


(‘Number of nodes=’, count)

8.END

SEARCH
1.If first=NULL then{
Print “List empty”; STOP;}

2. PTr=First; [point ptr to the 1st node]


3.while (ptr<>NULL) repeat steps 4 to 5 4.If

(DATA (ptr)= ‘X’)


Then {print “item found”;
12
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

STOP

}
5.ptr=NEXT (ptr); [shift ptr to the next node]
[end of while]

6.Print “item not found”; 7.END

INSERTION
1. X=new node;
2. Read(DATA(X);
3. If (FIRST=NULL) then
{
First=X;
NEXT(X)=NULL;

}
Else
{

NEXT(X)=First;
First=X;
}
4. END

13
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

INSERT AFTER A NODE


1. Ptr=List;
2. While (ptr<>NULL) repeat steps 3 to 4
3. If (DATA(ptr)=’VAL’) then
{
X=new node Read
(DATA(X);

NEXT(X)=NEXT(ptr)
NEXT(ptr)=X; BREAK;

}
4. ptr=NEXT(ptr)
[end of while]
5. END
INSERT BEFORE A NODE
1. ptr=LIST
[check if the first node is the desired one]
2. If(data(ptr)=’VAL’) then
{
X=new node;
Read DATA(X);
NEXT(X)=LIST;
LIST=X;
STOP;
}
3. While(ptr<>NULL ) repeat step 4 to 6
4. back=ptr;
5. ptr=NEXT(ptr);
6. If(DATA(ptr)=’VAL’)then
{
X=new node;
Read DATA(X);
NEXT(X)=ptr;
NEXT(back)=X;
EXIT;
}
7. END
DELETING A NODE FROM A LINKED LIST
CHECK IF THE FIRST NODE IS THE DESIRED ONE]
1. If (DATA(list)=’VAL’)then
{
Ptr=LIST;
LIST=NEXT(list);
Delete ptr;
Stop;
}
Back=list;
Ptr=list;
2.While(ptr<>NULL) repeat step 3 to 5

14
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

3.If(DATA(ptr)=’VAL’) then
{
NEXT(back)=NEXT(ptr);
Delete ptr;
Exit;}
4.back=ptr;

5.ptr=next(ptr);

[end of while loop]


6.END

15
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

16
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

17
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

18
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

19
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:3
POLYNOMIAL MANIPULATION USING LIST ADT
Date:

AIM:-

To write a ‘C’ program to represent a polynomial as a linked list and write functions for
polynomial addition

ALGORITHM:-

Let p and q be the two polynomials represented by linked lists


1 . while p and q are not null, repeat
2. if powers of the two terms ate equal
then if the terms do not cancel
then insert the sum of the terms into the sum Polynomial
Advance p
Advance q
Else if the power of the first polynomial> power of second
then insert the term from first polynomial into sum polynomial
Advance p
else insert the term from second polynomial into sum polynomial
Advance q
3. Copy the remaining terms from the non empty polynomial into the sum
polynomial.

20
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

21
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

22
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

23
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

24
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:4
STACK USING ARRAY IN PYTHON
Date:

AIM :

To implement stack ADT using array in Python

ALGORITHM:

Basic Operations

• push() − Pushing (storing) an element on the stack.

• pop() − Removing (accessing) an element from the stack.

• isFull() − check if stack is full.

• isEmpty() − check if stack is empty

Push Operation
The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps

• Checks if the stack is full.

• If the stack is full, produces an error and exit.

• If the stack is not full, increments top to point next empty space.

• Adds data element to the stack location, where top is pointing.

• Returns success.

25
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Pop Operation
In an array implementation of pop() operation, the data element is not actually removed,
instead top is decremented to a lower position in the stack to point to the next value. A Pop
operation may involve the following steps −

• Checks if the stack is empty.

• If the stack is empty, produces an error and exit.

• If the stack is not empty, accesses the data element at which top is pointing.

• Decreases the value of top by 1.

• Returns success.

26
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

27
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

28
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:5a
STACK ADT USING ARRAYS
Date:

AIM :

To implement stack ADT using array in C.

PROGRAM

29
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

30
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

31
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:5b
STACK ADT USING LINKED LIST
Date:

AIM:

To implement stack using linked list.

ALGORITHM:

1. Include all the header files which are used in the program. And declare all the user defined
functions.

2. Define a 'Node' structure with two members data and next.

3. Define a Node pointer 'top' and set it to NULL.

4. Implement the main method by displaying Menu with list of operations and make suitable
function calls in the main method.

push(value) - Inserting an element into the Stack

We can use the following steps to insert a new node into the stack...

• Create a newNode with given value.

• Check whether stack is Empty (top == NULL)

• If it is Empty, then set newNode → next = NULL.

• If it is Not Empty, then set newNode → next = top.

• Finally, set top = newNode.

pop() - Deleting an Element from a Stack

We can use the following steps to delete a node from the stack...

• Check whether stack is Empty (top == NULL).

• If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and


terminate the function

• If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.

• Then set 'top = top → next'.

• Finally, delete 'temp' (free(temp)).

32
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

display() - Displaying stack of elements

We can use the following steps to display the elements (nodes) of a stack...

• Check whether stack is Empty (top == NULL).

• If it is Empty, then display 'Stack is Empty!!!' and terminate the function.

• If it is Not Empty, then define a Node pointer 'temp' and initialize with top.

• Display 'temp → data --->' and move it to the next node. Repeat the same
until temp reaches to the first node in the stack (temp → next != NULL).

• Finally! Display 'temp → data ---> NULL'.

33
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

34
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

35
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:6A
. QUEUE ADT USING ARRAYS
Date:

AIM:

To implement queue ADT using array and its operations.

ALGORITHM:

Basic Operations
• enqueue() − add (store) an item to the queue.

• dequeue() − remove (access) an item from the queue.

• isfull() − Checks if the queue is full.

• isempty() − Checks if the queue is empty.

Enqueue Operation
• Check if the queue is full.

• If the queue is full, produce overflow error and exit.

• If the queue is not full, increment rear pointer to point the next empty space.

• Add data element to the queue location, where the rear is pointing.

• return success.

36
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Dequeue Operation
• Check if the queue is empty.

• If the queue is empty, produce underflow error and exit.

• If the queue is not empty, access the data where front is pointing.

• Increment front pointer to point to the next available data element.

• Return success.

37
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

38
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

39
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

40
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:6b
QUEUE ADT USING LINKED LIST
Date:

AIM:

To implement queue using linked list.

ALGORITHM:

2. Include all the header files which are used in the program. And declare all the user
defined functions.

3. Define a 'Node' structure with two members data and next.

4. Define two Node pointers 'front' and 'rear' and set both to NULL.

5. Implement the main method by displaying Menu of list of operations and make
suitable function calls in the main method to perform user selected operation.

enQueue(value) - Inserting an element into the Queue

We can use the following steps to insert a new node into the queue...

• Create a newNode with given value and set 'newNode → next' to NULL.

• Check whether queue is Empty (rear == NULL)

• If it is Empty then, set front = newNode and rear = newNode.

• If it is Not Empty then, set rear → next = newNode and rear = newNode.

deQueue() - Deleting an Element from Queue

We can use the following steps to delete a node from the queue...

• Check whether queue is Empty (front == NULL).

• If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and


terminate from the function

• If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.

• Then set 'front = front → next' and delete 'temp' (free(temp)).

display() - Displaying the elements of Queue

We can use the following steps to display the elements (nodes) of a queue...

• Check whether queue is Empty (front == NULL).

41
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

• If it is Empty then, display 'Queue is Empty!!!' and terminate the function.

• If it is Not Empty then, define a Node pointer 'temp' and initialize with front.

• Display 'temp → data --->' and move it to the next node. Repeat the same until 'temp'
reaches to 'rear' (temp → next != NULL).

• Finally! Display 'temp → data ---> NULL'.

42
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

43
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

44
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:7
INFIX TO POST FIX CONVERSION
Date:

AIM:-

To write a ‘C’ program to implement stack and use it to convert infix to post fix
expression.

ALGORITHM:

1. Start the program

2. Scan the Infix string from left to right.

3. Initialize an empty stack.

4. If the scanned character is an operand, add it to the Post fix string. If the scanned

character is an operator and if the stack is empty Push the character to stack.

4.1 If the scanned character is an Operand and the stack is not empty,

compare the precedence of the character with the element on top of the stack

(top Stack).

4.2 If top Stack has higher precedence over the scanned character Pop the

stack else Push the scanned character to stack. Repeat this step as long as

stack is not empty and top Stack has precedence over the character.

Repeat this step till all the characters are scanned.

5. (After all characters are scanned, we have to add any character that the stack may

have to the Postfix string.) If stack is not empty add top Stack to Postfix string and

Pop the stack. Repeat this step as long as stack is not empty.

6. Return the Postfix string.

7. Terminate the program.

45
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

46
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

47
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

48
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:8
CIRCULAR QUEUE
Date:

AIM:-

To write a ‘C’ program to implement Circular Queue.

ALGORITHM:

Algorithm to insert an element in a circular queue


1. If (FRONT == 1 and REAR == N) or (FRONT == REAR + 1) Then
2. Print: Overflow
3. Else
4. If (REAR == 0) Then [Check if QUEUE is empty]
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else If (REAR == N) Then [If REAR reaches end if QUEUE]
6. Set REAR = 1
7. Else
8. Set REAR = REAR + 1 [Increment REAR by 1]
[End of Step 4 If]
9. Set QUEUE[REAR] = ITEM
10. Print: ITEM inserted
[End of Step 1 If]
11. Exit

Algorithm for Deletion in a circular queue

1. If (FRONT == 0) Then [Check for Underflow]


2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [If only element is left]
(a) Set FRONT = 0
(b) Set REAR = 0
6. Else If (FRONT == N) Then [If FRONT reaches end if QUEUE]
7. Set FRONT = 1
8. Else
9. Set FRONT = FRONT + 1 [Increment FRONT by 1]
[End of Step 5 If]
10. Print: ITEM deleted
[End of Step 1 If]
11. Exit
49
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

PROGRAM

50
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

51
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

52
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

53
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:9
BINARY SEARCH TREES AND TREE TRAVERSALS
Date:

AIM:-

To write a ‘C’ program to implement binary search tree.

ALGORITHM:-

Binary Search Tree Insertion

1. Start the process.

2. Initialize and declare variables.


3. Construct the Tree

4. Data values are given which we call a key and a binary search tree
5. To search for the key in the given binary search tree, start with the root node

6. Compare the key with the data value of the root node. If they match, return the root
pointer.
7. If the key is less than the data value of the root node, repeat the process by using
the left subtree.

8. Otherwise, repeat the same process with the right subtree until either a match is found
or the subtree under consideration becomes an empty tree.
9. Terminate

Traversal Algorithm

Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)

54
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)

Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.

PROGRAM:

55
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

56
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

57
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

58
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

59
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

EX.No:10
Date: IMPLEMENTATION OF AVL TREE

AIM

To implement the AVL trees and its operations in C

ALGORITHM

1. Perform the normal BST insertion.

2. The current node must be one of the ancestors of the newly inserted node. Update the
height of the current node.

3. Get the balance factor (left subtree height – right subtree height) of the current node.

4. If balance factor is greater than 1, then the current node is unbalanced and we are either
in Left Left case or left Right case. To check whether it is left left case or not, compare
the newly inserted key with the key in left subtree root.

5. If balance factor is less than -1, then the current node is unbalanced and we are either in
Right Right case or Right Left case. To check whether it is Right Right case or not,
compare the newly inserted key with the key in right subtree root.

a) Left Left Case


T1, T2, T3 and T4 are subtrees.
z y
/\ /\
y T4 Right Rotate (z) x z
/\ - - - - - - - - -> / \ /\
x T3 T1 T2 T3 T4
/\
T1 T2

60
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

b) Left Right Case


z z x
/\ /\ /\
y T4 Left Rotate (y) x T4 Right Rotate(z) y z
/\ - - - - - - - - -> / \ - - - - - - - -> / \ / \
T1 x y T3 T1 T2 T3 T4
/\ /\
T2 T3 T1 T2

c) Right Right Case


z y
/ \ / \
T1 y Left Rotate(z) z x
/ \ - - - - - - - -> / \ /\
T2 x T1 T2 T3 T4
/\
T3 T4

d) Right Left Case


z z x
/\ /\ / \
T1 y Right Rotate (y) T1 x Left Rotate(z) z y
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ /\
x T4 T2 y T1 T2 T3 T4
/\ / \
T2 T3 T3 T4

61
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

PROGRAM:

62
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

63
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

64
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

65
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

66
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:11
BREADTH FIRST SEARCH
Date:

AIM:

To represent a graph and do the BFS for a given input graph.

Breadth First Search (BFS):

The aim of BFS algorithm is to traverse the graph as close as possible to the root node. Queue is
used in the implementation of the breadth first search.

Algorithm:

1. Push the root node in the Queue.


2. Loop until the queue is empty.

3. Remove the node from the Queue.


4. If the removed node has unvisited child nodes, mark them as visited and insert the unvisited
children in the queue.

PROGRAM :

67
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

68
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

69
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:12
DEPTH FIRST SEARCH
Date:

AIM:

To represent a graph and do the BFS and DFS graph traversals for a given input graph.

Depth First Search (DFS):

The aim of DFS algorithm is to traverse the graph in such a way that it tries to go far from the
root node. Stack is used in the implementation of the depth first search.
Algorithm:

1. Push the root node in the Stack.


2. Loop until stack is empty.
3. Peek the node of the stack.
4. If the node has unvisited child nodes, get the unvisited child node, mark it as traversed
and push it on stack.
5. If the node does not have any unvisited child nodes, pop the node from the stack.

Program:

70
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

71
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

72
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:13
BUBBLE SORT
Date:

AIM:

To implement bubble sort for sorting the elements in a given array.

ALGORITHM:

1. Starting with the first element (index = 0), compare the current element with the next

element of the array.

2. If the current element is greater than the next element of the array, swap them.

3. If the current element is less than the next element, move to the next element.

4. Repeat Step 1.

PROGRAM:

73
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

74
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:14
SELECTION SORT
Date:

AIM:
To implement the selection sort algorithm in C

ALGORITHM:

1. Set MIN to location 0

2. Search the minimum element in the list

3. Swap with value at location MIN

4. Increment MIN to point to next element

5. Repeat until list is sorted

PROGRAM:

75
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

76
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:15
INSERTION SORT
Date:

AIM:
To implement insertion sort

ALGORITHM :

1. Spilt a list in two parts - sorted and unsorted

2. Pick the first element element

3. Compare with all elements in the sorted sub-list

4. Shift all the elements in the sorted sub-list that is greater than the value to be sorted

5. Insert the value

6. Repeat until list is sorted

PROGRAM:

77
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

78
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:16
LINEAR SEARCH
Date:

AIM:

To implement linear search to search an element or value in a given array.

ALGORITHM:
1. Traverse the array using a for loop.
2. In every iteration, compare the target value with the current value of the array.

2.1 If the values match, return the current index of the array.

2.2 If the values do not match, move on to the next array element.

3. If no match is found, return -1.

PROGRAM:

79
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

80
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:17
BINARY SEARCH
Date:

AIM:

To implement linear search to search an element or value in a given array.

ALGORITHM:

1. The input array that is supposed to be sorted in ascending order.


2. Take two variables which will act as a pointer i.e, beg, and end.
3. Beg will be assigned with 0 and the end will be assigned to the last index of the array.
4. another variable mid will mark the middle of the current array. That will be computed as
(low+high)/2.
5. If the element present at the mid index is equal to the element to be searched, then just return the
mid index.
6. If the element to be searched is smaller than the element present at the mid index, move end to
mid-1, and all RHS will get discarded.
7. If the element to be searched is greater than the element present at the mid index, move beg to
mid+1, and all LHS will get discarded

PROGRAM:

81
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

82
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

83
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:18
Date: HASHING USING LINEAR PROBING

AIM:

To implement hashing using linear probing

ALGORITHM:

1. Start.

2. Given a File of N employee records with a set K of Keys (4-digit) which uniquely

determine the records in file F.

3. Assume that file F is maintained in memory by a Hash Table(HT) of m memory

locations with L as the set of memory addresses (2-digit) of locations in HT.

4. Let the keys in K and addresses in L are Integers

5. Hash function H: K ®L as H(K)=K mod m (remainder method)

6. Hashing as to map a given key K to the address space L, Resolve the collision (if any) is

using linear probing.

7. Stop.

84
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

85
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

86
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:19
Date: HASHING USING QUADRATIC PROBING

AIM:

To implement hashing using Quadratic probing

ALGORITHM:

1. Create an array of structure (i.e a hash table).


2. Take a key and a value to be stored in hash table as input.
3. Corresponding to the key, an index will be generated i.e every key is stored in a
particular array index.
4. Using the generated index, access the data located in that array index.
5. In case of absence of data, create one and insert the data item (key and value) into it and
increment the size of hash table.
6. In case the data exists, probe through the subsequent elements (looping back if
necessary) for free space to insert new data item.
a. This probing will continue until we reach the same element again (from where we
began probing)
b. Here, unlike Linear Probing, probing will be done according to the following
formula
(currentPosition + h) % arraySize => Linear Probing
(currentPosition + (h * h)) % arraySize => Quadratic Probing
where h = 1, 2, 3, 4 and so on.
7. To display all the elements of hash table, element at each index is accessed (via for loop).
8. To remove a key from hash table, we will first calculate its index and delete it if key
matches, else probe through elements until we find key or an empty space where not a
single data has been entered (means data does not exist in the hash table).
9. Exit

87
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

88
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

89
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

90
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:20
DOUBLY LINKED LIST
Date:

AIM

ALGORITHM

91
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

92
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

93
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

Exp. No:21
QUICK SORT
Date:

AIM

ALGORITHM

94
JCS1311/DATA STRUCTURES LAB DEPARTMENT OF CSE 2021-22

95

You might also like