Divyanshu dsa file1
Divyanshu dsa file1
Divyanshu dsa file1
GREATER NOIDA
Department of CSE(IoT)
School of Computer Sciences in Emerging Technologies
Lab File
of
Affiliated to Dr. A.P.J Abdul Kalam Technical University, Uttar Pradesh, Lucknow
Index
B. TECH. SECOND YEAR (CSE(IoT)) 3rd Sem
Lab Experiments
Experiment- 1
AIM- Construct a program to compare the time complexities of selection, bubble
and insertion sort by plotting the graph
Program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
min_idx = i;
min_idx = j;
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
int i, j, temp;
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
int i, key, j;
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int main() {
double time_used;
start = clock();
selectionSort(arr, n);
end = clock();
start = clock();
bubbleSort(arr, n);
end = clock();
start = clock();
insertionSort(arr, n);
end = clock();
return 0;
OUTPUT:
Experiment-2
AIM- Construct a program to compare the time complexities of various
algorithms by varying size “n”.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
if (arr[i] == x) return;
arr[j] = arr[j+1];
arr[j+1] = temp;
if (arr[mid] == x) return;
int main() {
srand(time(NULL));
int n = sizes[i];
start = clock();
linearSearch(arr, n, -1);
end = clock();
start = clock();
bubbleSort(arr, n);
end = clock();
start = clock();
binarySearch(arr, n, -1);
end = clock();
free(arr);
return 0;
OUTPUT:
Experiment- 3
AIM- Construct a Code to find the maximum element in an array.
Program:
#include <stdio.h>
max = arr[i];
return max;
int main() {
return 0;
OUTPUT:
Experiment- 4
AIM- Construct a Code to calculate the sum of all elements in an array.
Program:
#include <stdio.h>
int sum = 0;
sum += arr[i];
return sum;
int main() {
return 0;
OUTPUT:
Experiment- 5
AIM- Construct a Code to reverse the elements of an array.
Program:
#include <stdio.h>
int temp;
temp = arr[i];
arr[i] = arr[n-i-1];
arr[n-i-1] = temp;
int main() {
reverseArray(arr, n);
printf("\n");
return 0;
OUTPUT:
Experiment- 6
AIM- Construct a Code to check if an array is sorted in ascending order.
Program:
#include <stdio.h>
return 0;
return 1;
int main() {
if (isSorted(arr, n)) {
} else {
return 0;
OUTPUT:
Experiment- 7
AIM- Construct a Code to count the occurrence of a specific element in an
array.
Program:
#include <stdio.h>
int count = 0;
if (arr[i] == x)
count++;
return count;
int main() {
int x = 2;
return 0;
}OUTPUT:
Experiment- 8
AIM- Construct a Code creation and traversal of 2D Array in row major and
column major order.
Program:
#include <stdio.h>
int value = 1;
arr[i][j] = value++;
printf("Row-major order:\n");
printf("\n");
printf("Column-major order:\n");
printf("\n");
int main() {
int arr[rows][cols];
return 0;
OUTPUT:
Experiment- 9
AIM- Construct a code to print the transpose of a given matrix using function
Program:
#include <stdio.h>
printf("\n");
int main() {
transpose(3, 3, matrix);
return 0;
OUTPUT:
Experiment- 10
AIM- Program to find if a given matrix is Sparse or Not and print Sparse Matrix.
Program:
#include <stdio.h>
int zeroCount = 0;
if (matrix[i][j] == 0)
zeroCount++;
int main() {
if (isSparse(3, 3, matrix))
printf("Matrix is sparse\n");
else
return 0;
OUTPUT:
Experiment- 11
AIM- Construct a code to represent a sparse matrix in triplet form.
Program:
#include <stdio.h>
int k = 0;
if (matrix[i][j] != 0) {
triplet[k][0] = i;
triplet[k][1] = j;
triplet[k][2] = matrix[i][j];
k++;
int main() {
toTripletForm(3, 3, matrix);
return 0;
OUTPUT:
Experiment- 12
AIM- Construct a code to Implement Linear Search.
Program:
#include <stdio.h>
if (arr[i] == target) {
return i;
return -1;
int main() {
int target = 7;
if (result == -1) {
} else {
return 0;
OUTPUT:
Experiment- 13
AIM- Construct a code to implement Binary Search.
Program:
#include <stdio.h>
if (arr[mid] == target) {
return mid;
left = mid + 1;
} else {
right = mid - 1;
return -1;
int main() {
int target = 5;
if (result == -1) {
} else {
return 0;
OUTPUT:
Experiment- 14
AIM- Construct a program to Implement Selection Sort
Program:
#include <stdio.h>
int minIndex = i;
minIndex = j;
arr[minIndex] = arr[i];
arr[i] = temp;
int main() {
int size = 5;
selectionSort(arr, size);
return 0;
OUTPUT:
Experiment- 15
AIM- Construct a program to Implement Bubble Sort
Program:
#include <stdio.h>
arr[j + 1] = temp;
int main() {
int size = 7;
bubbleSort(arr, size);
return 0;
OUTPUT
Experiment- 16
AIM- Construct a program to Implement Insertion Sort
Program:
#include <stdio.h>
int j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int main() {
int size = 5;
insertionSort(arr, size);
return 0;
OUTPUT
Experiment- 17
AIM- Construct a program to Implement Shell Sort
Program:
#include <stdio.h>
int j;
arr[j] = temp;
int main() {
int size = 5;
shellSort(arr, size);
return 0;
OUTPUT
Experiment- 18
AIM- Construct a program to Implement Counting Sort
Program:
#include <stdio.h>
#include <string.h>
int output[size];
max = arr[i];
memset(count, 0, sizeof(count));
count[arr[i]]++;
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
arr[i] = output[i];
int main() {
int size = 7;
countingSort(arr, size);
return 0;
OUTPUT
Experiment- 19
AIM- Create a single linked list and perform basic operations (insertion,
deletion, traversal)
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
temp->data = data;
temp->next = head;
return temp;
head = temp->next;
free(temp);
return head;
prev = temp;
temp = temp->next;
prev->next = temp->next;
free(temp);
return head;
head = head->next;
printf("\n");
int main() {
traverse(head);
traverse(head);
return 0;
OUTPUT
Experiment- 20
AIM- Create a double linked list and perform basic operations (insertion,
deletion, traversal).
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
temp->data = data;
temp->next = head;
temp->prev = NULL;
return temp;
free(temp);
return head;
while (head) {
head = head->next;
printf("\n");
int main() {
traverse(head);
traverse(head);
return 0;
OUTPUT
Experiment- 21
AIM- Create a circular linked list and perform basic operations (insertion,
deletion, traversal).
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
temp->data = data;
if (!head) {
head = temp;
temp->next = head;
return head;
curr->next = temp;
temp->next = head;
return head;
prev = temp;
temp = temp->next;
free(temp);
return NULL;
if (temp == head) {
prev = head;
head = temp->next;
prev->next = head;
free(temp);
prev->next = head;
free(temp);
} else {
prev->next = temp->next;
free(temp);
return head;
if (!head) return;
do {
temp = temp->next;
printf("\n");
int main() {
traverse(head);
traverse(head);
return 0;
OUTPUT
Experiment- 22
AIM- Create a circular Double linked list and perform basic operations
(insertion, deletion, traversal).
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
temp->data = data;
if (!head) {
temp->next = temp;
temp->prev = temp;
return temp;
temp->next = head;
head->prev = temp;
temp->prev = last;
last->next = temp;
return head;
prev = temp;
temp = temp->next;
free(temp);
return NULL;
if (temp == head) {
prev = head->prev;
head = temp->next;
prev->next = head;
head->prev = prev;
free(temp);
temp->prev->next = head;
head->prev = temp->prev;
free(temp);
} else {
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
free(temp);
return head;
if (!head) return;
do {
temp = temp->next;
printf("\n");
int main() {
traverse(head);
traverse(head);
return 0;
OUTPUT
Experiment- 23
AIM- Reverse a single linked list
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
return prev;
temp->data = data;
temp->next = head;
return temp;
while (head) {
head = head->next;
printf("\n");
int main() {
traverse(head);
head = reverse(head);
traverse(head);
return 0;
OUTPUT
Experiment- 24
AIM- Check if a linked list is palindrome.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
int result = 1;
prev = slow;
slow = slow->next;
fast = fast->next->next;
prev->next = NULL;
second_half = reverse(slow);
while (temp2) {
if (temp1->data != temp2->data) {
result = 0;
break;
temp1 = temp1->next;
temp2 = temp2->next;
second_half = reverse(second_half);
prev->next = second_half;
return result;
temp->data = data;
temp->next = head;
return temp;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
return prev;
while (head) {
head = head->next;
printf("\n");
int main() {
traverse(head);
return 0;
OUTPUT
Experiment- 25
AIM- Reverse a double linked list.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
while (curr) {
temp = curr->prev;
curr->prev = curr->next;
curr->next = temp;
curr = curr->prev;
temp->data = data;
temp->next = head;
temp->prev = NULL;
return temp;
while (head) {
head = head->next;
printf("\n");
int main() {
traverse(head);
head = reverse(head);
traverse(head);
return 0;
OUTPUT
Experiment- 26
AIM- Find the middle element of a single linked list
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
temp->data = data;
temp->next = head;
return temp;
slow = slow->next;
fast = fast->next->next;
return slow->data;
int main() {
return 0;
OUTPUT
Experiment- 27
AIM- Find the middle element of a double linked list.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
temp->data = data;
temp->next = head;
temp->prev = NULL;
return temp;
slow = slow->next;
fast = fast->next->next;
return slow->data;
int main() {
return 0;
OUTPUT
Experiment- 28
AIM- Merge two sorted single linked lists.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
temp->data = data;
temp->next = head;
return temp;
result = l1;
} else {
result = l2;
return result;
while (head) {
head = head->next;
printf("\n");
int main() {
l1 = insert(l1, 30);
l1 = insert(l1, 20);
l1 = insert(l1, 10);
l2 = insert(l2, 40);
l2 = insert(l2, 15);
l2 = insert(l2, 5);
traverse(mergedList);
return 0;
OUTPUT
Experiment- 29
AIM- Detect and remove a loop in a circular linked list.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
slow = head;
slow = slow->next;
fast = fast->next;
fast->next = NULL;
return;
temp->data = data;
temp->next = head;
return temp;
while (head) {
head = head->next;
printf("\n");
int main() {
detectAndRemoveLoop(head);
traverse(head);
return 0;
OUTPUT
Experiment- 30
AIM- Construct a code to add two polynomials using linked list
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
};
temp->coeff = coeff;
temp->pow = pow;
temp->next = head;
return temp;
p2 = p2->next;
p1 = p1->next;
} else {
p1 = p1->next;
p2 = p2->next;
return result;
while (head) {
head = head->next;
printf("\n");
int main() {
p1 = insert(p1, 5, 2);
p1 = insert(p1, 4, 1);
p2 = insert(p2, 3, 1);
p2 = insert(p2, 2, 0);
printPolynomial(result);
return 0;
OUTPUT
Experiment- 31
AIM- Construct a program to Implement stack using array
Program:
#include <stdio.h>
stack[++top] = val;
int pop() {
int peek() {
int isEmpty() {
int main() {
push(10);
push(20);
OUTPUT
Experiment- 32
AIM- Construct a program to Implement stack using a linked list
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = val;
newNode->next = top;
top = newNode;
int pop() {
top = top->next;
free(temp);
return poppedValue;
int peek() {
int isEmpty() {
int main() {
push(10);
push(20);
OUTPUT
Experiment- 33
AIM- Construct a code to Infix to postfix conversion using a stack
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char stack[MAX];
void push(char c) {
stack[++top] = c;
char pop() {
return stack[top--];
int precedence(char c) {
return 0;
int isOperator(char c) {
int i = 0, k = 0;
while (infix[i]) {
if (isalnum(infix[i]))
postfix[k++] = infix[i];
push(infix[i]);
postfix[k++] = pop();
} else if (isOperator(infix[i])) {
postfix[k++] = pop();
push(infix[i]);
i++;
postfix[k++] = pop();
postfix[k] = '\0';
int main() {
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("%s", postfix);
OUTPUT
Experiment- 34
AIM- Construct a code for Balanced parentheses checker using a stack
Program:
#include <stdio.h>
#include <stdlib.h>
char stack[MAX];
void push(char c) {
char pop() {
return stack[top--];
push(expr[i]);
return 0;
int main() {
char expr[MAX];
scanf("%s", expr);
if (isBalanced(expr)) printf("Balanced");
OUTPUT
Experiment- 35
AIM- Implement Reverse a string using a stack.
Program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char stack[MAX];
void push(char c) {
char pop() {
return stack[top--];
push(str[i]);
str[i] = pop();
int main() {
char str[MAX];
scanf("%s", str);
reverseString(str);
printf("%s", str);
OUTPUT
Experiment- 36
AIM- Implement Binary Search using Recursion.
Program:
#include <stdio.h>
int main() {
int key = 4;
OUTPUT
Experiment- 37
AIM- Construct a python program to print Fibonacci Series using Recursion.
Program:
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
fib_series = fibonacci(n - 1)
fib_series.append(fib_series[-1] + fib_series[-2])
return fib_series
print(fibonacci(n))
OUTPUT
Experiment- 38
AIM- Construct a code to implement Tower of Hanoi.
Program:
#include <stdio.h>
if (n == 1) {
return;
int main() {
int n = 3;
OUTPUT -
Experiment- 39
AIM- Construct a program to Implement queue using array
Program:
#include <stdio.h>
#define SIZE 5
int dequeue() {
else front++;
return val;
int main() {
enqueue(10);
enqueue(20);
OUTPUT
Experiment- 40
AIM- Construct a code for Implementing a circular queue.
Program:
#include <stdio.h>
#define SIZE 5
printf("Queue Full\n");
} else {
queue[rear] = val;
int dequeue() {
if (front == -1) {
printf("Queue Empty\n");
return -1;
} else {
if (front == rear) {
} else {
return val;
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
OUTPUT
Experiment- 41
AIM- Construct a program to Implement queue using stack
Program:
#include <stdio.h>
stack1[++top1] = value;
int pop1() {
if (top1 == -1)
return -1;
return stack1[top1--];
stack2[++top2] = value;
int pop2() {
if (top2 == -1) {
push2(pop1());
if (top2 == -1)
return -1;
return stack2[top2--];
int main() {
push1(10);
push1(20);
push1(30);
return 0;
OUTPUT
Experiment- 42
AIM- Construct a program to Implement priority queue
Program:
#include <stdio.h>
#define MAX 10
int i = size++;
i = (i - 1) / 2;
pq[i] = value;
int delete() {
if (size == 0)
return -1;
pq[0] = pq[--size];
left = 2 * i + 1;
right = 2 * i + 2;
max = i;
max = left;
max = right;
if (max == i)
break;
pq[i] = pq[max];
pq[max] = temp;
i = max;
return root;
int main() {
insert(10);
insert(20);
insert(15);
return 0;
OUTPUT
Experiment- 43
AIM- Construct a program to Implement double ended queue
Program:
#include <stdio.h>
#define MAX 10
if (front == 0)
printf("Queue Full\n");
else {
if (front == -1)
front = rear = 0;
else
dq[--front] = value;
if (rear == MAX - 1)
printf("Queue Full\n");
else {
if (rear == -1)
front = rear = 0;
else
dq[++rear] = value;
int deleteFront() {
if (front == -1)
return -1;
if (front == rear)
else
front++;
return val;
int deleteRear() {
if (rear == -1)
return -1;
if (front == rear)
else
rear--;
return val;
int main() {
insertRear(10);
insertRear(20);
insertFront(30);
return 0;
OUTPUT
Experiment- 44
AIM- Construct a program to Implement Merge Sort with recursion
Program:
#include <stdio.h>
int i = 0, j = 0, k = left;
arr[k++] = L[i++];
else
arr[k++] = R[j++];
arr[k++] = L[i++];
arr[k++] = R[j++];
int main() {
mergeSort(arr, 0, 5);
return 0;
OUTPUT
Experiment- 45
AIM- Construct a program to Implement Quick Sort with recursion
Program:
#include <stdio.h>
int i = low - 1;
i++;
arr[i] = arr[j];
arr[j] = temp;
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
quickSort(arr, pi + 1, high);
int main() {
quickSort(arr, 0, 5);
return 0;
OUTPUT
Experiment- 46
AIM- Construct a program to Implement Merge Sort using iteration
Program:
#include <stdio.h>
int i = 0, j = 0, k = left;
arr[k++] = L[i++];
else
arr[k++] = R[j++];
arr[k++] = L[i++];
arr[k++] = R[j++];
int main() {
mergeSort(arr, 6);
return 0;
OUTPUT
Experiment- 47
AIM- Construct a program to Implement Quick Sort using iteration
Program:
#include <stdio.h>
#include <stdlib.h>
*a = *b;
*b = temp;
int i = low - 1;
i++;
swap(&arr[i], &arr[j]);
return i + 1;
stack[++top] = low;
stack[++top] = high;
high = stack[top--];
low = stack[top--];
if (p - 1 > low) {
stack[++top] = low;
stack[++top] = p - 1;
if (p + 1 < high) {
stack[++top] = p + 1;
stack[++top] = high;
int main() {
quickSort(arr, 0, n - 1);
return 0;
OUTPUT
Experiment- 48
AIM- Construct a program to Implement fractional knapsack
Program:
#include <stdio.h>
struct Item {
int value;
int weight;
float ratio;
};
*a = *b;
*b = temp;
swap(&arr[i], &arr[j]);
sort(arr, n);
int currentWeight = 0;
currentWeight += arr[i].weight;
finalValue += arr[i].value;
} else {
break;
return finalValue;
int main() {
struct Item arr[] = {{60, 10, 6.0}, {100, 20, 5.0}, {120, 30, 4.0}};
int W = 50;
return 0;
OUTPUT
Experiment- 49
AIM- Construct a program to Implement Activity selection problem
Program:
#include <stdio.h>
struct Activity {
int start;
int finish;
};
arr[i] = arr[j];
arr[j] = temp;
sort(arr, n);
int i = 0;
i = j;
int main() {
struct Activity arr[] = {{1, 2}, {3, 4}, {0, 6}, {5, 7}, {8, 9}, {5, 9}};
activitySelection(arr, n);
return 0;
OUTPUT
Experiment- 50
AIM- Construct a program to Implement Job scheduling problem
Program:
#include <stdio.h>
struct Job {
int id;
int deadline;
int profit;
};
*a = *b;
*b = temp;
swap(&arr[i], &arr[j]);
sort(arr, n);
int result[n];
int slot[n];
slot[i] = -1;
if (slot[j] == -1) {
slot[j] = i;
result[i] = 1;
break;
if (result[i] == 1)
int main() {
struct Job arr[] = {{1, 4, 20}, {2, 1, 10}, {3, 1, 40}, {4, 1, 30}};
jobScheduling(arr, n);
return 0;
OUTPUT