Dsa Lab File
Dsa Lab File
8 of 2013)
LABORATORY RECORD
Programme : UG
Specialization : Data Science & Artificial Intelligence
Semester & Year : III & II
Name of Student : Kumud Hasija
Registration. No : 11022210056
Section :D
Course Code : 21CS2113
Course Title : Data Structure Using C Laboratory
Name of Teacher : Dr. M. MOHAN
BONAFIDE CERTIFICATE
Reg. No : 11022210056
Certified that this is the bonafide record of practical done as a part of III
Semester during the academic year 2023 - 2024.
I
21CS2113 - Data Structure Using C Laboratory
LIST OF EXPERIMENTS
II
21CS2113 - Data Structure Using C Laboratory
Exp No. - 1
QUICK SORT
AIM
ALGORITHM
III
21CS2113 - Data Structure Using C Laboratory
SOURCE CODE
#include<stdio.h>
int main(){
int s ,A[50];
printf("Enter size of array :"); scanf("%d",&s);
input_array(A,s);
printf(“Array before sorting :”); print_array(A,s);
quick_sort(A,0,s);
printf(“Array after sorting :”) ; print_array(A,s); }
IV
21CS2113 - Data Structure Using C Laboratory
OUTPUT
RESULT
V
21CS2113 - Data Structure Using C Laboratory
Exp No. - 2
BINARY SEARCH
AIM
ALGORITHM
VI
21CS2113 - Data Structure Using C Laboratory
SOURCE CODE
#include<stdio.h>
int main(){
int s , a, A[50];
printf("Enter size of array :"); scanf("%d",&s);
input_array(A,s);
print_array(A,s);
printf ("Enter number to be searched :");
scanf ("%d", &a);
binary_search (B, 0, s-1, a);
}
VII
21CS2113 - Data Structure Using C Laboratory
OUTPUT
RESULT
VIII
21CS2113 - Data Structure Using C Laboratory
Exp No. - 3
INFIX – POSTFIX CONVERSION
AIM
ALGORITHM
IX
21CS2113 - Data Structure Using C Laboratory
SOURCE CODE
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
struct stack {
int top;
int size ;
char *arr; };
X
21CS2113 - Data Structure Using C Laboratory
int main() {
char s[50];
printf("Enter the expression :");
scanf("%s", s);
char *postfix = infixtopostfix(s);
printf(“Postfix expression : %s\n”,postfix);
}
XI
21CS2113 - Data Structure Using C Laboratory
OUTPUT
RESULT
XII
21CS2113 - Data Structure Using C Laboratory
Exp No. - 4
INFIX - PREFIX CONVERSION
AIM
ALGORITHM
XIII
21CS2113 - Data Structure Using C Laboratory
SOURCE CODE
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
struct stack {
int top;
int size ;
char *arr; };
int main() {
char s[50];
printf("Enter the expression :");
scanf("%s", s);
char *prefix = infixtoprefix(s);
printf(“Prefix expression : %s\n”,prefix);
}
XV
21CS2113 - Data Structure Using C Laboratory
OUTPUT
RESULT
XVI
21CS2113 - Data Structure Using C Laboratory
Exp No. - 5
EVALUATION OF POSTFIX AND PREFIX EXPRESSION
AIM
ALGORITHM
XVII
21CS2113 - Data Structure Using C Laboratory
SOURCE CODE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stack {
int top;
int size;
int* arr;
};
int main() {
char postfix_exp[50], prefix_exp[50];
printf("Enter the postfix expression: ");
scanf("%s", postfix_exp);
printf("Result of postfix expression result: %d\n", evaluatepostfix(postfix_exp));
printf("Enter the prefix expression: ");
scanf("%s", prefix_exp);
printf("Result of prefix expression result: %d\n", evaluateprefix(prefix_exp));}
XIX
21CS2113 - Data Structure Using C Laboratory
OUTPUT
RESULT
XX
21CS2113 - Data Structure Using C Laboratory
Exp No. - 6
AIM
ALGORITHM
XXI
21CS2113 - Data Structure Using C Laboratory
SOURCE CODE
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
struct Queue {
int front1, rear1;
int front2, rear2;
int arr[MAX_SIZE];
};
int main() {
struct Queue q; initialize(&q);
int choice, data, queueNumber;
do {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n"); printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to enqueue: "); scanf("%d", &data);
printf("Enter queue number (1 or 2): "); scanf("%d", &queueNumber);
enqueue(&q, data,queueNumber); break;
case 2:
printf("Enter queue number (1 or 2): "); scanf("%d", &queueNumber);
dequeue(&q, queueNumber); break;
case 3:
printf("Enter queue number (1 or 2): "); scanf("%d", &queueNumber);
display(&q, queueNumber);
break;
case 4:
printf("Exiting the program.\n"); break;
default:
printf("Invalid choice. Please try again.\n");}
} while (choice != 4);
return 0;}
XXIII
21CS2113 - Data Structure Using C Laboratory
OUTPUT
RESULT
XXIV
21CS2113 - Data Structure Using C Laboratory
Exp No. – 7
AIM
ALGORITHM
XXV
21CS2113 - Data Structure Using C Laboratory
SOURCE CODE
#include <stdio.h>
int main() {
int size ,data,choice;
int tree[100]={0};
printf("Enter the size of the array: ");
scanf("%d", &size);
do {
printf("Enter data to insert: ");
scanf("%d", &data);
insert(tree,data,0);
printf("Do you want to insert more? (1/0): ");
scanf("%d", &choice);
} while (choice != 0);
printf("\nInorder traversal:\n");
print(tree ,size);
printf("\n");
return 0;
}
XXVI
21CS2113 - Data Structure Using C Laboratory
OUTPUT
RESULT
XXVII
21CS2113 - Data Structure Using C Laboratory
Exp No. - 8
AIM
ALGORITHM
XXVIII
21CS2113 - Data Structure Using C Laboratory
SOURCE CODE
#include <stdio.h>
#include <stdlib.h>
struct node
{ int data;
struct node *left;
struct node *right;
int height;
};
return x;
}
XXIX
21CS2113 - Data Structure Using C Laboratory
struct node* leftRotate(struct node* x){
struct node* y = x->right;
struct node* T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(getHeight(x->right), getHeight(x->left)) + 1;
y->height = max(getHeight(y->right), getHeight(y->left)) + 1;
return y;
}
int bf = BalanceFactor(n);
XXX
21CS2113 - Data Structure Using C Laboratory
struct node * inorderpredessor(struct node * root){
root = root->left;
while(root->right != NULL){
root = root->right;}
return root;
}
else{
temp = inorderpredessor(root);
root->data = temp->data;
root->left =delete(root->left,temp->data);
}
int bf = BalanceFactor(root);
return root;
}
int main(){
struct node * root = NULL;
int choice, key;
do {
XXXII
21CS2113 - Data Structure Using C Laboratory
printf("\nAVL Tree Operations\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display (Inorder)\n");
printf("4. Display (Postorder)\n");
printf("5. Display (Preorder)\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the key to insert: ");
scanf("%d", &key);
root = insert(root, key);
break;
case 2:
printf("Enter the key to delete: ");
scanf("%d", &key);
root = delete(root, key);
break;
case 3:
printf("AVL (Inorder): ");
inorder(root);
printf("\n");
break;
case 4:
printf("AVL (Postorder): ");
postorder(root);
printf("\n");
break;
case 5:
printf("AVL (Preorder): ");
preorder(root);
printf("\n");
break;
case 6:
printf("Exit\n");
break;
default:
printf("Invalid choice!\n");
}
XXXIII
21CS2113 - Data Structure Using C Laboratory
OUTPUT
RESULT
XXXIV
21CS2113 - Data Structure Using C Laboratory
Exp No. – 9
AIM
Program to demonstrate insertion , deletion , traversal in binary search tree using linked list .
ALGORITHM
XXXV
21CS2113 - Data Structure Using C Laboratory
SOURCE CODE
#include <stdio.h>
#include <stdlib.h>
struct node
{ int data;
struct node *left;
struct node *right;
};
else{
temp = inorderpredessor(root);
root->data = temp->data;
root->left =delete(root->left,temp->data);
} return root;
}
int main(){
struct node * root = NULL;
int choice, key;
do {
printf("\nBST search Tree Operations\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display (Inorder)\n");
printf("4. Display (Postorder)\n");
printf("5. Display (Preorder)\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the key to insert: ");
scanf("%d", &key);
root = insert(root, key);
break;
case 2:
printf("Enter the key to delete: ");
scanf("%d", &key);
root = delete(root, key);
break;
case 3:
printf("BST (Inorder): ");
inorder(root);
printf("\n");
break;
case 4:
printf("BST (Postorder): ");
postorder(root);
printf("\n");
break;
case 5:
printf("BST (Preorder): ");
XXXVIII
21CS2113 - Data Structure Using C Laboratory
preorder(root);
printf("\n");
break;
case 6:
printf("Exit\n");
break;
default:
printf("Invalid choice!\n");
}
OUTPUT
XXXIX
21CS2113 - Data Structure Using C Laboratory
RESULT
XL
21CS2113 - Data Structure Using C Laboratory
Exp No. - 10
AIM
ALGORITHM
XLI
21CS2113 - Data Structure Using C Laboratory
SOURCE CODE
#include <stdio.h>
#include<stdlib.h>
struct stack {
int top;
int size ;
int *arr;
};
XLII
21CS2113 - Data Structure Using C Laboratory
int pop(struct stack *k){
if(isempty(k)){
printf("stack underflow\n");
}
else {
int val = k->arr[k->top];
k->top--;
return val;
}
}
int main() {
int V, choice, a, b, s;
printf("enter number of vertices :");
scanf("%d", &V);
int arr[V][V];
init(V, arr);
do {
printf("Enter 1 : Add a value \n");
printf("Enter 0 : exit \n");
printf("Enter your choice :");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("enter two vertices :");
scanf("%d%d", &a, &b);
insertEdge(V, arr, a, b);
printAdjMatrix(V, arr);
break;
case 0:
printf("Exit");
break;
}
} while (choice != 0);
printf("enter starting point :");
scanf("%d", &s);
DFS(V, arr, s);
return 0;
}
XLIV
21CS2113 - Data Structure Using C Laboratory
OUTPUT
RESULT
XLV
21CS2113 - Data Structure Using C Laboratory
Exp No. - 11
AIM
ALGORITHM
XLVI
21CS2113 - Data Structure Using C Laboratory
SOURCE CODE
#include <stdio.h>
#include<stdlib.h>
struct queue
{
int front;
int rear;
int size;
int *arr;
};
XLVII
21CS2113 - Data Structure Using C Laboratory
XLIX
21CS2113 - Data Structure Using C Laboratory
int main() {
int V, choice, a, b, s;
printf("enter number of vertices :");
scanf("%d", &V);
int arr[V][V];
init(V, arr);
do {
printf("Enter 1 : Add a value \n");
printf("Enter 0 : exit \n");
printf("Enter your choice :");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("enter two vertices :");
scanf("%d%d", &a, &b);
insertEdge(V, arr, a, b);
printAdjMatrix(V, arr);
break;
case 0:
printf("Exit");
break;
}
} while (choice != 0);
printf("enter starting point :");
scanf("%d", &s);
BFS(V, arr, s);
return 0;
}
L
21CS2113 - Data Structure Using C Laboratory
OUTPUT
RESULT
LI
21CS2113 - Data Structure Using C Laboratory
Exp No. - 12
KRUSKALS ALGORITHM
AIM
ALGORITHM
LII
21CS2113 - Data Structure Using C Laboratory
SOURCE CODE
#include <stdio.h>
#include <stdlib.h>
#define MAX_EDGES 1000
int main() {
int V, E;
printf("Enter number of vertices and edges: ");
scanf("%d %d", &V, &E);
kruskalMST(graph);
return 0; }
LIV
21CS2113 - Data Structure Using C Laboratory
OUTPUT
RESULT
LV