Design, develop and implement a program in C for the following operations on SINGLY CIRCULAR LINKED LIST(SCLL) with the header nodes:

Design, develop and implement a program in C for the following operations on SINGLY CIRCULAR LINKED LIST(SCLL) with the header nodes:

Design, develop and implement a program in C for the following operations on SINGLY CIRCULAR LINKED LIST(SCLL) with the header nodes:

(i) Represent and evaluate a polynomial p(x,y,z)=6×2 y2 z2 – 4yz5 + 3×3 yz+2xy5 z-2xyz3

(ii) 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:

Program:-

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
struct poly
{
int coef, expo1,expo2,expo3, flag;
struct poly *next;
};
typedef struct poly node;
void insert_end(node *h, int a, int x, int y, int z)
{
node *temp = h->next, *n;
n = (node*) malloc(sizeof(node));
n->coef = a;
n->expo1 = x;
n->expo2 = y;
n->expo3 = z;
n->flag= 0;
while(temp->next != h)
temp = temp -> next;
temp -> next = n;
n -> next = h;
}
void read_poly(node *head)
{
int a,x,y,z;
char ch;
do
{
printf("\nenter coef & expo1, expo2, expo3\n");
scanf("%d%d%d%d",&a,&x,&y,&z);
insert_end(head,a,x,y,z);
printf("do u want to continue(Y/N) ?");
ch=getche();
}while(ch == 'Y' || ch == 'y');
}
void add_poly(node *h1,node *h2,node *h3)
{
node *p1=h1->next, *p2;
int x;
while( p1 != h1)
{
p2=h2->next;
while(p2 != h2)
{
if( p1->expo1 == p2->expo1 && p1->expo2 == p2->expo2 && p1->expo3 == p2->expo3 && p2-> flag == 0 )
{
x = p1->coef + p2->coef;
insert_end(h3,x,p1->expo1,p1->expo2,p1->expo3);
p1->flag= p2->flag = 1;
}
p2 = p2->next;
}
p1 = p1 -> next;
}
p1=h1->next;
p2=h2->next;
while(p1 != h1)
{
if( p1->flag !=1)
insert_end(h3, p1->coef, p1->expo1, p1->expo2, p1->expo3);
p1 = p1->next;
}
while(p2 != h2)
{
if( p2->flag !=1)
insert_end(h3,p2->coef,p2->expo1, p2->expo2, p2->expo3);
p2 = p2->next;
}
}
void display(node *h)
{
node *temp = h->next;
if(temp == h)
{
printf("list empty\n");
return;
}
while(temp != h)
{
printf(" %+ dx^%dy^%dz^%d ",temp->coef,temp->expo1,temp->expo2,temp->expo3);
temp=temp->next;
}
}
void evaluate(node *h)
{
int x,y,z,sum=0;
node *temp = h->next;
printf("\nEvauate the resultant polynomial by giving values for X, Y and Z");
scanf("%d%d%d",&x,&y,&z);
while(temp != h)
{
sum = sum + temp->coef * pow(x,temp->expo1) * pow(y,temp->expo2) * pow(z,temp->expo3);
temp = temp->next;
}
printf("\nSum = %d",sum);
}
main()
{
node *h1,*h2,*h3;
h1 = (node*) malloc(sizeof(node));
h1->next = h1;
h2 = (node*) malloc(sizeof(node));
h2->next = h2;
h3 = (node*) malloc(sizeof(node));
h3->next = h3;
printf("\nenter the first poly");
read_poly(h1);
printf("\nenter the second poly");
read_poly(h2);
add_poly(h1,h2,h3);
printf("\nTHE FIRST POLY IS\n");
display(h1);
printf("\nTHE SEC POLY IS\n");
display(h2);
printf("\nADDition of TWO poly are\n");
display(h3);
evaluate(h3);
getch();
}

Click to rate this post!
[Total: 0 Average: 0]

We will be happy to hear your thoughts

Leave a reply

EducationTrick
Logo
Enable registration in settings - general