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

dsa lab program 3a

The document describes the implementation of three C programs: one for operations on a Binary Search Tree (BST) including creation, traversal, searching, and deletion; another for operations on a Singly Circular Linked List (SCLL) to represent and evaluate polynomials and find their sum; and a third for string manipulation to find and replace occurrences of a pattern in a main string. Each program includes relevant functions and example outputs demonstrating their functionality. The programs aim to provide practical applications of data structures and algorithms in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

dsa lab program 3a

The document describes the implementation of three C programs: one for operations on a Binary Search Tree (BST) including creation, traversal, searching, and deletion; another for operations on a Singly Circular Linked List (SCLL) to represent and evaluate polynomials and find their sum; and a third for string manipulation to find and replace occurrences of a pattern in a main string. Each program includes relevant functions and example outputs demonstrating their functionality. The programs aim to provide practical applications of data structures and algorithms in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Program 10:

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;

void inorder(NODE *ptr)


{
if(ptr)
{
inorder(ptr->llink);
printf("%d",ptr->info);
inorder(ptr->rlink);
}
}

void preorder(NODE *ptr)


{
if(ptr)
{
printf("%d",ptr->info);
preorder(ptr->llink);
preorder(ptr->rlink);
}
}
void postorder(NODE *ptr)
{
if(ptr)
{
postorder(ptr->llink);
postorder(ptr->rlink);
printf("%d",ptr->info);
}
}
void create()
{
NODE *nn,*prev,*temp;
int i,n;

printf("enter the value of n\n");


scanf("%d",&n);

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>

typedef struct node


{
int cf;
int xex,yex,zex;
struct node *link;
}NODE;

NODE *p3=NULL,*p3head=NULL;

NODE* insert(NODE *head, NODE *last, int n)


{
int i;
NODE *nn;
if(head==NULL)
{
nn=(NODE*)malloc(sizeof(NODE));
head=nn;
last=nn;
nn->link=nn;
}

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

void display(NODE* last)


{
NODE* temp=last->link->link;

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

void add(NODE *p1,NODE *p2,int n1,int n2)


{
NODE *nn=NULL,*a,*b;
int i=0,j=0,comp;
a=p1->link->link;
b =p2->link->link;
nn=(NODE*)malloc(sizeof(NODE));
p3head=nn;
p3=nn;
nn->link=nn;

while(i<n1 && j<n2)


{
nn=(NODE*)malloc(sizeof(NODE));
nn->link=p3head;
p3->link=nn;
p3=nn;
if(a->xex == b->xex )
{ nn->cf = a->cf + b->cf;
nn->xex=a->xex;
nn->yex=a->yex;
nn->zex=a->zex;
a=a->link;
b=b->link;
i++;j++;
}
else if(a->xex > b->xex )
{ nn->cf=a->cf;
nn->xex=a->xex;
nn->yex=a->yex;
nn->zex=a->zex;
a=a->link;
i++;
}
else
{ nn->cf=b->cf;
nn->xex=b->xex;
nn->yex=b->yex;
nn->zex=b->zex;
b=b->link;
j++;
}
}
while(i<n1)
{
nn=(NODE*)malloc(sizeof(NODE));
nn->link=p3head;
p3->link=nn;
p3=nn;
nn->cf=a->cf;
nn->xex=a->xex;
nn->yex=a->yex;
nn->zex=a->zex;
a=a->link;
i++;
}
while(j<n2)
{
nn=(NODE*)malloc(sizeof(NODE));
nn->link=p3head;
p3->link=nn;
p3=nn;
nn->cf=b->cf;
nn->xex=b->xex;
nn->yex=b->yex;
nn->zex=b->zex;
j++;
b=b->link;
}
}
void eval(NODE *p)
{
int x,y,z,res=0,xval,yval,zval;
NODE *temp;
temp=p->link->link;
printf("\n enter the value of x,y and z\n");
scanf("%d%d%d",&x,&y,&z);
while(temp!=p)
{
xval= pow(x,temp->xex);
yval= pow(y,temp->yex);
zval= pow(z,temp->zex);
res= res+temp->cf*xval*yval*zval;
temp=temp->link;
}
xval= pow(x,temp->xex);
yval= pow(y,temp->yex);
zval= pow(z,temp->zex);
res= res+temp->cf*xval*yval*zval;
printf("\n the result of the polynomial is %d",res);
}
void main()
{
int n1,n2;
NODE *head1=NULL,*head2=NULL,*p1=NULL,*p2=NULL;
printf("\n enter the number of terms of first polynomial \n");
scanf("%d",&n1);
p1=insert(head1,p1,n1);
eval(p1);
printf("\ enter the number of terms of second polynomial\n");
scanf("%d",&n2);
p2=insert(head2,p2,n2);
eval(p2);
add(p1,p2,n1,n2);
printf("\n the first polynomial is \n");
display(p1);
printf("\n the second polynomial is \n");
display(p2);
printf("\n the resultant polynomial is \n");
display(p3);
}

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

You might also like