dsa lab program 3a
dsa lab program 3a
Design, Develop and Implement a menu driven Program in C for the following operations on Binary
Search Tree (BST) of Integers
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report appropriate message
d. Delete an element(ELEM) from BST
e. Exit
#include<stdio.h>
typedef struct node
{
int info;
struct node *llink;
struct node *rlink;
}NODE;
NODE *root=NULL;
for(i=1;i<=n;i++)
{
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the info\n");
scanf("%d",&nn->info);
nn->llink=NULL;
nn->rlink=NULL;
if(root==NULL)
root=nn;
else
{
temp=root;
while(temp!=NULL)
{
prev=temp;
if (nn->info>temp->info)
temp=temp->rlink;
else
temp=temp->llink;
}
if(nn->info>prev->info)
prev->rlink=nn;
else
prev->llink=nn;
}
}
}
NODE * search(NODE *temp,int key)
{
if(temp==NULL)
return NULL;
else if(key>temp->info)
return search(temp->rlink,key);
else if(key<temp->info)
return search(temp->llink,key);
else
return temp;
}
void main()
{
int ch,key;
NODE *f;
do
{
printf("1:create 2:inorder 3:preorder 4:postorder 5:search\n");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2: inorder(root);
break;
case 3: preorder(root);
break;
case 4: postorder(root);
break;
case 5: printf("enter the key\n");
scanf("%d",&key);
f=search(root,key);
if(f==NULL)
printf("key not found\n");
else
printf("key found");
}
}while(ch<=5);
}
Program 9:
Design, Develop and Implement a Program in C for the following operations on Singly
Circular Linked List (SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the
result in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<math.h>
NODE *p3=NULL,*p3head=NULL;
for(i=1;i<=n;i++)
{
nn=(NODE*)malloc(sizeof(NODE));
printf("enter the coefficient \n");
scanf("%d",&nn->cf);
printf("enter the exponent values for the x, y and z \n");
scanf("%d%d%d",&nn->xex,&nn->yex,&nn->zex);
if(head->link==head)
{
nn->link=head;
head->link=nn;
last=nn;
}
else
{
nn->link=head;
last->link=nn;
last=nn;
}
}
return last;
}
if(temp==NULL)
{
printf("LIST is empty\n");
return;
}
while(temp!=last)
{
printf("(%dx^%dy^%dz^%d)+",temp->cf,temp->xex,temp->yex,temp->zex);
temp=temp->link;
}
printf("(%dx^%dy^%dz^%d)",temp->cf,temp->xex,temp->yex,temp->zex);
}
OUTPUT:
enter the number of terms of first polynomial
2
Enter the coefficient
5
enter the exponent values for the x, y and z
444
Enter the coefficient
-3
enter the exponent values for the x, y and z
222
enter the value of x,y and z
111
The result of polynomial is 42
enter the number of terms of second polynomial
3
Enter the coefficient
8
enter the exponent values for the x, y and z
444
Enter the coefficient
9
enter the exponent values for the x, y and z
333
Enter the coefficient
6
enter the exponent values for the x, y and z
111
enter the value of x,y and z
111
The result of polynomial is 23
The first polynomial is
(5 x^4 y^4 z^4)+(-3 x^2 y^2 z^2)
The second polynomial is
(8x^4 y^4 z^4)+(9 x^3 y^3 z^3)+ (6 x^1 y^1 z^1)
The resultant polynomial is
(13 x^4 y^4 z^4)+(9 x^3 y^3 z^3)+ (-3 x^2 y^2 z^2)+ (6 x^1 y^1 z^1)
Program 2.
Design, Develop and Implement a Program in C for the following operations on Strings
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in
STR with REP if PAT exists in STR. Report suitable messages in case PAT does not
exist in STR. Support the program with functions for each of the above operations.
Don't use Built-in functions
#include<stdio.h>
#define MAX 30
void str(char s[],char p[],char r[],char f[])
{
int i,j,k,m,t,found;
i=j=k=m=t=0,found=0;
while(s[i]!='\0')
{
if(s[m++]= =p[j++])
{
if(p[j]= ='\0')
{
for(k=0;r[k]!='\0';k++,t++)
f[t]=r[k];
j=0;
i=m;
found=1;
}
}
else
{
f[t++]=s[i++];
m=i;
j=0;
}
}
f[t]!='\0';
if(found==1)
{
printf("resultant string is");
puts(f);
}
else
printf("pattern not found in main string\n");
}
void main()
{
char s[MAX], p[MAX],r[MAX],f[MAX];
printf("enter the main string\n");
gets(s);
printf("enter the pattern string\n");
gets(p);
printf("enter the replace string\n");
gets(r);
str(s,p,r,f);
}
OUTPUT:
enter the main string
param
enter the pattern string
am
enter the replace string
is
resultant string is paris
enter the main string
param
enter the pattern string
cm
enter the replace string
is
pattern not found in main string