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

Dsa Lab File

This document contains the laboratory record of a student named Kumud Hasija for the course Data Structure Using C Laboratory. It includes the bonafide certificate signed by the faculty incharge and HOD, list of experiments conducted, and sample experiments showing the implementation of algorithms like quick sort, binary search, and infix to postfix conversion.

Uploaded by

Kumud Hasija
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Dsa Lab File

This document contains the laboratory record of a student named Kumud Hasija for the course Data Structure Using C Laboratory. It includes the bonafide certificate signed by the faculty incharge and HOD, list of experiments conducted, and sample experiments showing the implementation of algorithms like quick sort, binary search, and infix to postfix conversion.

Uploaded by

Kumud Hasija
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

(Established under Haryana Private University Act, 2006 as amended by Act No.

8 of 2013)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

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

Name : Kumud Hasija

Reg. No : 11022210056

Subject Code : 21CS2113

Subject : Data Structure Using C Laboratory

Course : B. Tech / Data Science & Artificial Intelligence

Certified that this is the bonafide record of practical done as a part of III
Semester during the academic year 2023 - 2024.

Faculty Incharge Head of the Department


(Dr. M. Mohan) (Dr. Puneet Goswami)

Submitted for University Practical Examination held on 16/01/2024

Internal Examiner External Examiner

I
21CS2113 - Data Structure Using C Laboratory
LIST OF EXPERIMENTS

Ex. No TITLE Page. No

1 QUICK SORT 3-5

2 BINARY SEARCH 6-8

3 INFIX – POSTFIX CONVERSION 9-12

4 INFIX - PREFIX CONVERSION 13-16

5 EVALUATION OF POSTFIX AND PREFIX EXPRESSION 17-20

6 IMPLEMENTATION OF TWO QUEUE’S IN SINGLE ARRAY 21-24

7 IMPLEMENTATION OF BINARY TREE USING ARRAY 25-27

8 INSERT, DELETE, DISPLAY IN AVL TREE 28-34

INSERT, DELETE, DISPLAY (TREE TRAVERSAL) IN BINARY


9 35-39
SEARCH TREE USING LINKED LIST

10 DEPTH SEARCH TRAVERSAL 40-45

11 BREADTH SEARCH TRAVERSAL 46-51

12 KRUSKALS ALGORITHM 52-55

II
21CS2113 - Data Structure Using C Laboratory

Exp No. - 1
QUICK SORT

AIM

Program to demonstrate quick sort in an array.

ALGORITHM

III
21CS2113 - Data Structure Using C Laboratory

SOURCE CODE

#include<stdio.h>

void input_array (int A[], int n)


{
for (int i = 0; i < n; i++)
{ printf ("enter element of array at index %d :", i); scanf ("%d", &A[i]); }
}

void print_array (int A[], int n)


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

int quick (int A[], int f, int l)


{
int pivot, i, j, temp;
pivot = A[f]; i = f + 1; j = l;
do{
while (A[i] <= pivot) { i++ ; }
while (A[j] > pivot) { j--; }
if (i < j)
{ temp = A[i]; A[i] = A[j]; A[j] = temp; }
} while (j > i);
temp = A[j]; A[j] = A[f]; A[f] = temp;
return j;
}

void quick_sort (int A[], int f, int l)


{ int pivot_index;
if (f < l)
{ pivot_index = quick (A, f, l);
quick_sort (A, f, pivot_index - 1);
quick_sort (A, pivot_index + 1, l); }
}

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

Program to demonstrate binary search in an array.

ALGORITHM

VI
21CS2113 - Data Structure Using C Laboratory

SOURCE CODE

#include<stdio.h>

void input_array (int A[], int n)


{
for (int i = 0; i < n; i++)
{ printf ("enter element of array :"); scanf ("%d", &A[i]); }
}

void print_array (int A[], int n)


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

int binary_search (int A[], int f, int l, int n)


{
int mid;
mid = (f + (l - f)) / 2;
if (f <= l)
{
if (A[mid] == n) { printf ("element %d is at index %d \n", n, mid); }
else{
if (A[mid] < n){
f = mid + 1;
binary_search (A, f, l, n);}
else{
l = mid - 1;
binary_search (A, f, l, n);}
}
}
else
{ printf ("element not found "); }
}

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

Program to demonstrate infix-postfix conversion

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; };

int isempty(struct stack *k){


if(k->top==-1){ return 1; }
else{ return 0; }
}

int isfull(struct stack *k){


if(k->top==k->size - 1){ return 1; }
else{ return 0; }
}

void push ( struct stack *k , char val){


if(isfull(k)) { printf("stack overflow\n"); }
else { k->top++;
k->arr[k->top]=val; }
}

int pop(struct stack *k){


if(isempty(k)){ printf("stack underflow\n"); }
else { char val = k->arr[k->top];
k->top--;
return val;}
}

int operator(char op){


if( op == '+' || op == '-' || op == '*' || op == '/' ){ return 1; }
else{ return -1; }
}

int precedence ( char operator){


if(operator=='*' || operator =='/'){ return 2; }
if(operator=='+' || operator =='-'){ return 1; }
else {return 0;}
}

X
21CS2113 - Data Structure Using C Laboratory

char *infixtopostfix( char infix[]){


struct stack * s = (struct stack *)malloc(sizeof(struct stack));
s->size = 100;
s->top = -1;
s->arr = (char *) malloc(s->size * sizeof(char));
char * postfix = (char *) malloc((strlen(infix)+2) * sizeof(char));
int i=0;
int j=0;
while(infix[i]!='\0')
{
if(infix[i]==" "|| infix[i]=="\t") { i++; continue; }
else if(operator(infix[i]) == -1){ postfix[j] = infix[i]; j++; }
else if(infix[i]=='(') { push(s,infix[i]); }
else if (infix[i]==')'){
while( s->top > -1 && s->arr[s->top]!='('){ postfix[j]=pop(s); j++;}
if ( s->arr[s->top] == '(') { pop(s); }
}
else if(operator(infix[i]) == 1){
while(s->top >-1 && precedence(infix[i])<=precedence(s->arr[s->top]))
{ postfix[j]=pop(s); j++; }
push(s,infix[i]);}
i++;
}

while( s->top > -1)


{
postfix[j] = pop(s); j++;
}
postfix[j] = '\0';
return postfix;
}

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

Program to demonstrate infix-prefix conversion

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 isempty(struct stack *k){


if(k->top==-1){ return 1; }
else{ return 0; }
}

int isfull(struct stack *k){


if(k->top==k->size - 1){ return 1; }
else{ return 0; }
}

void push ( struct stack *k , char val){


if(isfull(k)) { printf("stack overflow\n"); }
else { k->top++;
k->arr[k->top]=val; }
}

int pop(struct stack *k){


if(isempty(k)){ printf("stack underflow\n"); }
else { char val = k->arr[k->top];
k->top--;
return val;}
}

void reverse(char *exp) {


int s = strlen(exp); int j = s, i = 0;
char temp[s + 1];
temp[j--] = '\0';
while (exp[i] != '\0') { temp[j] = exp[i]; i++; j--; }
strcpy(exp, temp);
}

int operator(char op){


if( op == '+' || op == '-' || op == '*' || op == '/' ){ return 1; }
else{ return -1; }
}
XIV
21CS2113 - Data Structure Using C Laboratory

int precedence ( char operator){


if(operator=='*' || operator =='/'){ return 2; }
if(operator=='+' || operator =='-'){ return 1; }
else {return 0;}
}

char *infixtoprefix( char infix[]){


reverse(infix);
struct stack * s = (struct stack *)malloc(sizeof(struct stack));
s->size = 100;
s->top = -1;
s->arr = (char *) malloc(s->size * sizeof(char));
char * prefix = (char *) malloc((strlen(infix)+2) * sizeof(char));
int i=0;
int j=0;
while(infix[i]!='\0')
{
if(infix[i]==" "|| infix[i]=="\t") { i++; continue; }
else if(operator(infix[i]) == -1){ prefix[j] = infix[i]; j++; }
else if(infix[i]=='(') { push(s,infix[i]); }
else if (infix[i]==')'){
while( s->top > -1 && s->arr[s->top]!='('){ prefix[j]=pop(s); j++;}
if ( s->arr[s->top] == '(') { pop(s); }
}
else if(operator(infix[i]) == 1){
while(s->top >-1 && precedence(infix[i])<=precedence(s->arr[s->top]))
{ prefix[j]=pop(s); j++; }
push(s,infix[i]);}
i++;
}

while( s->top > -1) { prefix[j] = pop(s); j++;}


prefix[j] = '\0';
reverse(prefix);
return prefix;
}

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

Program to demonstrate evaluation of postfix and prefix expression

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 isoperand(char ch) {


return (ch >= '0' && ch <= '9');
}

void push(struct Stack* stack, int item) {


if (stack->top == stack->size - 1) { printf("Stack Overflow\n"); }
else { stack->top++; stack->arr[stack->top] = item; }
}

int pop(struct Stack* stack) {


if (stack->top == -1) { printf("Stack Underflow\n"); }
else { return stack->arr[stack->top--]; }
}

int evaluatepostfix(char* exp) {


struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->size = 100;
stack->top = -1;
stack->arr = (int*)malloc(stack->size * sizeof(int));
for (int i = 0; exp[i]; ++i) {
if (isoperand(exp[i])) { push(stack, exp[i] - '0'); }
else {
int val1 = pop(stack); int val2 = pop(stack);
switch (exp[i]) {
case '+':
push(stack, val2 + val1);
break;
case '-':
push(stack, val2 - val1);
break;
case '*':
push(stack, val2 * val1);
break;
XVIII
21CS2113 - Data Structure Using C Laboratory
case '/':
push(stack, val2 / val1);
break;
}
}
}
return pop(stack);
}

int evaluateprefix(char* exp) {


struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->size = 100;
stack->top = -1;
stack->arr = (int*)malloc(stack->size * sizeof(int));
int len = strlen(exp);
for (int i = len - 1; i >= 0; i--) {
if (isoperand(exp[i])) { push(stack, exp[i] - '0'); }
else {
int val1 = pop(stack); int val2 = pop(stack);
switch (exp[i]) {
case '+':
push(stack, val1 + val2);
break;
case '-':
push(stack, val1 - val2);
break;
case '*':
push(stack, val1 * val2);
break;
case '/':
push(stack,val1 / val2);
break;
}
}
}
return pop(stack);
}

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

IMPLEMENTATION OF TWO QUEUE’S IN SINGLE ARRAY

AIM

Program to demonstrate implementation of two queues in single array

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];
};

void initialize(struct Queue *q) {


q->front1 = -1; q->rear1 = -1;
q->front2 = MAX_SIZE; q->rear2 = MAX_SIZE;
}

int isFull(struct Queue *q, int queueNumber) {


if (queueNumber == 1) { return q->rear1 == q->front2 - 1; }
else { return q->rear2 == q->front1 + 1; }
}

int isEmpty(struct Queue *q, int queueNumber) {


if (queueNumber == 1) { return q->front1 == -1; }
else { return q->front2 == MAX_SIZE; }
}

void enqueue(struct Queue *q, int data, int queueNumber) {


if (isFull(q, queueNumber)) { printf("Queue %d is full.\n", queueNumber); return; }
if (queueNumber == 1) {
if (q->front1 == -1) { q->front1 = 0; }
q->rear1 = (q->rear1 + 1) % MAX_SIZE; q->arr[q->rear1] = data;
printf("Enqueued %d in Queue %d.\n", data, queueNumber); }
else {
if (q->front2 == MAX_SIZE) { q->front2 = MAX_SIZE - 1; }
q->rear2 = (q->rear2 - 1) % MAX_SIZE; q->arr[q->rear2] = data;
printf("Enqueued %d in Queue %d.\n", data, queueNumber);}
}

int dequeue(struct Queue *q, int queueNumber) {


if (isEmpty(q, queueNumber)) { printf("Queue %d is empty.\n", queueNumber); return -1;}
int data;
if (queueNumber == 1) {
data = q->arr[q->front1];
XXII
21CS2113 - Data Structure Using C Laboratory
if (q->front1 == q->rear1) { q->front1 = -1; q->rear1 = -1; }
else { q->front1 = (q->front1 + 1) % MAX_SIZE; }}
else {
data = q->arr[q->front2];
if (q->front2 == q->rear2) { q->front2 = MAX_SIZE; q->rear2 = MAX_SIZE;}
else { q->front2 = (q->front2 - 1) % MAX_SIZE; }
}
printf("Dequeued %d from Queue %d.\n", data, queueNumber); return data;
}

void display(struct Queue *q, int queueNumber) {


if (isEmpty(q, queueNumber)) { printf("Queue %d is empty.\n", queueNumber); return; }
printf("Queue %d elements: ", queueNumber);
if (queueNumber == 1) {
for (int i = q->front1; i <= q->rear1; i = (i + 1) % MAX_SIZE) {
printf("%d ", q->arr[i]);}}
else {
for (int i = q->front2; i >= q->rear2; i = (i - 1) % MAX_SIZE) { printf("%d ", q->arr[i]);}
}
printf("\n");
}

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

IMPLEMENTATION OF BINARY TREE USING ARRAY

AIM

Program to demonstrate implementation of binary tree using array

ALGORITHM

XXV
21CS2113 - Data Structure Using C Laboratory

SOURCE CODE

#include <stdio.h>

void insert(int tree[] ,int data, int index) {


if (tree[index] == 0) {
tree[index] = data;
} else {
if (data < tree[index]) {
insert(tree, data, 2 * index + 1);
} else {
insert(tree ,data, 2 * index + 2);
}
}
}

void print(int* tree, int size)


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

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

INSERT, DELETE, DISPLAY IN AVL TREE

AIM

Program to demonstrate insertion , deletion , display in avl tree

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;
};

int getHeight(struct node *n){


if(n==NULL)
return 0;
return n->height;
}

struct node *createnode(int data){


struct node* n = (struct node *) malloc(sizeof(struct node));
n->data = data; n->height = 1;
n->left = NULL; n->right = NULL;
return n;
}

int max (int a, int b){


return (a>b)?a:b;
}

int BalanceFactor(struct node * n){


if(n==NULL){ return 0; }
return getHeight(n->left) - getHeight(n->right);
}

struct node* rightRotate(struct node* y){


struct node* x = y->left;
struct node* T2 = x->right;
x->right = y;
y->left = T2;
x->height = max(getHeight(x->right), getHeight(x->left)) + 1;
y->height = max(getHeight(y->right), getHeight(y->left)) + 1;

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;
}

struct node *insert(struct node* n, int data){


if (n == NULL)
return createnode(data);

if (data < n->data)


n->left = insert(n->left, data);

else if (data > n->data)


n->right = insert(n->right, data);

n->height = 1 + max(getHeight(n->left), getHeight(n->right));

int bf = BalanceFactor(n);

// Left Left Case


if(bf>1 && data < n->left->data){
return rightRotate(n);
}
// Right Right Case
if(bf<-1 && data > n->right->data){
return leftRotate(n);
}
// Left Right Case
if(bf>1 && data > n->left->data){
n->left = leftRotate(n->left);
return rightRotate(n);
}
// Right Left Case
if(bf<-1 && data < n->right->data){
n->right = rightRotate(n->right);
return leftRotate(n);
}
return 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;
}

struct node * delete(struct node* root, int value){

struct node *temp;


if (root == NULL){
return NULL;
}

if(root->left == NULL && root->right == NULL){


free(root);
return NULL;
}

if(root->data > value){


root->left = delete(root->left,value);
return root;
}

if(root->data < value){


root->right = delete(root->right,value);
return root;
}

else{
temp = inorderpredessor(root);
root->data = temp->data;
root->left =delete(root->left,temp->data);
}

root->height = 1 + max(getHeight(root->left), getHeight(root->right));

int bf = BalanceFactor(root);

// Left Left Case


if(bf>1 && BalanceFactor(root->left)>=0){
return rightRotate(root);
}
// Right Right Case
if(bf<-1 && BalanceFactor(root->right)<=0){
return leftRotate(root);}
XXXI
21CS2113 - Data Structure Using C Laboratory

// Left Right Case


if(bf>1 && BalanceFactor(root->left)<0){
root->left = leftRotate(root->left);
return rightRotate(root);}

// Right Left Case


if(bf<-1 && BalanceFactor(root->right)>0){
root->right = rightRotate(root->right);
return leftRotate(root);}

return root;
}

void preorder(struct node *root)


{
if(root != NULL)
{ printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

void inorder(struct node *root)


{
if(root != NULL)
{ inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

void postorder(struct node *root)


{
if(root != NULL)
{ postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}

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");
}

} while (choice != 6);


return 0;}

XXXIII
21CS2113 - Data Structure Using C Laboratory
OUTPUT

RESULT

XXXIV
21CS2113 - Data Structure Using C Laboratory
Exp No. – 9

INSERT, DELETE, DISPLAY (TREE TRAVERSAL) IN BINARY SEARCH


TREE USING LINKED LIST

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;
};

struct node *createnode(int data)


{ struct node *k = (struct node *)malloc(sizeof(struct node));
k->data = data;
k->left = NULL;
k->right = NULL;
return k;
};

struct node * insert(struct node *root, int value)


{
if(root==NULL){ return createnode(value); }
else{
if (root->data == value)
{
printf("cannot insert value %d , already exist", value);
}
else if (root->data > value)
{
root->left = insert(root->left,value) ;
}
else
{
root->right = insert(root->right,value);
}
} return root;
}

struct node * inorderpredessor(struct node * root){


root = root->left;
while(root->right != NULL){
root = root->right;
}
return root;}
struct node * delete(struct node* root, int value){
XXXVI
21CS2113 - Data Structure Using C Laboratory
struct node *temp;
if (root == NULL){
return NULL;
}

if(root->left == NULL && root->right == NULL){


free(root);
return NULL;
}

if(root->data > value){


root->left = delete(root->left,value);
return root;
}

if(root->data < value){


root->right = delete(root->right,value);
return root;
}

else{
temp = inorderpredessor(root);
root->data = temp->data;
root->left =delete(root->left,temp->data);
} return root;
}

void preorder(struct node *root)


{
if (root != NULL)
{
printf("%d \t", root->data);
preorder(root->left);
preorder(root->right);
}
}

void postorder(struct node *root)


{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d \t", root->data);
}}
void inorder(struct node *root)
XXXVII
21CS2113 - Data Structure Using C Laboratory
{
if (root != NULL)
{
inorder(root->left);
printf("%d \t", root->data);
inorder(root->right);
}
}

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");
}

} while (choice != 6);


return 0;}

OUTPUT

XXXIX
21CS2113 - Data Structure Using C Laboratory
RESULT

XL
21CS2113 - Data Structure Using C Laboratory

Exp No. - 10

DEPTH SEARCH TRAVERSAL

AIM

Program to demonstrate depth search algorithm .

ALGORITHM

XLI
21CS2113 - Data Structure Using C Laboratory

SOURCE CODE

#include <stdio.h>
#include<stdlib.h>

struct stack {
int top;
int size ;
int *arr;
};

struct stack * createstack ( int size ){


struct stack * k = (struct stack *) malloc(sizeof(struct stack));
k->size=size;
k->top=-1;
k->arr=(int *) malloc(k->size * sizeof(int));
return k;
}

int isempty(struct stack *k){


if(k->top==-1){
return 1;
}
else{
return 0;}
}

int isfull(struct stack *k){


if(k->top==k->size - 1){
return 1;
}
else{
return 0;}
}

int push ( struct stack *k , int val){


if(isfull(k)){
printf("stack overflow\n");
}
else {
k->top++;
k->arr[k->top]=val;
}
}

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;
}
}

void init(int V ,int arr[V][V]) {


int i, j;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
arr[i][j] = 0;
}

void insertEdge(int V ,int arr[V][V], int i, int j) {


arr[i][j] = 1;
arr[j][i] = 1;
}

void printAdjMatrix(int V ,int arr[V][V]) {


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

int DFS( int V, int g[V][V] , int s){


int visited[V];
struct stack * k = createstack(20);
for(int i = 0 ; i<V ; i++){
visited[i]=0;}
push(k,s);
visited[s]=1;
printf("%d \t",s);
while(isempty(k)==0){
int i = pop(k);
for(int j = 0 ;j<V ; j++){
if(visited[j]==0 && g[i][j]==1){
printf("%d \t",j);
XLIII
21CS2113 - Data Structure Using C Laboratory
push(k,j);
visited[j]=1;
}
}
}
}

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

BREADTH SEARCH TRAVERSAL

AIM

Program to demonstrate breadth search traversal .

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;
};

struct queue *createqueue(int size)


{
struct queue *k = (struct queue *)malloc(sizeof(struct queue));
k->size = size;
k->front = k->rear = -1;
k->arr = (int *)malloc(k->size * sizeof(int));
return k;
};

int isempty(struct queue *k)


{
if (k->front == k->rear == -1)
{return 1;}
else
{return 0;}
}

int isfull(struct queue *k)


{
if (k->rear == k->size - 1)
{return 1;}
else
{return 0;}
}

XLVII
21CS2113 - Data Structure Using C Laboratory

int enqueue(struct queue *k, int val)


{
if (isfull(k))
{
printf("queue is overflow\n");
}
else
{
if (k->front == k->rear == -1)
{
k->front++;
k->rear++;
k->arr[k->rear] = val;
}
else
{
k->rear++;
k->arr[k->rear] = val;
}
}
}

int dequeue(struct queue *k)


{
if (isempty(k) == 1)
{
printf("stack underflow\n");
}
else
{
if (k->front == k->rear)
{
int val = k->arr[k->front];
k->front = k->rear = -1;
return val;
}
else
{
int val = k->arr[k->front];
k->arr[k->front] = 0;
k->front++;
return val;
}
}
}
XLVIII
21CS2113 - Data Structure Using C Laboratory

void init(int V ,int arr[V][V]) {


int i, j;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
arr[i][j] = 0;
}

void insertEdge(int V ,int arr[V][V], int i, int j) {


arr[i][j] = 1;
arr[j][i] = 1;
}

void printAdjMatrix(int V ,int arr[V][V]) {


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

int BFS( int V ,int g[V][V] , int s){


int visited[V];
struct queue * k = createqueue(20);
for(int i = 0 ; i<V ; i++){
visited[i]=0;}
enqueue(k,s);
visited[s]=1;
printf("%d \t",s);
while(isempty(k)==0){
int i = dequeue(k);
for(int j = 0 ;j<V ; j++){
if(visited[j]==0 && g[i][j]==1){
printf("%d \t",j);
enqueue(k,j);
visited[j]=1;
}
}
}
}

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

Program to demonstrate kruskals algorithm .

ALGORITHM

LII
21CS2113 - Data Structure Using C Laboratory

SOURCE CODE

#include <stdio.h>
#include <stdlib.h>
#define MAX_EDGES 1000

typedef struct Edge {


int src, dest, weight;
} Edge;

typedef struct Graph {


int V, E;
Edge edges[MAX_EDGES];
} Graph;

typedef struct Subset {


int parent, rank;
} Subset;

Graph* createGraph(int V, int E) {


Graph* graph = (Graph*) malloc(sizeof(Graph));
graph->V = V;
graph->E = E;
return graph;
}

int find(Subset subsets[], int i) {


if (subsets[i].parent != i) {
subsets[i].parent = find(subsets, subsets[i].parent); }
return subsets[i].parent;
}

void Union(Subset subsets[], int x, int y) {


int xroot = find(subsets, x);
int yroot = find(subsets, y);

if (subsets[xroot].rank < subsets[yroot].rank) {


subsets[xroot].parent = yroot;
} else if (subsets[xroot].rank > subsets[yroot].rank) {
subsets[yroot].parent = xroot;
} else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
LIII
21CS2113 - Data Structure Using C Laboratory

int compare(const void* a, const void* b) {


Edge* a_edge = (Edge*) a;
Edge* b_edge = (Edge*) b;
return a_edge->weight - b_edge->weight;
}

void kruskalMST(Graph* graph) {


Edge mst[graph->V];
int e = 0, i = 0;
qsort(graph->edges, graph->E, sizeof(Edge), compare);
Subset* subsets = (Subset*) malloc(graph->V * sizeof(Subset));
for (int v = 0; v < graph->V; ++v) {
subsets[v].parent = v;
subsets[v].rank = 0;
}
while (e < graph->V - 1 && i < graph->E) {
Edge next_edge = graph->edges[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
if (x != y) {
mst[e++] = next_edge;
Union(subsets, x, y);
}
}
printf("Minimum Spanning Tree:\n");
for (i = 0; i < e; ++i) {
printf("(%d, %d) -> %d\n", mst[i].src, mst[i].dest, mst[i].weight);
}
}

int main() {
int V, E;
printf("Enter number of vertices and edges: ");
scanf("%d %d", &V, &E);

Graph* graph = createGraph(V, E);

printf("Enter edges and their weights:\n");


for (int i = 0; i < E; ++i) {
scanf("%d %d %d", &graph->edges[i].src, &graph->edges[i].dest, &graph->edges[i].weight);
}

kruskalMST(graph);

return 0; }
LIV
21CS2113 - Data Structure Using C Laboratory

OUTPUT

RESULT

LV

You might also like