Professional Documents
Culture Documents
COURSE CODE
: 18CBC101J
COURSE NAME
: DATASTRUCTURES &ALGORITHMS
SEMESTER
: II
BRANCH
: CSBS
ACADEMIC YEAR
: 2022 - 2023
STAFF INCHARGE
HOD
AIM:
ALGORITHM:
START
ELSE
END IF
ENDProcedure
STOP
PROGRAM:
#include<stdio.h>
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:
ALGORITHM:
START
Create a sum array sum[] of size equal to maximum of 'm' and 'n'
Return sum[].
STOP
PROGRAM:
#include<stdio.h>
struct poly
int coeff;
int expo;
};
int t1,t2,t3;
t1=readPoly(p1);
t2=readPoly(p2);
t3=addPoly(p1,p2,t1,t2,p3);
printf("\n");
return 0;
int t1,i;
{
printf(" Enter the Coefficient(%d): ",i+1); scanf("%d",&p[i].coeff); printf("
return(t1);
int i,j,k;
i=0;
j=0;
k=0;
if(p1[i].expo==p2[j].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); }
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:
AIM:
To perform line editors with line count, word count showing on the screen.
ALGORITHM:
START
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
char str[100];
int i = 0, l = 0, f = 1;
clrscr();
l = l + 1;
}
printf("The number of characters in the string are %d\n", l); for(i = 0; i <= l-
1; i++)
f = f + 1;
RESULT:
EX NO: 4
AIM:
To perform all the basic operations in B tree, B+ tree , AVL tree and splay
tree.
ALGORITHM:
START
If root is NULL
return
If root exists then
while until insertion position is locate If data is greater than node. data
else
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 {
};
struct btreeNode *root; struct btreeNode * createNode(int val, struct
btreeNode *child) {
newNode->count = 1;
newNode->link[0] = root;
newNode->link[1] = child;
return newNode;
int j = node->count;
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;
median = MIN + 1;
else
median = MIN;
node->count = median;
} else {
*pval = node->val[node->count];
}
/* sets the value val in the node */
int pos;
if (!node) {
*pval = val;
*child = NULL;
return 1;
pos = 0;
} else {
(val < node->val[pos] && pos > 1); pos--); if (val == node->val[pos]) {
} else {
splitNode(*pval, pval, pos, node, *child, child); return 1;
return 0;
int flag, i;
dummy = myNode->link[pos];
dummy = dummy->link[0];
myNode->val[pos] = dummy->val[1];
int i = pos + 1;
while (i <= myNode->count) {
myNode->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];
return;
myNode->val[pos] = x->val[1];
x->link[0] = x->link[1];
x- >count--;
j++;
return;
int j = 1;
x2->count++;
x2->count++;
x2->val[x2->count] = x1->val[j]; x2->link[x2->count] = x1->link[j]; j++;
j = pos;
myNode->count--;
free(x1);
if (!pos) {
doLeftShift(myNode, 1);
} else {
mergeNodes(myNode, 1);
} else {
if (myNode->count != pos) {
doRightShift(myNode, pos);
} else {
} else {
mergeNodes(myNode, pos);
} else {
else
mergeNodes(myNode, pos);
if (myNode) {
pos = 0;
flag = 0;
} else {
(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);
} else {
removeVal(myNode, pos);
} else {
if (myNode->link[pos]) {
return flag;
if (!delValFromNode(val, myNode)) {
} else {
if (myNode->count == 0) {
tmp = myNode;
myNode = myNode->link[0];
free(tmp);
root = myNode;
return;
}
if (!myNode) {
return;
*pos = 0;
} else {
(val < myNode->val[*pos] && *pos > 1); (*pos)--); if (val == myNode-
>val[*pos]) {
int i;
if (myNode) {
traversal(myNode->link[i]);
printf("%d ", myNode->val[i + 1]);
traversal(myNode->link[i]);
int main() {
while (1) {
switch (ch) {
case 1:
insertion(val);
break;
case 2:
deletion(val, root);
break;
case 3:
break;
case 4:
traversal(root);
break;
case 5:
exit(0);
default:
break;
printf("\n");
RESULT:
1. AVL TREE
#include <stdio.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
struct node
int info;
int balance;
};
int main()
int choice,key;
while(1)
{
printf("\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Delete\n");
switch(choice)
case 1:
root = insert(root,key);
break;
case 2:
printf("\n");
display(root,0);
printf("\n");
break;
case 3:
root = del(root,key);
break;
case 4:
inorder(root);
break;
case 5:
exit(1);
default:
printf("Wrong choice\n");
return 0;
int i;
return;
else
display(ptr->rchild, level+1);
printf("\n");
for (i=0; i<level; i++)
printf(" ");
if(pptr==NULL)
pptr->lchild = NULL;
pptr->rchild = NULL;
pptr->balance = 0;
taller = TRUE;
if(taller==TRUE)
else
printf("Duplicate key\n");
taller = FALSE;
return pptr;
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;
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;
aptr = pptr->lchild;
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;
}
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;
aptr = pptr->rchild;
pptr->rchild = aptr->lchild;
aptr->lchild = pptr;
return aptr;
aptr = pptr->lchild;
pptr->lchild = aptr->rchild;
aptr->rchild = pptr;
return aptr;
return(pptr);
{
pptr->lchild = del(pptr->lchild, dkey); if(shorter == TRUE)
else
succ = pptr->rchild;
while(succ->lchild)
succ = succ->lchild;
pptr->info = succ->info;
else
{
tmp = pptr;
pptr = pptr->lchild;
pptr = pptr->rchild;
else /* no children */
pptr = NULL;
free(tmp);
shorter = TRUE;
return pptr;
switch(pptr->balance)
case 0:
pptr->balance = -1;
*pshorter = FALSE;
break;
case 1: pptr->balance = 0;
break;
return pptr;
switch(pptr->balance)
case 0:
pptr->balance = 1;
*pshorter = FALSE;
break;
case -1:
pptr->balance = 0;
break
return pptr;
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;
aptr->balance = 1;
bptr->balance = 0;
pptr->lchild = RotateLeft(aptr);
pptr = RotateRight(pptr);
}
return pptr;
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;
aptr->balance = 0;
bptr->balance = 0;
pptr->rchild = RotateRight(aptr);
pptr = RotateLeft(pptr);
return pptr;
}
if(ptr!=NULL)
inorder(ptr->lchild);
RESULT:
1. SPLAY TREE
#include<stdio.h>
#include<stdlib.h>
struct node
int key;
};
x->left = y->right;
y->right = x;
return y;
x->right = y->left;
y->left = x;
return y;
root = rightRotate(root);
root->left = leftRotate(root->left);
else
root->right = rightRotate(root->right);
}
root->right->right = splay(root->right-
if (root != NULL)
preOrder(root->right);
}
}
int main()
root->left = newNode(50);
root->right = newNode(200);
return 0;
RESULT:
EX NO:5
AIM:
ALGORITHM:
START
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() {
temp = cost[i][j];
startVertex = i;
endVertex = j;
tree[0][0] = startVertex;
tree[0][1] = endVertex;
tree[0][2] = temp;
minimumCost = temp;
else
nr[i] = endVertex;
nr[startVertex] = 100;
nr[endVertex] = 100;
temp = 99;
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.
temp = 99;
printf("%d", tree[i][j]);
printf("\n");
void main() {
int i, j;
clrscr();
printf("\nEnter the costs of edges in matrix form :"); for (i = 0; i < n; i++)
scanf("%d", &cost[i][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 uni(int,int);
void main()
{
printf("\n\tImplementation of Kruskal's Algorithm\n"); printf("\nEnter the
no. of vertices:"); scanf("%d",&n);
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]); if(cost[i][j]==0)
cost[i][j]=999;
for(i=1,min=999;i<=n;i++)
min=cost[i][j];
a=u=i;
b=v=j;}}}
u=find(u);
v=find(v);
if(uni(u,v))
cost[a][b]=cost[b][a]=999;
int find(int i)
while(parent[i])
i=parent[i];
return i;
if(i!=j)
{
parent[j]=i;
return 1;
return 0;
RESULT:
AIM:
program.
ALGORITHM:
Begin
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)
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)
return 0;
RESULT:
AIM:
ALGORITHM:
START
Set pos to 1.
Print the search element search_value present at index pos and then go to
step 8.
STOP
PROGRAM:
#include <stdio.h>
intidx;
if (arr[idx] == val)
return idx;
return -1;
int main(void)
{
intarr[] = { 12, 13, 14, 20, 41, 45 }; intsearch_value = 20;
else
return 0;
RESULT:
BINARY SEARCH
PROGRAM:
#include <stdio.h>
return -1;
int main(void){
int element = 9;
else {
return 0;
RESULT:
SELECTION SORT
ALGORITHM:
START
Search the minimum element in the list Swap with value at location MIN
STOP
PROGRAM:
#include <stdio.h>
intmain()
scanf("%d", &a[i]);
position=i;
for(j = i + 1; j < n; j++)
position=j;
if(position != i)
swap=a[i];
a[i]=a[position];
a[position=swap;
printf("Sorted Array:n");
printf("%dn", a[i]);
return0;
RESULT:
INSERTION SORT
ALGORITHM:
START
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
STOP
PROGRAM:
#include <stdio.h>
int main()
printf("\n Please Enter the Array Elements : "); for(i = 0; i < number; i++)
scanf("%d", &a[i]);
temp = a[j];
printf("\n");
return 0;
RESULT:
MERGE SORT
ALGORITHM:
START
END MERGE_SORT
STOP
PROGRAM:
#include <stdio.h>
int i, j, k;
i = 0;
j = 0;
k = beg;
{
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++;
}}
int i;
printf("\n");
}
int main()
mergeSort(a, 0, n - 1);
return 0;
RESULT:
QUICK SORT
ALGORITHM:
START
STOP
PROGRAM :
#include <stdio.h>
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)
}}
int i;
int main()
quick(a, 0, n - 1);
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 :