Data Structure
Data Structure
Acropolis Institute of
Technology and
Research, Indore Department of CSE
Submitted To: Dr. Mayur Rathi
(Artificial Intelligence & Machine
Learning)
Submitted By:
Sejal Raykhere
Enrollment No. : 0827AL231116
Class/Year/Sem : ALS-2/2nd / 3rd
CERTIFICATE
This is to certify that the experimental work entered in this journal as per
the B. TECH. II year syllabus prescribed by the RGPV was done by Mr./
2024- 202.
In this lab, students will be able to learn and practice basic data structures programming. Students
can expand their skill set by practical learning on various data structures and to understand the
processing of different algorithm for problem-solving. This lab complements the data structures and
computer algorithms courses. Data Structures provides the requisite environment for design and
analysis of algorithms for solving complex problems in the field of computer science. Students gain
practical knowledge by writing and executing programs in C/C++/JAVA/Python using various data
structures and implementing algorithm principles. The latest platforms compilers are provided to the
students to run their programs.
GENERAL INSTRUCTIONS FOR LABORATORY CLASSES
⮚ DO’S
✔ While entering into the LAB students should wear their ID cards.
✔ Students should sign in the LOGIN REGISTER before entering into the
laboratory.
✔ Students should come with observation and record note book to the laboratory.
✔ After completing the laboratory exercise, make sure to shutdown the system
properly.
⮚ DONT’S
Module3: Tree: Definitions - Height, depth, order, degree etc. Binary Search Tree -
Operations, Traversal, Search. AVL Tree, Heap, Applications and comparison of various
types of tree; Introduction to forest, multi-way Tree, B tree, B+ tree, B* tree and red-
black tree.
Module5: Sorting: Introduction, Sort methods like: Bubble Sort, Quick sort. Selection
sort, Heap sort, Insertion sort, Shell sort, Merge sort and Radix sort; comparison of
various sorting techniques. Searching: Basic Search Techniques: Sequential search,
Binary search, Comparison of search methods. Hashing & Indexing. Case Study:
Application of various data structures in operating system, DBMS etc.
HARDWARE AND SOFTWARE REQUIREMENTS:
PREREQUISITE:-
Experience with a high level language (C/C++, Java, Python) is suggested. Prior
knowledge of a Object-Oriented concepts is helpful but not mandatory.
Course Objectives
1. To write and execute programs in any high level language to solve problems using data
structures such as arrays, linked lists.
2. To write and execute programs in any high level language to solve problems using data
structures such as stacks, queues.
3. To write and execute programs in C++ to solve problems using data structures such as
trees, graphs, hash tables and search trees. ¬ To write and execute write programs in C+
+ to implement various sorting and searching methods..
⮚ Course Outcomes
.
Index
Date Pa Date of Grade &
S. of Name of the Experiment ge Submissio Sign of the
No Exp N n Faculty
. o.
1 Program for insertion and deletion in array at
different positions. (CO 1)
Additional remarks
Tutor
1 Title
2 Neatly Drawn and labeled experimental setup
2.1 Theoretical solution of the instant problemAlgorithm
2.2 Program
3 Tabulation Sheet
4 Results
INPUT OUTPUT
Page 10
Additional remarks
Page 11
Title 1
Page 12
2.1 Theoretical Solution of the Instant Problem (Algorithm)
2.2 program
#include <stdio.h>
Page 13
#define MAX_SIZE 100
int main() {
int arr[MAX_SIZE];
int size = 0;
Page 14
int choice, position, value;
do {
printf("\nMenu:\n");
printf("1. Insert Element\n");
printf("2. Delete Element\n");
printf("3. Display Array\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter position to insert (0 to %d): ", size);
scanf("%d", &position);
printf("Enter value to insert: ");
scanf("%d", &value);
insert(arr, &size, position, value);
break;
case 2:
printf("Enter position to delete (0 to %d): ", size - 1);
scanf("%d", &position);
delete(arr, &size, position);
break;
case 3:
display(arr, size);
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
return 0;
}
5 Tabulation Sheet
INPUT OUTPUT
Insert =1,2,3,4,5 [1 2 5 3 4]
Insert 5 at position 2
Delete= 1 2 5 3 6 4 [1 2 5 6 4]
Delete element at position 3
Page 15
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title:
Lab: Data Structure (AL303)
Additional remarks
Tutor
Title2
Page 16
2.1 Theoretical Solution of the Problem
Insertion: Inserting a node into a linked list involves:
1. Creating a new node.
2. Inserting the node at the desired position (either at the beginning, middle, or end).
3. Adjusting the pointers to maintain the structure of the linked list.
Deletion: Deleting a node from a linked list involves:
1. Identifying the position of the node to delete.
2. Adjusting the pointer of the previous node to skip over the node to be deleted.
3. Removing the node from the list to avoid memory leaks.
2.2 program
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
Page 17
struct Node* next;
};
void insertNode(struct Node** head, int position, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (position == 0) {
newNode->next = *head;
*head = newNode;
return;
}
if (temp == NULL) {
printf("Position is out of bounds.\n");
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
if (position == 0) {
*head = temp->next;
free(temp);
return;
}
Page 18
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
do {
printf("\nMenu:\n");
printf("1. Insert Node\n");
printf("2. Delete Node\n");
printf("3. Display List\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
Page 19
case 1:
printf("Enter position to insert (0 for beginning): ");
scanf("%d", &position);
printf("Enter value to insert: ");
scanf("%d", &value);
insertNode(&head, position, value);
break;
case 2
6 Tabulation Sheet
7 Results
INPUT OUTPUT
Inert at begin NULL inert at the 10NULL
beginning
Inert at end 10NULL insert 20 at the 1020NULL
end
Delete at position 051020NULL 1020NULL
delete node at position 0
Page 20
2. Clarity about the Outcome
3. Submitted the work in desired format
4. Shown capability to solve the problem
5. Contribution to the team work
Additional remarks
Title 3
int isFull(Stack* s) {
return s->top == MAX - 1;
}
int isEmpty(Stack* s) {
return s->top == -1;
}
Page 22
int pop(Stack* s) {
if (isEmpty(s)) {
printf("Stack Underflow! Cannot pop\n");
return -1; // Return -1 to indicate an error
}
return s->arr[s->top--];
}
int top(Stack* s) {
if (isEmpty(s)) {
printf("Stack is empty! No top element\n");
return -1; // Return -1 to indicate an error
}
return s->arr[s->top];
}
int main() {
Stack s;
initStack(&s);
push(&s, 10);
push(&s, 20);
push(&s, 30);
return 0;
}
8 Tabulation Sheet
9 Results
INPUT OUTPUT
Pushed 10 Pushed 10 onto the stack
Top Top element is: 100
Popped Popped element is: 10
Page 23
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title:
Lab: Data Structure (AL303)
Page 24
Additional remarks
Tilte 4
Page 25
Page 26
3. Theoretical Solution of the Instant Problem
A stack is a linear data structure that follows the Last In First Out (LIFO) principle. The main
operations of a stack include:
Push: Add an element to the top of the stack.
Pop: Remove the element from the top of the stack.
Top: Retrieve the element at the top of the stack without removing it.
3.1 Algorithm
1. Push Operation:
Check if the stack is full.
If not, increment the top index and add the new element at that index.
2. Pop Operation:
Check if the stack is empty.
If not, retrieve the element at the top index and decrement the top index.
3. Top Operation:
Check if the stack is empty.
If not, return the element at the top index.
3.2 program
#include <stdio.h>
#include <stdlib.h>
void initQueue(Queue* q) {
q->front = 0; // Front index
q->rear = -1; // Rear index
}
int isFull(Queue* q) {
return q->rear == MAX - 1;
}
int isEmpty(Queue* q) {
return q->front > q->rear;
}
Page 27
if (isFull(q)) {
printf("Queue Overflow! Cannot enqueue %d\n", value);
return;
}
q->arr[++q->rear] = value;
printf("Enqueued %d into the queue\n", value);
}
int dequeue(Queue* q) {
if (isEmpty(q)) {
printf("Queue Underflow! Cannot dequeue\n");
return -1; // Return -1 to indicate an error
}
return q->arr[q->front++];
}
int main() {
Queue q;
initQueue(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
enqueue(&q, 40);
enqueue(&q, 50);
while (!isEmpty(&q)) {
printf("Dequeued element is: %d\n", dequeue(&q));
}
return 0;
}
10 Tabulation Sheet
11 Results
INPUT OUTPUT
Enqueued 10 Enqueued 10 into the queue
Enqueued 20 Enqueued 20 into the queue
Page 28
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title:
Lab: Data Structure (AL303)
Additional remarks
Tilte 5
int main() {
Node* root = createNode
('A');
root->left = createNode('B');
root->right = createNode('C');
root->left->left = createNode('D');
root->left->right = createNode('E');
root->right->right = createNode('F');
Page 30
}
Tabulation Sheet
INPUT OUTPUT
Create A Node created with data 'A'
Create B Node created with data 'B'
Create C Node created with data 'C'
Create D Node created with data 'D'
Create E Node created with data 'E'
Create F Node created with data 'F'
In-order Traversal In-order Traversal
Results
The program successfully creates a binary tree with the following structure:
A
/\
B C
/\ \
D E F
In-order Traversal of the Binary Tree: D B E A C F
Page 32
Additional remarks
Page 33
Tilte 6
Page 34
Page 35
3. Theoretical Solution of the Instant Problem
Selection Sort is a simple comparison-based sorting algorithm. It works by dividing the input list
into two parts: a sorted part and an unsorted part. The algorithm repeatedly selects the smallest
(or largest, depending on the order) element from the unsorted part and moves it to the end of the
sorted part.
3.1 Algorithm
1. Start with the first element as the minimum.
2. Compare this minimum with the other elements in the array.
3. If a smaller element is found, update the minimum.
4. After completing the comparisons, swap the minimum element with the first element of
the unsorted part.
5. Move the boundary of the sorted part one element to the right.
6. Repeat the process for the remaining unsorted elements until the entire array is sorted.
3.2 Program
#include <stdio.h>
Page 36
printf("Initial Array: ");
printArray(arr, n);
selectionSort(arr, n);
return 0;
}
Tabulation Sheet
INPUT OUTPUT
[64, 25, 12, 22, 11] Find minimum (11), swap with 64
[11, 25, 12, 22, 64] Find minimum (12), swap with 25
[11, 12, 25, 22, 64] Find minimum (22), swap with 25
[11, 12, 22, 25, 64] No swap needed (already sorted)
[11, 12, 22, 25, 64] Final sorted array
Results
The program successfully implements the Selection Sort algorithm. The output shows the initial
and sorted arrays:
Initial Array: 64 25 12 22 11
Sorted Array: 11 12 22 25 64
Page 37
Acropolis Institute of Technology and Research, Indore
Department of CSE (Artificial Intelligence & Machine
Learning)
Group / Title:
Lab: Data Structure (AL303)
Additional remarks
Tilte 7
Page 38
Page 39
3. Theoretical Solution of the Instant Problem
Insertion Sort is a simple and intuitive sorting algorithm that builds a sorted array (or list) one
element at a time. It works similarly to the way you might sort playing cards in your hands. The
algorithm divides the input list into a sorted and an unsorted part. It repeatedly takes the first
element from the unsorted part and inserts it into the correct position in the sorted part.
3.1 Algorithm
1. Start from the second element (index 1) of the array.
2. Compare the current element with the elements in the sorted part (to its left).
3. Shift all elements in the sorted part that are greater than the current element to the right.
4. Insert the current element into its correct position.
5. Repeat the process for all elements in the array until the entire array is sorted
3.2 program
#include <stdio.h>
void insertionSort(int arr[], int n) {
int i, key, j;
Page 40
insertionSort(arr, n);
return 0;
}
12 Tabulation Sheet
13
INPUT OUTPUT
[64, 25, 12, 22, 11] Insert 25 before 64
[25, 64, 12, 22, 11] Insert 12 before 25
[12, 25, 64, 22, 11] Insert 22 before 64
[12, 22, 25, 64, 11] Insert 11 before 12
[11, 12, 22, 25, 64] Final sorted array
Results
The program successfully implements the Insertion Sort algorithm. The output shows the
initial and sorted array
Initial Array: 64 25 12 22 11
Sorted Array: 11
Page 41
Page 42
Page 43
3. Theoretical Solution of the Instant Problem
Quick Sort is a highly efficient sorting algorithm and is based on partitioning an array into
smaller sub-arrays. A pivot element is selected from the array, and the other elements are
rearranged so that those less than the pivot come before it and those greater come after it. The
sub-arrays are then sorted recursively.
3.1 Algorithm
1. Choose a Pivot: Select an element from the array as the pivot (commonly the last
element).
2. Partitioning: Rearrange the array so that elements less than the pivot are on the left, and
elements greater than the pivot are on the right.
3. Recursion: Recursively apply the above steps to the sub-arrays formed by the
partitioning.
4. Base Case: If the array has one or zero elements, it is already sorted.
3.2 program
#include <stdio.h>
Page 44
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
return 0;
}
INPUT OUTPUT
[64, 25, 12, 22, 11] Choose pivot (22)
[11, 12, 22, 64, 25] Partitioning around pivot (22)
Left Sub-array: [11, 12] Already sorted
Right Sub-array: [64, 25] Choose pivot (25)
[11, 12, 22, 25, 64] Final sorted array
Page 45
Results
The program successfully implements the Quick Sort algorithm. The output shows the
initial and sorted arrays:
Initial Array: 64 25 12 22 11
Sorted Array: 11 12
Page 46
Additional remarks
Tilte 9
Page 47
3. Theoretical Solution of the Instant Problem
Merge Sort is a divide-and-conquer algorithm that sorts an array by recursively dividing it into
two halves, sorting each half, and then merging the sorted halves back together. It is particularly
effective for large datasets and has a guaranteed time complexity of O(n log n).
3.1 Algorithm
1. Divide: Split the array into two halves until each sub-array contains one element.
2. Conquer: Recursively sort each half.
3. Combine: Merge the two sorted halves into a single sorted array.
3.2 Program
#include <stdio.h>
void merge(int arr[], int left, int mid, int right) {
int i, j, k;
int n1 = mid - left + 1; // Size of left sub-array
int n2 = right - mid;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j]
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
Page 48
}
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
return 0;
}
Page 49
Name Sejal Raykhere Enrollment No. 0827AL231116
Performing on First submission Second submission
Extra Regular
Additional remarks
Page 50
Tilte 10
Page 51
3. Theoretical Solution of the Instant Problem
Binary Search is an efficient algorithm for finding an item from a sorted list of items. It works by
repeatedly dividing the search interval in half. If the value of the search key is less than the item
in the middle of the interval, the search continues in the lower half, or if it is greater, it continues
in the upper half. This process repeats until the value is found or the interval is empty.
3.1 Algorithm
1. Set two pointers, low and high, to the first and last index of the array, respectively.
2. While low is less than or equal to high:
Calculate the middle index: mid = (low + high) / 2.
If the target value is equal to the middle element, return the index.
If the target value is less than the middle element, adjust high to mid - 1.
If the target value is greater than the middle element, adjust low to mid + 1.
3. If the target value is not found, return -1.
3.2 program
#include <stdio.h>
if (arr[mid] == target) {
return mid; // Target found
}
else {
low = mid + 1;
}
}
return -1; // Target not found
}
void printArray(int arr[], int size) {
Page 52
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {11, 12, 22, 25, 64};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 22;
if (result != -1) {
printf("Target %d found at index: %d\n", target, result);
} else {
printf("Target %d not found in the array.\n", target);
}
return 0;
}
14 Tabulation Sheet
15
INPUT OUTPUT
[11, 12, 22, 25, 64] Compare arr[2] (22) with target (22)
Results
The program successfully implements the Binary Search algorithm. The output shows the initial
sorted array and the result of the search:
Initial Sorted Array: 11 12 22 25 64
Target 22 found at index: 2
Page 53
Page 54