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

Data Structure Lab Manual

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

Data Structure Lab Manual

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

Course Name: Data Structure

Course Code: E1PY203B

School: SCAT Year : 2024-2025

Semester: 2 Semester

Program: MCA Master of computer Application


Session: 5

Submitted By: Submitted To:

Name: Ayush Kabdwal UNNI KRISHNAN

Admission No: 24SCSE2030125

1|Page
Index
Sr. Name of Experiment Page Date of Signature of
No No Experiment faculty
1 Write a program to find the largest and the second largest 3 21-03-2025
elements from an array.
2 Write a program to reverse the contents of an array. 4 21-03-2025
3 Write a program to rotate an array 5 21-03-2025
4 Write a program to merge two sorted arrays. 7 28-03-2025
5 Implement the representation of sparse matrix. 9 28-03-2025
6 Write a program to add two polynomials represented using 12 28-03-2025
two dimensional arrays.
7 Write a program to find the Transpose of a square matrix 14 4-04-2025
without using any additional array. Transpose to be created in
the given matrix itself.
8 Write a program to implement Towers of Hanoi problem. 16 4-04-2025
9 Write a program to implement Ackermann's function. 18 4-04-2025
Y+1 if x = 0
A(x, y)= A(x – 1,1) if y = 0
A(x – 1,A(x, y-1)) otherwise
10 Write a program to create a singly linked list and print the 20 11-04-2025
contents of it
11 Write a program to implement Quick Sort. 22 11-04-2025
12 Write a program to find the Kth largest element from an array 24 25-04-2025
using the technique used in Quick Sort.
13 Write a program to implement Bubble Sort. 26 25-04-2025
14 Write a program to implement insertion sort. 28 16-05-2025
15 Write a program to implement Selection Sort. 30 16-05-2025
16 Implement Linear Search, Write a module to add the element 32 23-05-2025
at the Kth position if the searching is a failure.
17 Write a program to implement Binary search. 35 23-05-2025

2|Page
Objectives 1: Write a program to find the largest and the second largest
elements from an array.
Source code:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 5, 8, 30, 25};
int n = sizeof(arr) / sizeof(arr[0]);
int max = arr[0], second = -1;
for (int i = 1; i < n; i++)
{ if (arr[i] > max) {
second = max;
max = arr[i];
} else if (arr[i] > second && arr[i] != max)
{ second = arr[i];
}
}
printf("Largest: %d\n", max);
printf("Second Largest: %d\n", second);
return 0;
}
Output :
Enter the number of elements in the array: 5
Enter the elements of the array: 10 20 5 8 30 25
Largest: 30
Second Largest: 25

3|Page
Objectives 2: Write a program to reverse the contents of an array.
Source code:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int temp;
for (int i = 0; i < n / 2; i++)
{ temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
printf("Reversed array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Output :
Reversed array: 50 40 30 20 10

4|Page
Objectives 3: Write a program to rotate an array.
Source code:
#include <stdio.h>
void rotateLeft(int arr[], int size)
{ int first = arr[0];
for (int i = 0; i < size - 1; i++)
{ arr[i] = arr[i + 1];
}
arr[size - 1] = first;
}

void printArray(int arr[], int size)


{ for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);

printf("Original array:\n");
printArray(arr, size);

5|Page
rotateLeft(arr, size);

printf("Array after left rotation:\n");


printArray(arr, size);

return 0;
}

Output:
Original array:
12345
Array after left rotation:
23451

6|Page
Objectives 4: Write a program to merge two sorted arrays.
Source code:
#include <stdio.h>
void mergeArrays(int arr1[], int size1, int arr2[], int size2, int merged[])
{ int i = 0, j = 0, k = 0;
while (i < size1 && j < size2)
{ if (arr1[i] < arr2[j]) {
merged[k++] = arr1[i++];
} else {
merged[k++] = arr2[j++];
}
}
while (i < size1)
{ merged[k++] = arr1[i+
+];
}
while (j < size2)
{ merged[k++] = arr2[j+
+];
}
}
void printArray(int arr[], int size)
{ for (int i = 0; i < size; i++)
printf("%d ", arr[i]); printf("\
n");
7|Page
}

8|Page
int main() {
int arr1[] = {1, 3, 5, 7};
int arr2[] = {2, 4, 6, 8, 10};

int size1 = sizeof(arr1) / sizeof(arr1[0]);


int size2 = sizeof(arr2) / sizeof(arr2[0]);

int merged[size1 + size2];

mergeArrays(arr1, size1, arr2, size2, merged);

printf("Merged array: ");


printArray(merged, size1 + size2);

return 0;
}

Output:
Merged array: 1 2 3 4 5 6 7 8 10

9|Page
Objectives 5: Implement the representation of sparse matrix.
Source code:
#include <stdio.h>
#define MAX 100
struct Element {
int row;
int col;
int value;
};
int main() {
int matrix[4][5] = {
{0, 0, 3, 0, 4},
{0, 0, 5, 7, 0},
{0, 0, 0, 0, 0},
{0, 2, 6, 0, 0}
};
struct Element sparse[MAX];
int k = 0; // Counter for non-zero elements
printf("Original Matrix:\n");
for (int i = 0; i < 4; i++)
{ for (int j = 0; j < 5; j++)
{
printf("%d ", matrix[i][j]);
if (matrix[i][j] != 0)
{ sparse[k].row = i;
10 | P a g
e
sparse[k].col = j;
sparse[k].value = matrix[i][j];
k++;
}
}
printf("\n");
}
printf("\nSparse Matrix Representation (row, col, value):\n");
for (int i = 0; i < k; i++) {
printf("%d %d %d\n", sparse[i].row, sparse[i].col, sparse[i].value);
}
return 0;
}

Output:
Original Matrix:
00304
00570
00000
02600

Sparse Matrix Representation (row, col, value):


023
044

10 | P a g e
125
137
312
326

11 | P a g e
Objectives 6: Write a program to add two polynomials represented using two
dimensional arrays.
Source code:
#include <stdio.h>
void addPolynomials(int poly1[][2], int n1, int poly2[][2], int n2) {
int result[20][2], i = 0, j = 0, k = 0;

while (i < n1 && j < n2) {


if (poly1[i][1] > poly2[j][1])
result[k][0] = poly1[i][0], result[k++][1] = poly1[i++][1];
else if (poly1[i][1] < poly2[j][1])
result[k][0] = poly2[j][0], result[k++][1] = poly2[j++][1];
else {
result[k][0] = poly1[i][0] + poly2[j][0];
result[k++][1] = poly1[i][1];
i++, j++;
}
}

while (i < n1)


result[k][0] = poly1[i][0], result[k++][1] = poly1[i++][1];
while (j < n2)
result[k][0] = poly2[j][0], result[k++][1] = poly2[j++][1];

printf("Sum = ");

12 | P a g e
for (int x = 0; x < k; x++) {
printf("%dx^%d", result[x][0], result[x][1]);
if (x < k - 1) printf(" + ");
}
printf("\n");
}

int main() {
// Polynomial 1: 3x^3 + 2x^2 + 5
int poly1[3][2] = {{3, 3}, {2, 2}, {5, 0}};
// Polynomial 2: 4x^3 + 2x + 6
int poly2[3][2] = {{4, 3}, {2, 1}, {6, 0}};

addPolynomials(poly1, 3, poly2, 3);


return 0;
}

Output:
Sum = 7x^3 + 2x^2 + 2x^1 + 11x^0

13 | P a g e
Objectives 7: Write a program to find the Transpose of a square matrix without
using any additional array. Transpose to be created in the given matrix itself.
Source code:
#include <stdio.h>
#define SIZE 3
int main() {
int matrix[SIZE][SIZE] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printf("Original Matrix:\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++)
{ printf("%d ", matrix[i][j]);
}
printf("\n");
}
for (int i = 0; i < SIZE; i++) {
for (int j = i + 1; j < SIZE; j++)
{ int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}

14 | P a g e
printf("\nTransposed Matrix:\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++)
{ printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}

Output:
Original Matrix:
123
456
789

Transposed Matrix:
147
258
369

15 | P a g e
Objectives 8: Write a program to implement Towers of Hanoi problem.
Source code:
#include <stdio.h>
void towersOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 1) {
printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
return;
}
towersOfHanoi(n - 1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towersOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}

int main() {
int n = 3; // Number of disks
printf("Steps to solve Towers of Hanoi for %d disks:\n", n);
towersOfHanoi(n, 'A', 'C', 'B');
return 0;
}

Output:
Steps to solve Towers of Hanoi for 3 disks:
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B

16 | P a g e
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C

17 | P a g e
Objectives 9: Write a program to implement Ackermann's function.
Y+1 if x = 0
A(x, y)= A(x – 1,1) if y = 0
A(x – 1,A(x, y-1)) otherwise
Source code:
#include <stdio.h>
int ackermann(int x, int y)
{ if (x == 0)
return y + 1;
else if (y == 0)
return ackermann(x - 1, 1);
else
return ackermann(x - 1, ackermann(x, y - 1));
}

int main() {
int x, y;
printf("Enter values for x and y: ");
scanf("%d %d", &x, &y);

int result = ackermann(x, y);


printf("Ackermann(%d, %d) = %d\n", x, y, result);

return 0;

18 | P a g e
}

Output:
Enter values for x and y: 2 3
Ackermann(2, 3) = 9

19 | P a g e
Objectives 10: Write a program to create a singly linked list and print the
contents of it
Source code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct
Node)); newNode->data = data;
newNode->next = NULL;
return newNode;
}
void printList(struct Node* head)
{ struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {

20 | P a g e
struct Node* head = createNode(10);
head->next = createNode(20);
head->next->next = createNode(30);

// Print the list


printf("Linked List contents: ");
printList(head);

return 0;
}

Output:
Linked List contents: 10 -> 20 -> 30 -> NULL

21 | P a g e
Objectives 11: Write a program to implement Quick Sort.
Source code:
#include <stdio.h>
void swap(int* a, int* b)
{ int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for(int j = low; j < high; j++)
{ if(arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[high]);
return i + 1;
}
void quickSort(int arr[], int low, int high)
{ if(low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);

22 | P a g e
quickSort(arr, pi + 1, high);
}
}

int main() {
int arr[] = {9, 7, 5, 11, 12, 2};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Original array: ");
for(int i=0; i<n; i++) printf("%d ", arr[i]);
printf("\n");
quickSort(arr, 0, n-1);
printf("Sorted array: ");
for(int i=0; i<n; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}

Output:
Original array: 9 7 5 11 12 2
Sorted array: 2 5 7 9 11 12

23 | P a g e
Objectives 12: Write a program to find the Kth largest element from an array
using the technique used in Quick Sort.
Source code:
#include <stdio.h>
void swap(int *a, int *b)
{ int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Choose last element as pivot
int i = low;
for (int j = low; j < high; j++) {
if (arr[j] >= pivot) { // For Kth largest, we sort in descending order
swap(&arr[i], &arr[j]);
i++;
}
}
swap(&arr[i], &arr[high]);
return i;
}
int kthLargest(int arr[], int low, int high, int k)
{ if (low <= high) {
int pivotIndex = partition(arr, low, high);

24 | P a g e
if (pivotIndex == k - 1) // Found the kth largest element
return arr[pivotIndex];
else if (pivotIndex > k - 1)
return kthLargest(arr, low, pivotIndex - 1, k);
else
return kthLargest(arr, pivotIndex + 1, high, k);
}
return -1; // Not found case (should not happen if k is valid)
}
int main() {
int arr[] = {7, 10, 4, 3, 20, 15};
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3;
int result = kthLargest(arr, 0, n - 1, k);
if(result != -1)
printf("The %dth largest element is %d\n", k, result);
else
printf("Invalid input\n");
return 0;
}

Output:
The 3th largest element is 10

25 | P a g e
Objectives 13: Write a program to implement Bubble Sort.
Source code:
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

26 | P a g e
Output:
Sorted array: 11 12 22 25 34 64 90

27 | P a g e
Objectives 14: Write a program to implement insertion sort.
Source code:
#include <stdio.h>
void insertionSort(int arr[], int n)
{ int i, key, j;
for (i = 1; i < n; i++)
{ key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{ arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");

28 | P a g e
return 0;
}

Output:
Sorted array: 5 6 11 12 13

29 | P a g e
Objectives 15: Write a program to implement Selection Sort.
Source code:
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, min_idx, temp;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");

30 | P a g e
return 0;
}

Output:
Sorted array:
11 12 22 25 64

31 | P a g e
Objectives 16: Implement Linear Search, Write a module to add the element at
the Kth position if the searching is a failure.
Source code:
#include <stdio.h>
#define MAX 100
int linearSearch(int arr[], int n, int key)
{ for (int i = 0; i < n; i++) {
if (arr[i] == key)
return i;
}
return -1;
}
int insertAtPosition(int arr[], int *n, int key, int k)
{ if (k < 0 || k > *n || *n >= MAX) {
printf("Invalid position or array is full.\n");
return 0;
}

for (int i = *n; i > k; i--) {


arr[i] = arr[i - 1];
}
arr[k] = key;
(*n)++;
return 1;
}

32 | P a g e
int main() {
int arr[MAX] = {10, 20, 30, 40, 50};
int n = 5;
int key, k;

printf("Enter element to search: ");


scanf("%d", &key);

int pos = linearSearch(arr, n, key);

if (pos != -1) {
printf("Element %d found at position %d\n", key, pos);
} else {
printf("Element not found.\n");
printf("Enter position (0 to %d) to insert the element: ", n);
scanf("%d", &k);

if (insertAtPosition(arr, &n, key, k))


{ printf("Element inserted. New array:\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
}

33 | P a g e
return 0;
}

Output:
Enter element to search: 25
Element not found.
Enter position (0 to 5) to insert the element: 2
Element inserted. New array:
10 20 25 30 40 50

34 | P a g e
Objectives 17: Write a program to implement Binary search.
Source code:
#include <stdio.h>
int binarySearch(int arr[], int size, int target)
{ int low = 0, high = size - 1;

while (low <= high) {


int mid = (low + high) / 2;

if (arr[mid] == target)
return mid; // Target found at index mid
else if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
}

return -1; // Target not found


}

int main() {
int arr[] = {2, 4, 6, 8, 10, 12, 14};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 10;

35 | P a g e
int result = binarySearch(arr, size, target);

if (result != -1)
printf("Element %d found at index %d.\n", target, result);
else
printf("Element %d not found in the array.\n", target);

return 0;
}

Output:
Element 10 found at index 4.

36 | P a g e

You might also like