Data Structure Lab Manual
Data Structure Lab Manual
Engineering
Department of Information Technology &Engineering
DATA STRUCTURES
(SECE2221)|[Link].
SHRUTHI
Name of Student:
Mr./Ms. SHRUTHI
Engineering having
(SECE2221).
Date: Date:
CONTENTS
2. One-dimensional array
4 28.06.2024
3. Data Structure -
7 28.06.2024
4. Structure and Function. 12 05.07.2024
PRACTICAL NO: - 1
AIM:
Write a program to create memory for int, char, and float variables at a run time.
ALGORITHM:
Here's a simple algorithm to create memory for int, char, and float variables at runtime:
1. Start
2. Include Required Header: Include the <stdlib.h> header file for memory
allocation functions and <stdio.h> for input/output functions.
3. Allocate Memory:
Once you're done using the allocated memory, use free to release it.
[Link]
CODING :
#include <stdio.h>
#include <stdlib.h>
int main() {
// 1. Allocate memory for an int
int *intPtr = (int *)malloc(sizeof(int));
if (intPtr == NULL) {
printf("Memory allocation failed for int.\n");
return 1; // Exit the program with an error code
OUTPUT:
SHRUTI KATARIYA 2
23SE02IT030
RESULT:
SHRUTI KATARIYA 3
23SE02IT030
PRACTICAL NO: - 2
AIM:
C program to read a one-dimensional array, and print sum of all elements along with
inputted array elements.
ALGORITHM:
1. Start
2. Input the Size of the Array
Prompt the user to enter the number of elements for the array.
Read the integer value provided by the user and store it in a variable n.
3. Declare the Array
Declare an integer array array with a size of n.
4. Input Array Elements
Print a prompt asking the user to enter n elements for the array.
Use a loop to read n integer values from the user and store each value in
the array.
5. Compute the Sum of Array Elements
Initialize a variable sum to zero.
Use a loop to iterate through each element of the array and add each
element's value to sum.
6. Print Array Elements
Print a message indicating that the array elements are being displayed.
Use a loop to print each element of the array on the same line.
7. Print the Sum
Print a message indicating the sum of the array elements.
Display the computed sum.
8. End
CODING:
#include <stdio.h>
int main() {
SHRUTI KATARIYA 4
23SE02IT030
scanf("%d", &n);
int array[n];
scanf("%d", &array[i]);
sum += array[i];
printf("Array elements:\n");
SHRUTI KATARIYA 5
23SE02IT030
printf("\n");
return 0;
OUTPUT:
RESULT:
SHRUTI KATARIYA 6
23SE02IT030
PRACTICAL NO: -3
AIM:
Develop a structure to represent planets in the solar system. Each planet has the
fieldfor the planets name, its distance from the sun in miles and the number of moons it
[Link] a program to read the data for each planet and store. Also print the name of
theplanet that has the highest number of moons.
ALGORITHM:
1. Start
Create a struct to represent each planet, with fields for the planet's name,
distance from the sun (in miles), and the number of moons.
Prompt the user to input data for each planet: name, distance from the sun, and
number of moons.
Traverse the array of structures to find the planet with the highest number of
moons.
Print the name of the planet with the highest number of moons.
CODING:
#include <stdio.h>
#include <string.h>
SHRUTI KATARIYA 7
23SE02IT030
struct Planet {
char name[50];
int number_of_moons;
};
scanf("%s", p->name);
scanf("%lf", &p->distance_from_sun);
scanf("%d", &p->number_of_moons);
max_moons_planet = planets[i];
SHRUTI KATARIYA 8
23SE02IT030
return max_moons_planet;
int main() {
int num_planets;
scanf("%d", &num_planets);
inputPlanetData(&planets[i]);
SHRUTI KATARIYA 9
23SE02IT030
most_moons_planet.name, most_moons_planet.number_of_moons);
return 0;
OUTPUT:
SHRUTI KATARIYA 10
23SE02IT030
RESULT:
SHRUTI KATARIYA 11
23SE02IT030
PRACTICAL NO: - 4
AIM;
Write a c program to Add Two Complex Numbers by Passing Structure to a Function.
ALGORITHM:
1. Start
Create a struct to represent a complex number, with fields for the real and
imaginary parts.
Prompt the user to enter the real and imaginary parts of two complex numbers.
Pass the complex numbers to the addition function and get the result.
CODING:
#include <stdio.h>
#include <string.h>
struct Planet {
SHRUTI KATARIYA 12
23SE02IT030
char name[50];
int number_of_moons;
};
scanf("%s", p->name);
scanf("%lf", &p->distance_from_sun);
scanf("%d", &p->number_of_moons);
max_moons_planet = planets[i];
return max_moons_planet;
SHRUTI KATARIYA 13
23SE02IT030
int main() {
int num_planets;
scanf("%d", &num_planets);
inputPlanetData(&planets[i]);
most_moons_planet.name, most_moons_planet.number_of_moons);
SHRUTI KATARIYA 14
23SE02IT030
return 0;
OUTPUT:
RESULT:
Above program of adding two complex numbers using structures and functions is
executed successfully.
SHRUTI KATARIYA 15
23SE02IT030
PRACTICAL NO: -5
AIM:
Write a c program to store Student Information using Structures
ALGORITHM:
1. Start
Create a struct to represent a student with fields for the student’s name, roll
number, and grade.
Write a function to prompt the user to enter the student's details and store them
in a struct.
6. Main Function:
In the main() function, use the above functions to input and display student
information.
7. End
CODING:
#include <stdio.h>
#include <string.h>
SHRUTI KATARIYA 16
23SE02IT030
struct Student {
char name[100];
int roll_number;
char grade;
};
scanf("%d", &s->roll_number);
printf("\nStudent Information:\n");
SHRUTI KATARIYA 17
23SE02IT030
int main() {
int num_students;
scanf("%d", &num_students);
inputStudentInfo(&students[i]);
displayStudentInfo(students[i]);
return 0;
SHRUTI KATARIYA 18
23SE02IT030
OUTPUT:
SHRUTI KATARIYA 19
23SE02IT030
RESULT:
SHRUTI KATARIYA 20
23SE02IT030
PRACTICAL NO: - 6
AIM:
Write program to perform Insertion sort.
ALGORITHM:
1. Initialize:
Compare the key with the elements before it. If the key is smaller than
the compared element, shift the compared element one position to the
right.
3. Insert Key:
4. Repeat:
Repeat steps 2-3 for all elements in the unsorted part of the list
CODING:
#include <stdio.h>
SHRUTI KATARIYA 21
23SE02IT030
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
return 0;
}
OUTPUT:
SHRUTI KATARIYA 22
23SE02IT030
RESULT:
SHRUTI KATARIYA 23
23SE02IT030
PRACTICAL NO: -7
AIM:
ALGORITHM:
1. Initialize:
For the current position, find the minimum element in the unsorted portion of
the array (from the current position to the end).
3. Swap:
Swap the found minimum element with the element at the current position.
Move to the next position and repeat the process until the entire array is sorted.
CODING:
#include <stdio.h>
SHRUTI KATARIYA 24
23SE02IT030
min_index = i;
min_index = j;
// Swap the found minimum element with the first element of the unsorted portion
if (min_index != i) {
temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
int i;
printf("\n");
int main() {
SHRUTI KATARIYA 25
23SE02IT030
printArray(arr, n);
selectionSort(arr, n);
printArray(arr, n);
return 0;
OUTPUT:
RESULT:
SHRUTI KATARIYA 26
23SE02IT030
PRACTICAL NO: -8
AIM:
Write a program to perform Bubble sort.
ALGORITHM:
3. If the current element is greater than the next element, swap them.
5. Repeat steps 2-4 for each pair of adjacent elements, until the end of the list is
reached. This completes one pass.
6. Repeat the entire process for the remaining elements (excluding the last sorted
elements) until no more swaps are needed.
7. End when no swaps are made in a full pass through the list, indicating the list is
sorted.
CODING:
#include <stdio.h>
SHRUTI KATARIYA 27
23SE02IT030
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1; // A swap has occurred
}
}
// If no two elements were swapped by the inner loop, then the array is sorted
if (swapped == 0) {
break;
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
return 0;
}
OUTPUT:
SHRUTI KATARIYA 28
23SE02IT030
RESULT:
SHRUTI KATARIYA 29
23SE02IT030
PRACTICAL NO: - 9
AIM:
Write a program to perform linear search.
ALGORITHM:
1. Start
3. Initialize: i = 0
[Link] i by 1
[Link]
CODING:
#include <stdio.h>
SHRUTI KATARIYA 30
23SE02IT030
int main() {
int arr[] = {5, 7, 12, 23, 34, 45, 56}; // Example array
int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
int target;
return 0;
}
OUTPUT:
RESULT:
SHRUTI KATARIYA 31
23SE02IT030
ALGORITHM:
1. Initialize:
2. While Loop:
3. Return:
If the loop exits, return an indication that the target is not in the array (e.g., -1).
CODING:
#include <stdio.h>
SHRUTI KATARIYA 32
23SE02IT030
int main() {
// Example usage
int arr[] = {2, 3, 4, 10, 40}; // Must be sorted
int size = sizeof(arr) / sizeof(arr[0]);
int target = 10;
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found in array\n");
}
return 0;
}
OUTPUT:
RESULT:
Above program of binary search is executed successfully.
SHRUTI KATARIYA 33
23SE02IT030
AIM:
Write a program to implement stack and perform push, pop operation.
ALGORITHM:
1. Initialization
Create a stack with a fixed size (if using an array) or a dynamic size (if using a linked
list).
Initialize the stack's top index (or pointer) to indicate an empty stack.
2. Push Operation
3. Pop Operation
CODING:
#include <stdio.h>
#include <stdlib.h>
SHRUTI KATARIYA 34
23SE02IT030
int main() {
Stack s;
initialize(&s);
SHRUTI KATARIYA 35
23SE02IT030
display(&s);
return 0;
}
OUTPUT:
RESULT:
Above program stack and perform push, pop operation is executed successfully
SHRUTI KATARIYA 36
23SE02IT030
PRACTICAL NO: - 12
AIM:
Write a program to perform the following operations in linear queue – Addition,
Deletion and Traversing
ALGORITHM:
1. Initialization
2. Addition (Enqueue)
Check if the queue is full. If rear is equal to MAX_SIZE - 1, print "Queue is full."
Otherwise, increment rear and add the new element to the position pointed to
by rear.
If front is -1, set front to 0.
3. Deletion (Dequeue)
4. Traversal
CODING:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int items[MAX_SIZE];
int front, rear;
} Queue;
SHRUTI KATARIYA 37
23SE02IT030
q->front = -1;
q->rear = -1;
}
SHRUTI KATARIYA 38
23SE02IT030
int main() {
Queue q;
initializeQueue(&q);
traverse(&q);
dequeue(&q);
traverse(&q);
enqueue(&q, 40);
traverse(&q);
return 0;
}
OUTPUT:
SHRUTI KATARIYA 39
23SE02IT030
RESULT:
SHRUTI KATARIYA 40
23SE02IT030
Write a program to perform the following operations in singly linked list – Creation,
Insertion, and Deletion.
ALGORITHM:
1. Creation
2. Insertion
At the Beginning:
o Create a new node.
o Set its data to the new value.
o Set its next to the current head.
o Update the head to the new node.
At the End:
o Create a new node.
o Set its data to the new value.
o Traverse the list to find the last node.
o Set the next of the last node to the new node.
At a Given Position:
o Create a new node.
o Traverse the list to the position before where you want to insert the new
node.
o Set the new node’s next to the next node of the current node.
o Update the next of the current node to the new node.
3. Deletion
SHRUTI KATARIYA 41
23SE02IT030
At a Given Position:
o Traverse the list to the node before the position to be deleted.
o Set the next of this node to the node after the node to be deleted.
o Free the memory of the node to be deleted.
CODING:
#include <stdio.h>
#include <stdlib.h>
SHRUTI KATARIYA 42
23SE02IT030
int main() {
int choice, data;
while (1) {
printf("\nMenu:\n");
printf("1. Insert at beginning\n");
printf("2. Insert at end\n");
SHRUTI KATARIYA 43
23SE02IT030
SHRUTI KATARIYA 44
23SE02IT030
SHRUTI KATARIYA 45
23SE02IT030
SHRUTI KATARIYA 46
23SE02IT030
RESULT:
SHRUTI KATARIYA 47
23SE02IT030
ALGORITHM:
1. Initialize:
2. Create Nodes:
SHRUTI KATARIYA 48
23SE02IT030
o If the node to delete is the head, update the head to the next node and
update the last node's next to point to the new head.
o For any other node, update the next pointer of the previous node to skip
the node being deleted.
CODING:
#include <stdio.h>
#include <stdlib.h>
SHRUTI KATARIYA 49
23SE02IT030
}
}
SHRUTI KATARIYA 50
23SE02IT030
int main() {
int choice, data;
while (1) {
printf("\nMenu:\n");
printf("1. Insert at beginning\n");
printf("2. Insert at end\n");
printf("3. Delete from beginning\n");
printf("4. Delete from end\n");
printf("5. Display list\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insertAtBeginning(data);
break;
case 2:
printf("Enter data to insert: ");
scanf("%d", &data);
insertAtEnd(data);
break;
case 3:
deleteFromBeginning();
break;
case 4:
deleteFromEnd();
break;
case 5:
displayList();
break;
case 6:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT:
SHRUTI KATARIYA 51
23SE02IT030
SHRUTI KATARIYA 52
23SE02IT030
SHRUTI KATARIYA 53
23SE02IT030
RESULT:
Above program of Circular linekd list operation is executed successfully.
SHRUTI KATARIYA 54
23SE02IT030
ALGORITHM:
Initialize:
o Start with an empty list.
o Set head and tail to NULL.
Create Nodes:
o For each element in the provided data list:
1. Allocate memory for a new node.
2. Set the node's data.
3. Update the next pointer of the previous node to point to the new
node.
4. Update the prev pointer of the new node to point to the previous
node.
5. Update the tail to point to the new node.
6. Ensure the next pointer of the new tail is NULL.
SHRUTI KATARIYA 55
23SE02IT030
CODING:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
};
SHRUTI KATARIYA 56
23SE02IT030
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
if (head == NULL) {
head = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
if (head == NULL) {
head = newNode;
} else {
SHRUTI KATARIYA 57
23SE02IT030
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
void deleteFromBeginning() {
if (head == NULL) {
printf("List is empty.\n");
} else {
head = head->next;
if (head != NULL) {
head->prev = NULL;
free(temp);
SHRUTI KATARIYA 58
23SE02IT030
void deleteFromEnd() {
if (head == NULL) {
printf("List is empty.\n");
free(head);
head = NULL;
} else {
temp = temp->next;
temp->prev->next = NULL;
free(temp);
void displayList() {
if (head == NULL) {
printf("List is empty.\n");
} else {
SHRUTI KATARIYA 59
23SE02IT030
temp = temp->next;
printf("NULL\n");
int main() {
while (1) {
printf("\nMenu:\n");
printf("6. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
insertAtBeginning(data);
break;
SHRUTI KATARIYA 60
23SE02IT030
case 2:
scanf("%d", &data);
insertAtEnd(data);
break;
case 3:
deleteFromBeginning();
break;
case 4:
deleteFromEnd();
break;
case 5:
displayList();
break;
case 6:
exit(0);
default:
return 0;
OUTPUT:
SHRUTI KATARIYA 61
23SE02IT030
SHRUTI KATARIYA 62
23SE02IT030
SHRUTI KATARIYA 63
23SE02IT030
RESULT:
SHRUTI KATARIYA 64
23SE02IT030
ALGORITHM:
CODING:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
SHRUTI KATARIYA 65
23SE02IT030
};
newNode->data = data;
newNode->next = NULL;
return newNode;
if (top == NULL) {
top = newNode;
} else {
newNode->next = top;
top = newNode;
SHRUTI KATARIYA 66
23SE02IT030
void pop() {
if (top == NULL) {
printf("Stack is empty.\n");
} else {
top = top->next;
free(temp);
void display() {
if (top == NULL) {
printf("Stack is empty.\n");
} else {
temp = temp->next;
printf("NULL\n");
SHRUTI KATARIYA 67
23SE02IT030
int main() {
while (1) {
printf("\nMenu:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
push(data);
break;
case 2:
pop();
break;
case 3:
display();
SHRUTI KATARIYA 68
23SE02IT030
break;
case 4:
exit(0);
default:
return 0;
OUTPUT:
SHRUTI KATARIYA 69
23SE02IT030
SHRUTI KATARIYA 70
23SE02IT030
RESULT:
SHRUTI KATARIYA 71
23SE02IT030
ALGORITHM:
Start from the front pointer and traverse through the nodes, printing their data
CODING:
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
// Queue structure
SHRUTI KATARIYA 72
23SE02IT030
struct Queue {
struct Node *front, *rear;
};
if (q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
if (q->front == NULL)
q->rear = NULL;
free(temp);
}
SHRUTI KATARIYA 73
23SE02IT030
int main() {
struct Queue* q = createQueue();
int choice, value;
while (1) {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to be inserted: ");
scanf("%d", &value);
enqueue(q, value);
break;
case 2:
dequeue(q);
break;
case 3:
display(q);
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
OUTPUT:
SHRUTI KATARIYA 74
23SE02IT030
SHRUTI KATARIYA 75
23SE02IT030
RESULT:
SHRUTI KATARIYA 76
23SE02IT030
AIM:
Write a program to create a binary search tree and perform – Insertion, Deletion, and
Traversal.
ALGORITHM:
1. Insertion:
2. Deletion:
3. Traversal:
In-order Traversal: Visit the left subtree, then the current node, then
the right subtree.
Pre-order Traversal: Visit the current node, then the left subtree, then
the right subtree.
Post-order Traversal: Visit the left subtree, then the right subtree, then
the current node
CODING:
#include <stdio.h>
#include <stdlib.h>
SHRUTI KATARIYA 77
23SE02IT030
int data;
} TreeNode;
newNode->data = data;
return newNode;
if (root == NULL) {
return createNode(data);
} else {
SHRUTI KATARIYA 78
23SE02IT030
return root;
root = root->left;
return root;
if (root == NULL) {
return root;
} else {
if (root->left == NULL) {
SHRUTI KATARIYA 79
23SE02IT030
free(root);
return temp;
free(root);
return temp;
root->data = temp->data;
return root;
if (root != NULL) {
inorderTraversal(root->left);
inorderTraversal(root->right);
int main() {
SHRUTI KATARIYA 80
23SE02IT030
while (1) {
printf("1. Insert\n");
printf("2. Delete\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
break;
case 2:
scanf("%d", &data);
SHRUTI KATARIYA 81
23SE02IT030
break;
case 3:
inorderTraversal(root);
printf("\n");
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
return 0;
OUTPUT:
SHRUTI KATARIYA 82
23SE02IT030
SHRUTI KATARIYA 83
23SE02IT030
RESULT:
SHRUTI KATARIYA 84
23SE02IT030
ALGORITHM:
1. Creation:
2. Insertion:
To insert a node, traverse the tree to find the correct location (based on binary search
tree properties if it's a binary search tree).
Insert the node in the appropriate position (left or right of the current node).
3. Deletion:
CODING:
#include <stdio.h>
#include <stdlib.h>
SHRUTI KATARIYA 85
23SE02IT030
SHRUTI KATARIYA 86
23SE02IT030
free(root);
return temp;
}
// Node with two children: Get the inorder successor (smallest in the right
subtree)
struct Node* temp = findMin(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}
int main() {
struct Node* root = NULL;
int choice, data;
while (1) {
printf("\n1. Insert\n2. Delete\n3. Inorder Traversal\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insertNode(root, data);
break;
case 2:
printf("Enter data to delete: ");
scanf("%d", &data);
root = deleteNode(root, data);
break;
case 3:
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n");
break;
case 4:
exit(0);
SHRUTI KATARIYA 87
23SE02IT030
default:
printf("Invalid choice!\n");
}
}
return 0;
}
OUTPUT:
SHRUTI KATARIYA 88
23SE02IT030
RESULT:
SHRUTI KATARIYA 89