Dsa Lab File
Dsa Lab File
PRACTICAL FILE
Code: BCP263
Output –
Q.2
Code –
- WAP to find maximum and minimum element of an array.
Output –
Q.3 - WAP to merge two arrays in one array.
Output –
Q.4
Code –
Output –
Q.5 - WAP to transpose a n*n matrices.
Code –
#include <stdio.h> #define N 3
void transpose(int A[N][N])
{ for (int i = 0; i < N; i++)
for (int j = i+1; j < N; j++) {
int temp = A[i][j]; A[i][j]
= A[j][i];
A[j][i] = temp;}
} void printMatrix(int A[N][N])
{ for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++){
printf("%d ", A[i]
[j]); }printf("\n");} } int
main() {
int A[N][N] = { {1, 2, 3},{4, 5, 6},{7, 8, 9} };
printf("Original matrix:\n"); printMatrix(A);
transpose(A); printf("Transposed matrix:\n");
printMatrix(A); return 0;
}
Output –
Q.6 –
Code –
WAP for addition and subtraction of two numbers in n*n matrices.
Output –
Output –
#include<stdio.h>
#define MAX 10 int
arr[MAX]; int
add(int top); int
pop(int top); void
display(int top); int
main(){ int i=1,
ch, top=0; while(i
== 1){
printf("Enter your choice 1 - add 2 - remove 3 - display 4 - exit\n");
scanf("%d",&ch); switch(ch){ case 1: top = add(top);
break; case 2: top = pop(top);
break; case
3: display(top);
break;
case 4: i=0;
break;
default: printf("Invalid input!\n");
}} return 0; } int add(int
top){ if(top == MAX)
{ printf("Stack Overflow!\
n");
return top;} int val;
printf("Enter value to add : \n");
scanf("%d",&val);
if(top == 0)
{ arr[top] = val;
} top++;
arr[top] = val;
return top; } int
pop(int top)
{ if(top == 0)
{
printf("Stack Underflow!\n");
return top;
} arr[top]
= 0; top--;
return top;
}
void display(int top)
{ printf("Stack is :\n");
for(int i=top; i>0; i--)
printf("%d\n",arr[i]);
}
Output –
–
Code
#include<stdio.h> #define MAX 10 int arr[MAX], front=-1, rear=-1;
void add(); void pop(); void display(); int main(){ int i=1, ch;
while(i == 1){ printf("Enter your choice 1 - add 2 - remove 3 -
display 4 - exit :\t"); scanf("%d",&ch); switch(ch)
{ case 1: add(); break; case 2: pop(); break;
case 3: display();
break;
case 4: i=0;
break;
default: printf("Invalid input!\n");
}} return 0;} void
add(){ if(rear == MAX)
{ printf("Overflow!\
n");
return; } int val;
printf("Enter value to add : \t");
scanf("%d",&val);
if(front == -1){ //Empty queue
front = 0; rear = front;
arr[rear] = val; return; }
rear++; arr[rear] = val;
} void pop(){ if(front == rear){
printf("Stack underflow!\n");
return;
} front++;
}
void display(){ printf("Your
queue is :\n"); for(int i=front;
i<rear+1; i++){ printf("%d\
t",arr[i]);
}
}
Output –
Q.10 – WAP to implement linked list (all insertion and deletion operations).
Code –
#include<stdio.h>
#include<stdlib.h> struct
node{
int data; struct node* next; }*start, *temp, *p; void add(); void
del(); void display(); int main(){ int i=1, ch; while(i==1)
{ printf("Enter your choice 1 - add 2 - delete 3 - display 4 - exit:\
t"); scanf("%d",&ch); switch(ch){ case 1: add();
break; case 2: del(); break; case 3: display();
break;
case 4: i=0;
break;
default: printf("Invalid input!\n");
} }
return 0;
}
void addAtBeg(int val){ // Insert at begining
temp = (struct node*)malloc(sizeof(struct node));
temp->data = val; temp->next = NULL;
if(start == NULL){ //first element
start = temp;
return;
} p = start;
start = temp;
temp->next = p;
}
void addAtEnd(int val){ // Insert at last
temp = (struct node*)malloc(sizeof(struct node));
temp->data = val; temp->next = NULL;
if(start == NULL){ addAtBeg(val);
return;
}
struct node* currNode = start; while(currNode-
>next != NULL){
currNode = currNode->next;
}
currNode->next = temp;
}
void addAtPos(int val){ // Insert at position
int pos;
printf("Enter value of node to add after :\t");
scanf("%d",&pos); temp = (struct
node*)malloc(sizeof(struct node)); temp->data =
val; temp->next = NULL; struct node*
currNode = start; while(currNode->next !=
NULL){ if(currNode->data == pos)
{ temp->next = currNode->next;
currNode->next = temp; printf("Inserted!");
return;}
currNode = currNode-
>next; }printf("Not found!");
}
void add(){ // Add function
int val,ch; printf("Enter your choice 1 - add at begining 2 - add at last 3 - add at
position: \t"); scanf("%d",&ch); printf("Enter value to add :\t");
scanf("%d",&val);
switch(ch){ case 1:
addAtBeg(val); break;
case 2: addAtEnd(val);
break;
case 3: addAtPos(val);
break;
default: printf("Invalid input!\n");
}
}
Output –
Q.11 - WAP to implement binary search.
Code –
#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x) {
while (l <= r) { int m = l + (r - l) / 2;
if (arr[m] == x) return m; if
(arr[m] < x) l = m + 1; else
r = m - 1;
} return -1; } int main(void) {
int arr[] = {2, 3, 4, 10, 40}; int n
= sizeof(arr) / sizeof(arr[0]); int x
= 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present in array\n")
: printf("Element is present at index %d\n", result); return 0;
}
Output –
–
–
Q.12 WAP to implement Bubble sort, selection sort, insertion sort.
Code
#include <stdio.h> void
swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[], int n)
{ for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
printf("Bubble Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}
void selectionSort(int arr[], int n) {
int i, j, min_idx; 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;
swap(&arr[min_idx], &arr[i]);
}
Output –
Output –
Code
–
–
#include <stdio.h>
#include <stdlib.h>
struct node { int
data; struct node*
left; struct node*
right;
};
struct node* newNode(int data) {
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data; node->left = NULL; node->right =
NULL; return(node);
}
void printPreorder(struct node* node) {
if (node == NULL)
return; printf("%d ",
node->data);
printPreorder(node->left);
printPreorder(node->right);
}
void printInorder(struct node* node) {
if (node == NULL)
return; printInorder(node-
>left); printf("%d ", node->data);
printInorder(node->right);
}
void printPostorder(struct node* node) {
if (node == NULL)
return; printPostorder(node-
>left); printPostorder(node->right);
printf("%d ", node->data);
} int main()
{
struct node *root = newNode(1); root->left =
newNode(2); root->right = newNode(3); root-
>left->left = newNode(4); root->left->right =
newNode(5); printf("\nPreorder traversal of
binary tree is \n"); printPreorder(root);
printf("\nInorder traversal of binary tree is \n");
printInorder(root);
printf("\nPostorder traversal of binary tree is \n"); printPostorder(root);
return 0;
}
Output –
Code
#include <stdio.h>
#include <stdlib.h>
struct node { int
key;
struct node *left, *right;
};
–
–
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item; temp->left = temp->right = NULL;
return temp;
} struct node* insert(struct node* node, int key)
{ if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key) node->right
= insert(node->right, key); return node;
}
struct node * minValueNode(struct node* node) {
struct node* current = node; while (current
&& current->left != NULL)
current = current->left;
return current;}
struct node* deleteNode(struct node* root, int key) {
if (root == NULL) return root; if (key < root-
>key) root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
if (root->left == NULL)
{ struct node *temp = root-
>right;
free(root);
return temp;
}
else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
struct node* temp = minValueNode(root->right); root-
>key = temp->key;
root->right = deleteNode(root->right, temp->key);
} return
root; }
void
inorder(str
uct node
*root)
{ if
(root !=
NULL) {
inorder(ro
ot->left);
printf("%d
\n", root-
>key);
inorder(ro
ot->right);
}
} int main() { struct node
*root = NULL; root =
insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
printf("Inorder traversal of the given tree \n");
inorder(root); printf("\nDelete 20\n"); root =
deleteNode(root, 20); printf("Inorder traversal of
the modified tree \n"); inorder(root); return 0;
}
Output –
Code
#include <stdio.h>
#define INFINITY 9999 #define
MAX 10
void Dijkstra(int Graph[MAX][MAX], int n, int start);
void Dijkstra(int Graph[MAX][MAX], int n, int start) {
int cost[MAX][MAX], distance[MAX], pred[MAX];
int visited[MAX], count, mindistance, nextnode, i, j;
for (i = 0; i < n; i++) for (j = 0; j < n; j++) if
(Graph[i][j] == 0)
cost[i][j] = INFINITY;
else cost[i][j] =
Graph[i][j]; for (i = 0; i <
n; i++) { distance[i] =
cost[start][i]; pred[i] =
start; visited[i] = 0;
} distance[start] = 0;
visited[start] = 1;
count = 1; while
(count < n - 1) {
mindistance = INFINITY; for (i = 0; i < n;
i++) if (distance[i] < mindistance && !
visited[i]) { mindistance = distance[i];
nextnode = i;} visited[nextnode] = 1; for (i
= 0; i < n; i++)
if (!visited[i])
if (mindistance + cost[nextnode][i] < distance[i]) {
distance[i] = mindistance + cost[nextnode][i];
pred[i] = nextnode;
}
count++;
} for (i = 0; i < n;
i++) if (i != start) {
printf("\nDistance from source to %d: %d", i, distance[i]);
}
} int main()
{
int Graph[MAX][MAX], i, j, n, u;
n = 7;
Graph[0][0] = 0; Graph[0][1] = 0; Graph[0][2] = 1; Graph[0][3] = 2; Graph[0][4] = 0;
Graph[0][5] = 0; Graph[0][6] = 0; Graph[1][0] = 0; Graph[1][1] = 0; Graph[1][2] = 2;
Graph[1][3] = 0; Graph[1][4] = 0; Graph[1][5] = 3; Graph[1][6] = 0; Graph[2][0] = 1;
Graph[2][1] = 2; Graph[2][2] = 0; Graph[2][3] = 1; Graph[2][4] = 3; Graph[2][5] = 0;
Graph[2][6] = 0; Graph[3][0] = 2; Graph[3][1] = 0; Graph[3][2] = 1; Graph[3][3] = 0;
Graph[3][4] = 0; Graph[3][5] = 0; Graph[3][6] = 1; Graph[4][0] = 0; Graph[4][1] = 0;
Graph[4][2] = 3; Graph[4][3] = 0; Graph[4][4] = 0; Graph[4][5] = 2; Graph[4][6] = 0;
Graph[5][0] = 0; Graph[5][1] = 3; Graph[5][2] = 0; Graph[5][3] = 0; Graph[5][4] = 2;
Graph[5][5] = 0; Graph[5][6] = 1; Graph[6][0] = 0; Graph[6][1] = 0; Graph[6][2] = 0;
Graph[6][3] = 1; Graph[6][4] = 0; Graph[6][5] = 1; Graph[6][6] = 0;
Dijkstra(Graph, n, u);
return 0;
}
Output –
Q.17. WAP to implement Prim’s algorithm.
Code – #include<stdio.h>
#include<stdbool.h>
#define INF 9999999
#define V 5 int G[V]
[V] = { {0, 9, 75, 0,
0},
{9, 0, 95, 19, 42},
{75, 95, 0, 51, 66},
{0, 19, 51, 0, 31},
{0, 42, 66, 31, 0}};
int main() {
int no_edge; // number of edge
int selected[V];
memset(selected, false, sizeof(selected));
no_edge = 0; selected[0] = true; int
x; // row number int y; // col number
printf("Edge : Weight\n"); while
(no_edge < V - 1) { int min = INF; x
= 0; y = 0;
for (int i = 0; i < V; i++) {
if (selected[i]) { for (int j
= 0; j < V; j++) {
if (!selected[j] && G[i][j]) { // not in selected and there is an edge
if (min > G[i][j]) { min = G[i][j]; x = i;
y = j;}}}}}
printf("%d - %d : %d\n", x, y, G[x][y]);
selected[y] = true;
no_edge++;
}
return
0; }
Output –
Code –
#include <stdio.h> #define
MAX 30
typedef struct edge {
int u, v, w; } edge;
typedef struct edge_list
{
edge data[MAX];
int n; }
edge_list;
edge_list elist;
int Graph[MAX][MAX], n;
edge_list spanlist; void kruskalAlgo(); int
find(int belongs[], int vertexno); void
applyUnion(int belongs[], int c1, int c2);
void sort(); void print(); void
kruskalAlgo() { int
belongs[MAX], i, j, cno1, cno2;
elist.n = 0; for (i = 1; i < n; i++)
for (j = 0; j < i; j++) { if (Graph[i]
[j] != 0) { elist.data[elist.n].u = i;
elist.data[elist.n].v = j;
elist.data[elist.n].w = Graph[i][j];
elist.n++;
} } sort(); for (i = 0; i < n; i++)
belongs[i] = i; spanlist.n = 0; for (i = 0;
i < elist.n; i++) { cno1 = find(belongs,
elist.data[i].u); cno2 = find(belongs,
elist.data[i].v); if (cno1 != cno2)
{ spanlist.data[spanlist.n] =
elist.data[i]; spanlist.n = spanlist.n +
1; applyUnion(belongs, cno1,
cno2);}}} int find(int belongs[], int
vertexno) { return (belongs[vertexno]);
}
void applyUnion(int belongs[], int c1, int c2) {
int i; for (i = 0; i <
n; i++) if
(belongs[i] == c2)
belongs[i] = c1;
} void sort()
{
int i, j;
edge temp;
for (i = 1; i < elist.n; i++) for (j = 0; j <
elist.n - 1; j++) if (elist.data[j].w >
elist.data[j + 1].w) { temp =
elist.data[j]; elist.data[j] = elist.data[j
+ 1];
elist.data[j + 1] = temp;
}
} void print() {
int i, cost = 0;
for (i = 0; i < spanlist.n; i++) {
printf("\n%d - %d : %d", spanlist.data[i].u, spanlist.data[i].v, spanlist.data[i].w);
cost = cost + spanlist.data[i].w;
}
printf("\nSpanning tree cost: %d", cost);
} int main() { int
i, j, total_cost;
n = 6;
Graph[0][0] = 0; Graph[0][1] = 4; Graph[0][2] = 4; Graph[0][3] = 0; Graph[0][4] = 0;
Graph[0][5] = 0; Graph[0][6] = 0; Graph[1][0] = 4; Graph[1][1] = 0; Graph[1][2] = 2;
Graph[1][3] = 0; Graph[1][4] = 0; Graph[1][5] = 0; Graph[1][6] = 0; Graph[2][0] = 4;
Graph[2][1] = 2; Graph[2][2] = 0; Graph[2][3] = 3; Graph[2][4] = 4; Graph[2][5] = 0;
Graph[2][6] = 0; Graph[3][0] = 0; Graph[3][1] = 0; Graph[3][2] = 3; Graph[3][3] = 0;
Graph[3][4] = 3; Graph[3][5] = 0; Graph[3][6] = 0; Graph[4][0] = 0; Graph[4][1] = 0;
Graph[4][2] = 4; Graph[4][3] = 3; Graph[4][4] = 0; Graph[4][5] = 0; Graph[4][6] = 0;
Graph[5][0] = 0; Graph[5][1] = 0; Graph[5][2] = 2; Graph[5][3] = 0; Graph[5][4] = 3;
Graph[5][5] = 0; Graph[5][6] = 0;
kruskalAlgo(); print();
}
Output –