DSA FILE -
DSA FILE -
Lab file of
Submitted by
VIHAAN SOOD
Enrollment No. 00416449724
Submitted to
In
Bachelor Of Technology
In
ASSIGNMENT 1:
#include <stdio.h>
int main()
{
int Array[]={0,2,4,6,8};
for(int i=0;i<5;i++)
{
printf("%d ",Array[i]);
}
printf("\n");
return 0;
OUTPUT:
Aim: Write a program to perform the following
operations on two 2D arrays: Addition of the two
arrays. Subtraction of the second array from the first
array. Multiplication of the two arrays (element-wise
and matrix multiplication)
ASSIGNMENT 2:
#include <stdio.h>
#define SIZE 3
int main() {
int a1[SIZE][SIZE] = {{2, 4, 6}, {8, 10, 12}, {14, 16, 18}};
int a2[SIZE][SIZE] = {{1, 3, 5}, {7, 9, 11}, {13, 15, 17}};
int vihaan_sood[SIZE][SIZE];
printf("Addition:\n");
add_matrices(a1, a2, vihaan_sood);
print_matrix(vihaan_sood);
printf("Subtraction:\n");
subtract_matrices(a1, a2, vihaan_sood);
print_matrix(vihaan_sood);
printf("Element-wise Multiplication:\n");
elementwise_multiply(a1, a2, vihaan_sood);
print_matrix(vihaan_sood);
printf("Matrix Multiplication:\n");
matrix_multiply(a1, a2, vihaan_sood);
print_matrix(vihaan_sood);
return 0;
}
OUTPUT:
Aim: Implement the following stack operations using
an array: 1) PUSH 2) POP 3) PEEK
ASSIGNMENT 3:
#include <stdio.h>
int stack[MAX];
if (top == MAX- 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = value;
void pop() {
if (top ==-1) {
printf("Stack Underflow\n");
} else {
void peek() {
if (top ==-1) {
printf("Stack is empty\n");
} else {
}
void display() {
if (top ==-1) {
printf("Stack is empty\n");
} else {
printf("\n");
int main() {
push(10);
push(20);
push(30);
peek();
pop();
peek();
display();
return 0;
OUTPUT:
Aim: Write a C program that: i)Accepts an m × n matrix
from the user, Computes and displays its transpose.
ii) Check if the given square matrix is symmetric (i.e.,
A[i][j] == A[j][i]).
ASSIGNMENT 4:
#include <stdio.h>
int main() {
int m, n;
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
printf("Original Matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Aim: Write a C program that accepts an n × n square
matrix from the user. Computes the sum of the main
diagonal elements (where i == j), & Displays the
computed sum.
ASSIGNMENT 5:
#include <stdio.h>
int main() {
int n;
int vihaan_sood[n][n];
int sum = 0;
for (int i = 0; i < n; i++) {
sum += vihaan_sood[i][i];
}
ASSIGNMENT 6:
A.
#include <stdio.h>
int queue[MAX];
if (rear == MAX- 1) {
printf("Queue Overflow\n");
} else {
queue[++rear] = value;
void dequeue() {
printf("Queue Underflow\n");
} else {
void display() {
} else {
printf("\n");
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
OUTPUT:
B.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
} else {
rear->next = newNode;
rear = newNode;
void dequeue() {
if (front == NULL) {
printf("Queue Underflow\n");
} else {
front = front->next;
free(temp);
}
void display() {
if (front == NULL) {
printf("Queue is empty\n");
} else {
temp = temp->next;
printf("\n");
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
OUTPUT:
Aim: Write a C program to implement a Circular Queue
using an array with the following operations: Enqueue,
Dequeue and Display
ASSIGNMENT 7:
#include <stdio.h>
#define MAX 5
int queue[MAX];
printf("Queue Overflow\n");
front = rear = 0;
queue[rear] = value;
} else {
queue[rear] = value;
void dequeue() {
if (front ==-1) {
printf("Queue Underflow\n");
} else {
void display() {
if (front ==-1) {
printf("Queue is empty\n");
} else {
int i = front;
while (1) {
if (i == rear) break;
i = (i + 1) % MAX;
printf("\n");
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
display();
dequeue();
dequeue();
display();
enqueue(60);
enqueue(70);
display();
return 0;
}
OUTPUT:
Aim: WAP in C using stacks to:
a. Convert the infix expression (A + B) * (C - D) to
postfix and prefix.
b. Convert the postfix expression AB+CD-* to infix and
prefix.
c. Convert the prefix expression *+AB-CD to infix and
postfix.
ASSIGNMENT 8:
a.)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
typedef struct {
int top;
char items[MAX];
} Stack;
void init(Stack* s) {
s->top = -1;
int isEmpty(Stack* s) {
}
char peek(Stack* s) {
return s->items[s->top];
if (s->top == MAX - 1) {
printf("Stack overflow\n");
return;
s->items[++(s->top)] = value;
char pop(Stack* s) {
if (isEmpty(s)) {
printf("Stack underflow\n");
return '\0';
return s->items[(s->top)--];
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
return 0;
}
int isOperator(char c) {
Stack s;
init(&s);
int k = 0;
if (isalnum(infix[i])) {
postfix[k++] = infix[i];
push(&s, infix[i]);
postfix[k++] = pop(&s);
pop(&s);
} else if (isOperator(infix[i])) {
postfix[k++] = pop(&s);
push(&s, infix[i]);
while (!isEmpty(&s)) {
postfix[k++] = pop(&s);
}
postfix[k] = '\0';
Stack s;
init(&s);
if (!reversed) {
return;
if (reversed[i] == '(') {
reversed[i] = ')';
reversed[i] = '(';
reversed[len] = '\0';
if (!tempPostfix) {
free(reversed);
return;
}
infixToPostfix(reversed, tempPostfix);
free(reversed);
free(tempPostfix);
int main() {
infixToPostfix(infix, postfix);
infixToPrefix(infix, prefix);
printf("\nFinal Output:\n");
return 0;
OUTPUT:
b.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int top;
char expressions[MAX][MAX];
} Stack;
void init(Stack* s) {
s->top = -1;
int isEmpty(Stack* s) {
if (s->top == MAX - 1) {
printf("Stack overflow!\n");
return;
strcpy(s->expressions[++(s->top)], str);
char* pop(Stack* s) {
if (isEmpty(s)) {
printf("Stack underflow!\n");
return NULL;
}
return s->expressions[(s->top)--];
int isOperator(char c) {
Stack s;
init(&s);
if (isOperator(postfix[i])) {
strcpy(rightOperand, pop(&s));
strcpy(leftOperand, pop(&s));
push(&s, combined);
} else {
push(&s, operand);
strcpy(infix, pop(&s));
Stack s;
init(&s);
strcpy(rightOperand, pop(&s));
strcpy(leftOperand, pop(&s));
push(&s, combined);
} else {
push(&s, operand);
strcpy(prefix, pop(&s));
int main() {
convertPostfixToInfix(postfix, infix);
convertPostfixToPrefix(postfix, prefix);
return 0;
OUTPUT:
c.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int top;
char expressions[MAX][MAX];
} Stack;
void init(Stack* s) {
s->top = -1;
int isEmpty(Stack* s) {
if (s->top == MAX - 1) {
printf("Stack overflow!\n");
return;
strcpy(s->expressions[++(s->top)], str);
char* pop(Stack* s) {
if (isEmpty(s)) {
printf("Stack underflow!\n");
return NULL;
}
return s->expressions[(s->top)--];
int isOperator(char c) {
Stack s;
init(&s);
if (isOperator(prefix[i])) {
strcpy(leftOperand, pop(&s));
strcpy(rightOperand, pop(&s));
push(&s, combined);
} else {
push(&s, operand);
strcpy(infix, pop(&s));
Stack s;
init(&s);
if (isOperator(prefix[i])) {
strcpy(leftOperand, pop(&s));
strcpy(rightOperand, pop(&s));
push(&s, combined);
} else {
push(&s, operand);
strcpy(postfix, pop(&s));
int main() {
convertPrefixToInfix(prefix, infix);
convertPrefixToPostfix(prefix, postfix);
return 0;
OUTPUT:
Aim: WAP in C to implement a Binary Search Tree (BST)
with functions for insertion, searching, and in-order
traversal
ASSIGNMENT 9:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
return newNode;
return root;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
int main() {
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);
inorder(root);
printf("\n");
return 0;
OUTPUT:
Aim: Write a C program to construct a B-Tree of Order
3 by inserting the given keys sequentially. The B-Tree
should follow the properties of an m-order B-Tree,
where each node can have at most m-1 keys and m
children. Given Keys (in order of insertion):12, 10, 15,
4, 1, 17, 3, 13
ASSIGNMENT 10:
#include <stdio.h>
#include <stdlib.h>
#define MIN_DEGREE 2
int num_keys;
int leaf;
} BTreeNode;
BTreeNode* root;
} BTree;
node->num_keys = 0;
node->leaf = leaf;
node->children[i] = NULL;
}
return node;
BTreeNode* newChild;
int middleKey;
} SplitResult;
BTreeNode* y = parent->children[index];
BTreeNode* z = createNode(y->leaf);
SplitResult result;
if (!y->leaf) {
y->num_keys = MIN_DEGREE - 1;
z->num_keys = MIN_DEGREE - 1;
result.newChild = z;
return result;
}
void insertNonFull(BTreeNode* node, int key) {
int i = node->num_keys - 1;
if (node->leaf) {
node->keys[i + 1] = node->keys[i];
i--;
node->keys[i + 1] = key;
node->num_keys++;
} else {
i--;
i++;
node->children[j + 1] = node->children[j];
node->keys[i] = split.middleKey;
node->children[i + 1] = split.newChild;
node->num_keys++;
i++;
insertNonFull(node->children[i], key);
if (tree->root == NULL) {
tree->root = createNode(1);
tree->root->keys[0] = key;
tree->root->num_keys = 1;
return;
newRoot->children[0] = tree->root;
newRoot->keys[0] = split.middleKey;
newRoot->children[1] = split.newChild;
newRoot->num_keys = 1;
tree->root = newRoot;
insertNonFull(tree->root, key);
int i;
inorderTraversal(root->children[i]);
inorderTraversal(root->children[i]);
printBTreeHelper(root->children[root->num_keys], space);
printf("\n");
printf(" ");
printf("\n");
printBTreeHelper(root->children[i], space);
printBTreeHelper(root, 0);
printf("\n");
int main() {
insert(&tree, keys[i]);
}
printf("B-Tree of Order 3 after insertion (Level-wise):\n");
printBTree(tree.root);
inorderTraversal(tree.root);
return 0;
OUTPUT:
Aim: Write a C program to search for `23` in the array
`{34, 7, 23, 32, 5, 62}` using Linear Search on the
unsorted array, then sort it and use Binary Search.
Display the index if found.
ASSIGNMENT 11:
#include <stdio.h>
if(arr[i] == key)
return i;
return -1;
arr[j] = arr[j+1];
arr[j+1] = temp;
if(arr[mid] == key)
return mid;
low = mid + 1;
else
high = mid - 1;
return -1;
int main() {
if(linearIndex != -1)
else
bubbleSort(arr, size);
if(binaryIndex != -1)
else
return 0;
OUTPUT:
Aim: Write a C program to implement Bubble Sort and
Insertion Sort on a given array. Display the array after
applying each sorting technique: int arr[] = {45, 12, 89,
33, 67, 23, 10, 5, 76, 38};
ASSIGNMENT 12:
#include <stdio.h>
arr[j] = arr[j+1];
arr[j+1] = temp;
arr[j+1] = arr[j];
j--;
arr[j+1] = key;
printf("\n");
int main() {
int arr1[] = {45, 12, 89, 33, 67, 23, 10, 5, 76, 38};
int arr2[] = {45, 12, 89, 33, 67, 23, 10, 5, 76, 38};
bubbleSort(arr1, n);
display(arr1, n);
insertionSort(arr2, n);
display(arr2, n);
return 0;
}include <stdio.h>
if(arr[i] == key)
return i;
return -1;
arr[j] = arr[j+1];
arr[j+1] = temp;
if(arr[mid] == key)
return mid;
low = mid + 1;
else
high = mid - 1;
return -1;
int main() {
}
int linearIndex = linearSearch(arr, size, key);
if(linearIndex != -1)
else
bubbleSort(arr, size);
if(binaryIndex != -1)
else
return 0;
OUTPUT:
Aim: WAP in C to implement quick sort program using
arrays
ASSIGNMENT 13:
#include <stdio.h>
*a = *b;
*b = temp;
int i = low - 1;
i++;
swap(&arr[i], &arr[j]);
swap(&arr[i+1], &arr[high]);
return i + 1;
quickSort(arr, p + 1, high);
}
void display(int arr[], int n) {
printf("\n");
int main() {
int arr[] = {45, 12, 89, 33, 67, 23, 10, 5, 76, 38};
quickSort(arr, 0, n - 1);
display(arr, n);
return 0;
OUTPUT:
Aim: WAP to implement merge sort program using
arrays
ASSIGNMENT 14:
#include <stdio.h>
int n1 = m - l + 1, n2 = r - m;
int i = 0, j = 0, k = l;
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
void display(int arr[], int n) {
printf("\n");
int main() {
int arr[] = {45, 12, 89, 33, 67, 23, 10, 5, 76, 38};
mergeSort(arr, 0, n - 1);
display(arr, n);
return 0;
OUTPUT:
Aim: WAP in C to implement BFS and DFS on a graph
represented using an adjacency matrix. The program
should take the number of vertices and edges as input,
allow the user to enter the edge connections, and
perform both traversals starting from a user-defined
starting vertex. Finally, display the order of traversal for
both algorithms.
ASSIGNMENT 15:
#include <stdio.h>
front = rear = 0;
queue[rear++] = start;
visited[start] = 1;
int u = queue[front++];
queue[rear++] = v;
visited[v] = 1;
}
}
visited[u] = 1;
dfsUtil(v, n);
dfsUtil(start, n);
int main() {
int n, e, u, v, start;
scanf("%d", &n);
scanf("%d", &e);
adj[u][v] = adj[v][u] = 1;
scanf("%d", &start);
printf("BFS traversal: ");
bfs(start, n);
printf("\n");
dfs(start, n);
printf("\n");
return 0;
OUTPUT:
Aim: WAP To implement the Selection Sort algorithm
in C for sorting an array of integers in ascending order.
ASSIGNMENT 16:
#include <stdio.h>
int min = i;
min = j;
if (min != i) {
arr[i] = arr[min];
arr[min] = temp;
printf("\n");
int main() {
int arr[] = {45, 12, 89, 33, 67, 23, 10, 5, 76, 38};
display(arr, n);
return 0;
OUTPUT:
Aim: WAP To implement Kruskal's and Prim's
algorithms in C for finding the Minimum Spanning Tree
(MST) of a connected, weighted, undirected graph.
ASSIGNMENT 17:
#include <stdio.h>
#include <stdlib.h>
int parent[MAX];
int find(int i) {
while (parent[i])
i = parent[i];
return i;
int a = find(i);
int b = find(j);
if (a != b)
parent[b] = a;
min = INF;
for (i = 0; i < n; i++)
min = cost[i][j];
a = u = i;
b = v = j;
u = find(u);
v = find(v);
if (u != v) {
unionSet(u, v);
mincost += min;
visited[0] = 1;
if (visited[i]) {
min = cost[i][j];
a = i;
b = j;
}
}
if (a != -1 && b != -1) {
visited[b] = 1;
mincost += min;
int main() {
int cost[MAX][MAX], n, e, u, v, w;
scanf("%d", &n);
cost[i][j] = INF;
scanf("%d", &e);
cost[u][v] = cost[v][u] = w;
if (cost[i][j] != INF)
cost[i][j] = cost[j][i];
prim(cost, n);gf
return 0;
OUTPUT: