You are on page 1of 71

LAB MANUAL

COURSE CODE

: 18CBC101J

COURSE NAME

: DATASTRUCTURES &ALGORITHMS
SEMESTER

: II

BRANCH

: CSBS

ACADEMIC YEAR

: 2022 - 2023

STAFF INCHARGE

HOD

EX NO: 1 TOWERS OF HANOI USING USER DEFINED STACKS

AIM:

To perform towers of Hanoi using user defined stacks.

ALGORITHM:

START

ProcedureHanoi(disk, source, dest, aux) IF disk ==1, THEN

move disk from source to dest

ELSE

Hanoi(disk -1, source, aux, dest)

move disk from source to dest

Hanoi(disk -1, aux, dest, source)

END IF
ENDProcedure

STOP

PROGRAM:

#include<stdio.h>

void TOH(int n,char x,char y,char z)

if(n>0)

TOH(n-1,x,z,y);

printf("\n%c to %c",x,y);

TOH(n-1,z,y,x);

int main()

int n=3;

TOH(n,'A','B','C');

RESULT:

EX NO: 2
READING, WRITING AND ADDITION OF POLYNOMIALS

AIM:

To perform reading , writing, and addition operations using polynomials.

ALGORITHM:

START

Create a sum array sum[] of size equal to maximum of 'm' and 'n'

Copy A[] to sum[].

Travers array B[] and do following for every element B[i]

sum[i] = sum[i] + B[i]

Return sum[].

STOP

PROGRAM:

#include<stdio.h>

struct poly

int coeff;

int expo;

};

struct poly p1[10],p2[10],p3[10];

int readPoly(struct poly []);


int addPoly(struct poly [],struct poly [],int ,int ,struct poly []); void
displayPoly( struct poly [],int terms); int main()

int t1,t2,t3;

t1=readPoly(p1);

printf(" \n First polynomial : "); displayPoly(p1,t1);

t2=readPoly(p2);

printf(" \n Second polynomial : "); displayPoly(p2,t2);

t3=addPoly(p1,p2,t1,t2,p3);

printf(" \n\n Resultant polynomial after addition : "); displayPoly(p3,t3);

printf("\n");

return 0;

int readPoly(struct poly p[10])

int t1,i;

printf("\n\n Enter the total number of terms in the polynomial:");


scanf("%d",&t1);

printf("\n Enter the COEFFICIENT and EXPONENT in DESCENDING


ORDER\n"); for(i=0;i<t1;i++)

{
printf(" Enter the Coefficient(%d): ",i+1); scanf("%d",&p[i].coeff); printf("

Enter the exponent(%d): ",i+1); scanf("%d",&p[i].expo);

return(t1);

int addPoly(struct poly p1[10],struct poly p2[10],int t1,int t2,struct poly


p3[10])

int i,j,k;

i=0;

j=0;

k=0;

while(i<t1 && j<t2)

if(p1[i].expo==p2[j].expo)

p3[k].coeff=p1[i].coeff + p2[j].coeff; p3[k].expo=p1[i].expo;

i++;

j++;

k++;

}
else if(p1[i].expo>p2[j].expo)

p3[k].coeff=p1[i].coeff;

p3[k].expo=p1[i].expo;

i++;

k++;

else

p3[k].coeff=p2[j].coeff;

p3[k].expo=p2[j].expo;

j++;

k++;

while(i<t1)

p3[k].coeff=p1[i].coeff;

p3[k].expo=p1[i].expo;

i++;
k++;

while(j<t2)

p3[k].coeff=p2[j].coeff;

p3[k].expo=p2[j].expo;

j++;

k++;

return(k); }

void displayPoly(struct poly p[10],int term)

int k;

for(k=0;k<term-1;k++) printf("%d(x^%d)+",p[k].coeff,p[k].expo);
printf("%d(x^%d)",p[term-1].coeff,p[term-1].expo);

RESULT:

EX NO: 3 LINE EDITORS WITH LINE COUNT, WORD COUNT

SHOWING ON THE SCREEN

AIM:

To perform line editors with line count, word count showing on the screen.
ALGORITHM:

START

Read the text until an empty line

Compare each character with newline to count no of lines Compare each


character with tab or space ‘ ‘ to count no of words Compare first character
with NULL to find the end of text No of characters = length of each line of
text Print no of lines, no of words, no of chars STOP

PROGRAM:

#include <stdio.h>

#include <conio.h>

#include <string.h>

void main()

char str[100];

int i = 0, l = 0, f = 1;

clrscr();

puts("Enter any string\n"); gets(str);

for(i = 0; str[i] !='\0'; i++)

l = l + 1;

}
printf("The number of characters in the string are %d\n", l); for(i = 0; i <= l-
1; i++)

if(str[i] == ' ')

f = f + 1;

printf("The number of words in the string are %d", f); getch();

RESULT:

EX NO: 4

TREES WITH ALL OPERATIONS

AIM:

To perform all the basic operations in B tree, B+ tree , AVL tree and splay
tree.

ALGORITHM:

START

If root is NULL

then create root node

return
If root exists then

compare the data with node. data

while until insertion position is locate If data is greater than node. data

Go to right sub tree

else

go to left sub tree

end while

insert data

end If

STOP

PROGRAM:

1.B TREE

#include <stdio.h>

#include <stdlib.h>

#define MAX 4

#define MIN 2

struct btreeNode {

int val[MAX + 1], count;

struct btreeNode *link[MAX + 1];

};
struct btreeNode *root; struct btreeNode * createNode(int val, struct
btreeNode *child) {

struct btreeNode *newNode;

newNode = (struct btreeNode *)malloc(sizeof(struct btreeNode));


newNode->val[1] = val;

newNode->count = 1;

newNode->link[0] = root;

newNode->link[1] = child;

return newNode;

void addValToNode(int val, int pos, struct btreeNode *node, struct


btreeNode *child) {

int j = node->count;

while (j > pos) {

node->val[j + 1] = node->val[j]; node->link[j + 1] = node->link[j]; j--;

node->val[j + 1] = val;

node->link[j + 1] = child;

node->count++;

void splitNode (int val, int *pval, int pos, struct btreeNode *node, struct
btreeNode *child, struct btreeNode **newNode) {
int median, j;

if (pos > MIN)

median = MIN + 1;

else

median = MIN;

*newNode = (struct btreeNode *)malloc(sizeof(struct btreeNode)); j =


median + 1;

while (j <= MAX) {

(*newNode)->val[j - median] = node->val[j]; (*newNode)->link[j -


median] = node->link[j]; j++;

node->count = median;

(*newNode)->count = MAX - median;

if (pos <= MIN) {

addValToNode(val, pos, node, child);

} else {

addValToNode(val, pos - median, *newNode, child);

*pval = node->val[node->count];

(*newNode)->link[0] = node->link[node->count]; node->count--;

}
/* sets the value val in the node */

int setValueInNode(int val, int *pval, struct btreeNode *node, struct


btreeNode **child) {

int pos;

if (!node) {

*pval = val;

*child = NULL;

return 1;

if (val < node->val[1]) {

pos = 0;

} else {

for (pos = node->count;

(val < node->val[pos] && pos > 1); pos--); if (val == node->val[pos]) {

printf("Duplicates not allowed\n"); return 0;

if (setValueInNode(val, pval, node->link[pos], child)) {

if (node->count < MAX) {

addValToNode(*pval, pos, node, *child);

} else {
splitNode(*pval, pval, pos, node, *child, child); return 1;

return 0;

void insertion(int val) {

int flag, i;

struct btreeNode *child;

flag = setValueInNode(val, &i, root, &child); if (flag)

root = createNode(i, child);

void copySuccessor(struct btreeNode *myNode, int pos) {

struct btreeNode *dummy;

dummy = myNode->link[pos];

for (;dummy->link[0] != NULL;)

dummy = dummy->link[0];

myNode->val[pos] = dummy->val[1];

void removeVal(struct btreeNode *myNode, int pos) {

int i = pos + 1;
while (i <= myNode->count) {

myNode->val[i - 1] = myNode->val[i]; myNode->link[i - 1] = myNode-


>link[i]; i++;

myNode->count--;

void doRightShift(struct btreeNode *myNode, int pos) {

struct btreeNode *x = myNode->link[pos]; int j = x->count;

while (j > 0) {

x->val[j + 1] = x->val[j];

x->link[j + 1] = x->link[j];

x->val[1] = myNode->val[pos];

x->link[1] = x->link[0];

x->count++;

x = myNode->link[pos - 1];

myNode->val[pos] = x->val[x->count]; myNode->link[pos] = x->link[x-


>count]; x->count--;

return;

void doLeftShift(struct btreeNode *myNode, int pos) {


int j = 1;

struct btreeNode *x = myNode->link[pos - 1]; x->count++;

x->val[x->count] = myNode->val[pos]; x->link[x->count] = myNode-


>link[pos]->link[0]; x = myNode->link[pos];

myNode->val[pos] = x->val[1];

x->link[0] = x->link[1];

x- >count--;

while (j <= x->count) {

x->val[j] = x->val[j + 1];

x->link[j] = x->link[j + 1];

j++;

return;

void mergeNodes(struct btreeNode *myNode, int pos) {

int j = 1;

struct btreeNode *x1 = myNode->link[pos], *x2 = myNode->link[pos - 1];

x2->count++;

x2->val[x2->count] = myNode->val[pos]; x2->link[x2->count] = myNode-


>link[0]; while (j <= x1->count) {

x2->count++;
x2->val[x2->count] = x1->val[j]; x2->link[x2->count] = x1->link[j]; j++;

j = pos;

while (j < myNode->count) {

myNode->val[j] = myNode->val[j + 1]; myNode->link[j] = myNode-


>link[j + 1]; j++;

myNode->count--;

free(x1);

void adjustNode(struct btreeNode *myNode, int pos) {

if (!pos) {

if (myNode->link[1]->count > MIN) {

doLeftShift(myNode, 1);

} else {

mergeNodes(myNode, 1);

} else {

if (myNode->count != pos) {

if(myNode->link[pos - 1]->count > MIN) {

doRightShift(myNode, pos);
} else {

if (myNode->link[pos + 1]->count > MIN) {

doLeftShift(myNode, pos + 1);

} else {

mergeNodes(myNode, pos);

} else {

if (myNode->link[pos - 1]->count > MIN) doRightShift(myNode, pos);

else

mergeNodes(myNode, pos);

int delValFromNode(int val, struct btreeNode *myNode) {

int pos, flag = 0;

if (myNode) {

if (val < myNode->val[1]) {

pos = 0;

flag = 0;
} else {

for (pos = myNode->count;

(val < myNode->val[pos] && pos > 1); pos--); if (val == myNode-
>val[pos]) {

flag = 1;

} else {

flag = 0;

if (flag) {

if (myNode->link[pos - 1]) {

copySuccessor(myNode, pos);

flag = delValFromNode(myNode->val[pos], myNode->link[pos]); if (flag


== 0) {

printf("Given data is not present in B-Tree\n");

} else {

removeVal(myNode, pos);

} else {

flag = delValFromNode(val, myNode->link[pos]);


}

if (myNode->link[pos]) {

if (myNode->link[pos]->count < MIN) adjustNode(myNode, pos);

return flag;

void deletion(int val, struct btreeNode *myNode) {

struct btreeNode *tmp;

if (!delValFromNode(val, myNode)) {

printf("Given value is not present in B-Tree\n"); return;

} else {

if (myNode->count == 0) {

tmp = myNode;

myNode = myNode->link[0];

free(tmp);

root = myNode;

return;
}

void searching(int val, int *pos, struct btreeNode *myNode) {

if (!myNode) {

return;

if (val < myNode->val[1]) {

*pos = 0;

} else {

for (*pos = myNode->count;

(val < myNode->val[*pos] && *pos > 1); (*pos)--); if (val == myNode-
>val[*pos]) {

printf("Given data %d is present in B-Tree", val); return;

searching(val, pos, myNode->link[*pos]); return;

void traversal(struct btreeNode *myNode) {

int i;

if (myNode) {

for (i = 0; i < myNode->count; i++) {

traversal(myNode->link[i]);
printf("%d ", myNode->val[i + 1]);

traversal(myNode->link[i]);

int main() {

int val, ch;

while (1) {

printf("1. Insertion\t2. Deletion\n"); printf("3. Searching\t4. Traversal\n");


printf("5. Exit\nEnter your choice:"); scanf("%d", &ch);

switch (ch) {

case 1:

printf("Enter your input:"); scanf("%d", &val);

insertion(val);

break;

case 2:

printf("Enter the element to delete:"); scanf("%d", &val);

deletion(val, root);

break;

case 3:

printf("Enter the element to search:"); scanf("%d", &val);


searching(val, &ch, root);

break;

case 4:

traversal(root);

break;

case 5:

exit(0);

default:

printf("U have entered wrong option!!\n");

break;

printf("\n");

RESULT:

1. AVL TREE

#include <stdio.h>

#include <stdlib.h>

#define FALSE 0

#define TRUE 1
struct node

struct node *lchild;

int info;

struct node *rchild;

int balance;

};

void inorder(struct node *ptr);

struct node *RotateLeft(struct node *pptr); struct node *RotateRight(struct


node *pptr); struct node *insert(struct node *pptr, int ikey); struct node
*insert_left_check(struct node *pptr, int *ptaller); struct node
*insert_right_check(struct node *pptr, int *ptaller); struct node
*insert_LeftBalance(struct node *pptr); struct node
*insert_RightBalance(struct node *pptr); struct node *del(struct node *pptr,
int dkey); struct node *del_left_check(struct node *pptr, int *pshorter);
struct node *del_right_check(struct node *pptr, int *pshorter); struct node
*del_LeftBalance(struct node *pptr,int *pshorter); struct node
*del_RightBalance(struct node *pptr,int *pshorter); void display(struct
node *ptr,int level);

int main()

int choice,key;

struct node *root = NULL;

while(1)

{
printf("\n");

printf("1.Insert\n");

printf("2.Display\n");

printf("3.Delete\n");

printf("4.Inorder Traversal\n"); printf("5.Quit\n");

printf("\nEnter your choice : "); scanf("%d",&choice);

switch(choice)

case 1:

printf("\nEnter the key to be inserted : "); scanf("%d",&key);

root = insert(root,key);

break;

case 2:

printf("\n");

display(root,0);

printf("\n");

break;

case 3:

printf("\nEnter the key to be deleted : "); scanf("%d",&key);

root = del(root,key);
break;

case 4:

inorder(root);

break;

case 5:

exit(1);

default:

printf("Wrong choice\n");

return 0;

void display(struct node *ptr,int level)

int i;

if(ptr == NULL )/*Base Case*/

return;

else

display(ptr->rchild, level+1);

printf("\n");
for (i=0; i<level; i++)

printf(" ");

printf("%d", ptr->info); display(ptr->lchild, level+1);

struct node *insert(struct node *pptr, int ikey)

static int taller;

if(pptr==NULL)

pptr = (struct node *) malloc(sizeof(struct node)); pptr->info = ikey;

pptr->lchild = NULL;

pptr->rchild = NULL;

pptr->balance = 0;

taller = TRUE;

else if(ikey < pptr->info)

pptr->lchild = insert(pptr->lchild, ikey); if(taller==TRUE)

pptr = insert_left_check( pptr, &taller );


}

else if(ikey > pptr->info)

pptr->rchild = insert(pptr->rchild, ikey);

if(taller==TRUE)

pptr = insert_right_check(pptr, &taller);

else

printf("Duplicate key\n");

taller = FALSE;

return pptr;

struct node *insert_left_check(struct node *pptr, int *ptaller )

switch(pptr->balance)

case 0:

pptr->balance = 1;
break;

case -1:

pptr->balance = 0;

*ptaller = FALSE;

break;

case 1:

pptr = insert_LeftBalance(pptr);

*ptaller = FALSE;

return pptr;

struct node *insert_right_check(struct node *pptr, int *ptaller )

switch(pptr->balance)

case 0:

pptr->balance = -1;

break;

case 1:

pptr->balance = 0;
*ptaller = FALSE;

break;

case -1:

pptr = insert_RightBalance(pptr);

*ptaller = FALSE;

return pptr;

struct node *insert_LeftBalance(struct node *pptr)

struct node *aptr, *bptr;

aptr = pptr->lchild;

if(aptr->balance == 1) /* Case L_C1 : Insertion in AL */

pptr->balance = 0;

aptr->balance = 0;

pptr = RotateRight(pptr);

else

{
bptr = aptr->rchild;

switch(bptr->balance)

case -1:

pptr->balance = 0;

aptr->balance = 1;

break;

case 1:

pptr->balance = -1;

aptr->balance = 0;

break;

case 0:

pptr->balance = 0;

aptr->balance = 0;

bptr->balance = 0;

pptr->lchild = RotateLeft(aptr);

pptr = RotateRight(pptr);

return pptr;
}

struct node *insert_RightBalance(struct node *pptr)

struct node *aptr, *bptr;

aptr = pptr->rchild;

if(aptr->balance == -1)

pptr->balance = 0;

aptr->balance = 0;

pptr = RotateLeft(pptr);

else

bptr = aptr->lchild;

switch(bptr->balance)

case -1:

pptr->balance = 1;

aptr->balance = 0;

break;
case 1:

pptr->balance = 0;

aptr->balance = -1;

break;

case 0:

pptr->balance = 0;

aptr->balance = 0;

bptr->balance = 0;

pptr->rchild = RotateRight(aptr);

pptr = RotateLeft(pptr);

return pptr;

struct node *RotateLeft(struct node *pptr)

struct node *aptr;

aptr = pptr->rchild;

pptr->rchild = aptr->lchild;

aptr->lchild = pptr;
return aptr;

struct node *RotateRight(struct node *pptr)

struct node *aptr;

aptr = pptr->lchild;

pptr->lchild = aptr->rchild;

aptr->rchild = pptr;

return aptr;

struct node *del(struct node *pptr, int dkey)

struct node *tmp, *succ; static int shorter;

if( pptr == NULL)

printf("Key not present \n"); shorter = FALSE;

return(pptr);

if( dkey < pptr->info )

{
pptr->lchild = del(pptr->lchild, dkey); if(shorter == TRUE)

pptr = del_left_check(pptr, &shorter);

else if( dkey > pptr->info )

pptr->rchild = del(pptr->rchild, dkey); if(shorter==TRUE)

pptr = del_right_check(pptr, &shorter);

else

if( pptr->lchild!=NULL && pptr->rchild!=NULL )

succ = pptr->rchild;

while(succ->lchild)

succ = succ->lchild;

pptr->info = succ->info;

pptr->rchild = del(pptr->rchild, succ->info); if( shorter == TRUE )

pptr = del_right_check(pptr, &shorter);

else
{

tmp = pptr;

if( pptr->lchild != NULL )

pptr = pptr->lchild;

else if( pptr->rchild != NULL)

pptr = pptr->rchild;

else /* no children */

pptr = NULL;

free(tmp);

shorter = TRUE;

return pptr;

struct node *del_left_check(struct node *pptr, int *pshorter)

switch(pptr->balance)

case 0:

pptr->balance = -1;
*pshorter = FALSE;

break;

case 1: pptr->balance = 0;

break;

case -1: pptr = del_RightBalance(pptr, pshorter);

return pptr;

struct node *del_right_check(struct node *pptr, int *pshorter)

switch(pptr->balance)

case 0:

pptr->balance = 1;

*pshorter = FALSE;

break;

case -1:

pptr->balance = 0;

break

pptr = del_LeftBalance(pptr, pshorter );


}

return pptr;

struct node *del_LeftBalance(struct node *pptr,int *pshorter)

struct node *aptr, *bptr;

aptr = pptr->lchild;

if( aptr->balance == 0)

pptr->balance = 1;

aptr->balance = -1;

*pshorter = FALSE;

pptr = RotateRight(pptr);

else if(aptr->balance == 1 )

pptr->balance = 0;

aptr->balance = 0;

pptr = RotateRight(pptr);

}
else

bptr = aptr->rchild;

switch(bptr->balance)

case 0:

pptr->balance = 0;

aptr->balance = 0;

break;

case 1:

pptr->balance = -1;

aptr->balance = 0;

break;

case -1: pptr->balance = 0;

aptr->balance = 1;

bptr->balance = 0;

pptr->lchild = RotateLeft(aptr);

pptr = RotateRight(pptr);

}
return pptr;

struct node *del_RightBalance(struct node *pptr,int *pshorter)

struct node *aptr, *bptr;

aptr = pptr->rchild;

if (aptr->balance == 0)

pptr->balance = -1;

aptr->balance = 1;

*pshorter = FALSE;

pptr = RotateLeft(pptr);

else if(aptr->balance == -1 )

pptr->balance = 0;

aptr->balance = 0;

pptr = RotateLeft(pptr);

else
{

bptr = aptr->lchild;

switch(bptr->balance)

case 0:

pptr->balance = 0;

aptr->balance = 0;

break;

case 1:

pptr->balance = 0;

aptr->balance = -1;

break;

case -1: pptr->balance = 1;

aptr->balance = 0;

bptr->balance = 0;

pptr->rchild = RotateRight(aptr);

pptr = RotateLeft(pptr);

return pptr;
}

void inorder(struct node *ptr)

if(ptr!=NULL)

inorder(ptr->lchild);

printf("%d ",ptr->info); inorder(ptr->rchild);

RESULT:

1. SPLAY TREE

#include<stdio.h>

#include<stdlib.h>

struct node

int key;

struct node *left, *right;

};

struct node* newNode(int key)

struct node* node = (struct node*)malloc(sizeof(struct node)); node->key =


key;
node->left = node->right = NULL; return (node);

struct node *rightRotate(struct node *x)

struct node *y = x->left;

x->left = y->right;

y->right = x;

return y;

struct node *leftRotate(struct node *x)

struct node *y = x->right;

x->right = y->left;

y->left = x;

return y;

struct node *splay(struct node *root, int key)

if (root == NULL || root->key == key) return root;

if (root->key > key)


{

if (root->left == NULL) return root; if (root->left->key > key)

root->left->left = splay(root->left->left, key); else

root = rightRotate(root);

else if (root->left->key < key) // Zig-Zag (Left Right)

root->left->right = splay(root->left->right, key); if (root->left->right !=


NULL)

root->left = leftRotate(root->left);

return (root->left == NULL)? root: rightRotate(root);

else

if (root->right == NULL) return root; if (root->right->key > key)

root->right->left = splay(root->right->left, key); if (root->right->left !=


NULL)

root->right = rightRotate(root->right);
}

else if (root->right->key < key)// Zag-Zag (Right Right)

root->right->right = splay(root->right-

>right, key); root = leftRotate(root);

return (root->right == NULL)? root: leftRotate(root);

struct node *search(struct node *root, int key)

return splay(root, key);

void preOrder(struct node *root)

if (root != NULL)

printf("%d ", root->key); preOrder(root->left);

preOrder(root->right);

}
}

int main()

struct node *root = newNode(100);

root->left = newNode(50);

root->right = newNode(200);

root->left->left = newNode(40); root->left->left->left = newNode(30); root-


>left->left->left->left = newNode(20); root = search(root, 20);

printf("Preorder traversal of the modified Splay tree is \n"); preOrder(root);

return 0;

RESULT:

EX NO:5

ALL GRAPH ALGORITHMS

AIM:

To perform all the graph algorithms using c program.

ALGORITHM:

START

Initialize the minimum spanning tree with a vertex chosen at random.

Find all the edges that connect the tree to new vertices, find the minimum
and add it to the tree
Keep repeating step 2 until we get a minimum spanning tree STOP

PROGRAM:

PRIMS ALGORITHM:

#include<stdio.h>

#include<conio.h>

int n, cost[10][10];

void prim() {

int i, j, startVertex, endVertex;

int k, nr[10], temp, minimumCost = 0, tree[10][3]; temp = cost[0][0];

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

if (temp > cost[i][j]) {

temp = cost[i][j];

startVertex = i;

endVertex = j;

tree[0][0] = startVertex;

tree[0][1] = endVertex;
tree[0][2] = temp;

minimumCost = temp;

for (i = 0; i < n; i++) {

if (cost[i][startVertex] < cost[i][endVertex]) nr[i] = startVertex;

else

nr[i] = endVertex;

nr[startVertex] = 100;

nr[endVertex] = 100;

temp = 99;

for (i = 1; i < n - 1; i++) {

for (j = 0; j < n; j++) {

if (nr[j] != 100 && cost[j][nr[j]] < temp) {

temp = cost[j][nr[j]];

k = j;

tree[i][0] = k;

tree[i][1] = nr[k];

tree[i][2] = cost[k][nr[k]];
minimumCost = minimumCost + cost[k][nr[k]]; nr[k] = 100.

for (j = 0; j < n; j++) {

if (nr[j] != 100 && cost[j][nr[j]] > cost[j][k]) nr[j] = k;

temp = 99;

printf("\nThe min spanning tree is:- "); for (i = 0; i < n - 1; i++) {

for (j = 0; j < 3; j++)

printf("%d", tree[i][j]);

printf("\n");

printf("\nMin cost : %d", minimumCost);

void main() {

int i, j;

clrscr();

printf("\nEnter the no. of vertices :"); scanf("%d", &n);

printf("\nEnter the costs of edges in matrix form :"); for (i = 0; i < n; i++)

for (j = 0; j < n; j++) {

scanf("%d", &cost[i][j]);
}

printf("\nThe matrix is : "); for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

printf("%d\t", cost[i][j]);

printf("\n");

prim();

getch();

RESULT:

KRUSKALS ALGORITHM:

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

int i,j,k,a,b,u,v,n,ne=1;

int min,mincost=0,cost[9][9],parent[9]; int find(int);

int uni(int,int);

void main()

{
printf("\n\tImplementation of Kruskal's Algorithm\n"); printf("\nEnter the
no. of vertices:"); scanf("%d",&n);

printf("\nEnter the cost adjacency matrix:\n"); for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

scanf("%d",&cost[i][j]); if(cost[i][j]==0)

cost[i][j]=999;

printf("The edges of Minimum Cost Spanning Tree are\n"); while(ne < n)

for(i=1,min=999;i<=n;i++)

for(j=1;j <= n;j++)

if(cost[i][j] < min)

min=cost[i][j];

a=u=i;

b=v=j;}}}
u=find(u);

v=find(v);

if(uni(u,v))

printf("%d edge (%d,%d) =%d\n",ne++,a,b,min); mincost +=min;

cost[a][b]=cost[b][a]=999;

printf("\n\tMinimum cost = %d\n",mincost); getch();

int find(int i)

while(parent[i])

i=parent[i];

return i;

int uni(int i,int j)

if(i!=j)

{
parent[j]=i;

return 1;

return 0;

RESULT:

EX NO: 6 SAVING/RETREVING NON-LINEARD DATA


STRUCTURE

IN/ FROM A FILE

AIM:

To perform saving and retrieving of non-linear data structure in or from a


file using C

program.

ALGORITHM:

Begin

Create a structure to declare variables.

Open file to write.

Check if any error occurs in file opening.

Initialize the variables with data.

If file open successfully, write struct using write method.

Close the file for writing.


Open the file to read.

Check if any error occurs in file opening.

If file open successfully, read the file using read method.

Close the file for reading.

Check if any error occurs.

Print the data.

End.

PROGRAM:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct person

int id;

char fname[20];

char lname[20];

};

int main ()

FILE *outfile;
outfile = fopen ("person.dat", "w"); if (outfile == NULL)

fprintf(stderr, "\nError opened file\n"); exit (1);

struct person input1 = {1, "rohan", "sharma"}; struct person input2 = {2,
"mahendra", "dhoni"}; fwrite (&input1, sizeof(struct person), 1, outfile);
fwrite (&input2, sizeof(struct person), 1, outfile); if(fwrite != 0)

printf("contents to file written successfully !\n"); else

printf("error writing file !\n"); fclose (outfile);

return 0;

RESULT:

EX.NO: 7 LINEAR AND BINARY SEARCH ALGORITHMS

AIM:

To perform linear and binary search algorithms using c program.

ALGORITHM:

START

Set pos to 1.

if pos> n then go to step 7.

if arr[pos] = search_value then go to step 6.

Set pos to pos + 1.


Go to Step 2.

Print the search element search_value present at index pos and then go to
step 8.

Print the search element not present in an array.

STOP

PROGRAM:

#include <stdio.h>

intLinear_search(intarr[], int n, intval)

intidx;

for (idx = 0; idx< n; idx++)

if (arr[idx] == val)

return idx;

return -1;

int main(void)

{
intarr[] = { 12, 13, 14, 20, 41, 45 }; intsearch_value = 20;

int n = sizeof(arr) / sizeof(arr[1]); int index = Linear_search(arr, n,


search_value); if(index == -1)

printf("The search element is not in array");

else

printf("The search element is found at index %d", index);

return 0;

RESULT:

BINARY SEARCH

PROGRAM:

#include <stdio.h>

int recursiveBinarySearch(int array[], int start_index, int end_index, int


element){

if (end_index >= start_index){

int middle = start_index + (end_index - start_index )/2; if (array[middle] ==


element)
return middle;

if (array[middle] > element)

return recursiveBinarySearch(array, start_index, middle-1, element); return


recursiveBinarySearch(array, middle+1, end_index, element);

return -1;

int main(void){

int array[] = {1, 4, 7, 9, 16, 56, 70}; int n = 7;

int element = 9;

int found_index = recursiveBinarySearch(array, 0, n-1, element);


if(found_index == -1 ) {

printf("Element not found in the array ");

else {

printf("Element found at index : %d",found_index);

return 0;

RESULT:

EX NO 8 :SELECTION, INSERTION, MERGE , QUICK SORT


AIM:

To perform various sorting techniques using c program.

SELECTION SORT

ALGORITHM:

START

Set MIN to location 0

Search the minimum element in the list Swap with value at location MIN

Increment MIN to point to next element Repeat until list is sorted

STOP

PROGRAM:

#include <stdio.h>

intmain()

inta[100], n, i, j, position, swap;

printf("Enter number of elementsn"); scanf("%d", &n);

printf("Enter %d Numbersn", n); for(i = 0; i < n; i++)

scanf("%d", &a[i]);

for(i = 0; i < n - 1; i++)

position=i;
for(j = i + 1; j < n; j++)

if(a[position] > a[j])

position=j;

if(position != i)

swap=a[i];

a[i]=a[position];

a[position=swap;

printf("Sorted Array:n");

for(i = 0; i < n; i++)

printf("%dn", a[i]);

return0;

RESULT:

INSERTION SORT

ALGORITHM:
START

If it is the first element, it is already sorted. return 1; Pick next element

Compare with all element, in the sorted sub-list Shift all the element in the
sorted sub-list that is greater than the value to be sorted Insert the value

Repeat until list is sorted

STOP

PROGRAM:

#include <stdio.h>

int main()

int a[100], number, i, j, temp;

printf("\n Please Enter the total Number of Elements : "); scanf("%d",


&number);

printf("\n Please Enter the Array Elements : "); for(i = 0; i < number; i++)

scanf("%d", &a[i]);

for(i = 1; i <= number - 1; i++)

for(j = i; j > 0 && a[j - 1] > a[j]; j--)

temp = a[j];

a[j] = a[j - 1];


a[j - 1] = temp;

printf("\n Insertion Sort Result : "); for(i = 0; i < number; i++)

printf(" %d \t", a[i]);

printf("\n");

return 0;

RESULT:

MERGE SORT

ALGORITHM:

START

MERGE_SORT(arr, beg, end)

if beg < end

set mid = (beg + end)/2

MERGE_SORT(arr, beg, mid)

MERGE_SORT(arr, mid + 1, end)

MERGE (arr, beg, mid, end)


end of if

END MERGE_SORT

STOP

PROGRAM:

#include <stdio.h>

void merge(int a[], int beg, int mid, int end)

int i, j, k;

int n1 = mid - beg + 1;

int n2 = end - mid;

int LeftArray[n1], RightArray[n2];

for (int i = 0; i < n1; i++)

LeftArray[i] = a[beg + i];

for (int j = 0; j < n2; j++)

RightArray[j] = a[mid + 1 + j];

i = 0;

j = 0;

k = beg;

while (i < n1 && j < n2)

{
if(LeftArray[i] <= RightArray[j])

a[k] = LeftArray[i];

i++;

else

a[k] = RightArray[j];

j++;

k++;

while (i<n1)

a[k] = LeftArray[i];

i++;

k++;

while (j<n2)

{
a[k] = RightArray[j];

j++; k++;

}}

void mergeSort(int a[], int beg, int end)

if (beg < end)

int mid = (beg + end) / 2;

mergeSort(a, beg, mid);

mergeSort(a, mid + 1, end);

merge(a, beg, mid, end);

void printArray(int a[], int n)

int i;

for (i = 0; i < n; i++)

printf("%d ", a[i]);

printf("\n");

}
int main()

int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 };

int n = sizeof(a) / sizeof(a[0]); printf("Before sorting array elements are -


\n"); printArray(a, n);

mergeSort(a, 0, n - 1);

printf("After sorting array elements are - \n"); printArray(a, n);

return 0;

RESULT:

QUICK SORT

ALGORITHM:

START

QUICKSORT (array A, start, end)

if (start < end)

p = partition(A, start, end)

QUICKSORT (A, start, p - 1)

QUICKSORT (A, p + 1, end)

STOP
PROGRAM :

#include <stdio.h>

int partition (int a[], int start, int end)

int pivot = a[end];

int i = (start - 1);

for (int j = start; j <= end - 1; j++)

if (a[j] < pivot)

i++;

int t = a[i];

a[i] = a[j];

a[j] = t;

}}

int t = a[i+1];

a[i+1] = a[end];

a[end] = t;

return (i + 1);

}
void quick(int a[], int start, int end)

if (start < end)

{ int p = partition(a, start, end);

quick(a, start, p - 1); quick(a, p + 1, end);

}}

void printArr(int a[], int n)

int i;

for (i = 0; i < n; i++) printf("%d ", a[i]);

int main()

int a[] = { 24, 9, 29, 14, 19, 27 };

int n = sizeof(a) / sizeof(a[0]);

printf("Before sorting array elements are - \n"); printArr(a, n);

quick(a, 0, n - 1);

printf("\nAfter sorting array elements are - \n"); printArr(a, n);

return 0;

RESULT :
Document Outline
SEMESTER :
ACADEMIC YEAR :
HOD
LIST OF EXPERIMENTS
EX NO: 1 TOWERS OF HANOI USING USER DEFINED
STACKS
ALGORITHM:
PROGRAM:
RESULT:
EX NO: 2 READING, WRITING AND ADDITION OF
POLYNOMIALS
ALGORITHM: (1)
PROGRAM: (1)
RESULT: (1)
EX NO: 3 LINE EDITORS WITH LINE COUNT, WORD COUNT
SHOWING ON THE SCREEN
AIM:
ALGORITHM:
PROGRAM:
RESULT:
EX NO: 4 TREES WITH ALL OPERATIONS
ALGORITHM:
PROGRAM:
RESULT:
1. AVL TREE
RESULT: (1)
1. SPLAY TREE
RESULT:
EX NO:5 ALL GRAPH ALGORITHMS
PROGRAM:
RESULT: (1)
KRUSKALS ALGORITHM:
RESULT: (2)
EX NO: 6 SAVING/RETREVING NON-LINEARD DATA
STRUCTURE IN/ FROM A FILE
ALGORITHM:
PROGRAM:
RESULT:
EX.NO: 7 LINEAR AND BINARY SEARCH ALGORITHMS
ALGORITHM: (1)
PROGRAM:
RESULT:
BINARY SEARCH
RESULT: (1)
EX NO 8 :SELECTION, INSERTION, MERGE , QUICK SORT
SELECTION SORT
PROGRAM: (1)
RESULT:
INSERTION SORT
PROGRAM: (2)
RESULT:
MERGE SORT
PROGRAM:
RESULT: (1)
QUICK SORT
PROGRAM :
RESULT :

You might also like