0% found this document useful (0 votes)
22 views8 pages

9 PolynomialAddition

The document outlines a C program for performing operations on a Singly Circular Linked List (SCLL) representing polynomials. It includes functions for evaluating a specific polynomial, adding two polynomials, and displaying the result. The program features a menu-driven interface for user interaction to perform these operations.

Uploaded by

raghavlanger663
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

9 PolynomialAddition

The document outlines a C program for performing operations on a Singly Circular Linked List (SCLL) representing polynomials. It includes functions for evaluating a specific polynomial, adding two polynomials, and displaying the result. The program features a menu-driven interface for user interaction to perform these operations.

Uploaded by

raghavlanger663
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

/* Lab 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 2 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.

*/

//Header files

#include<stdlib.h>

#include<stdio.h>

#include<malloc.h>

#include<math.h>

#include<ctype.h>

struct polylist

int coef;

int x,y,z;

float sal;

struct polylist *link;

};

typedef struct polylist *NODE;

#define MALLOC(p,s,t)\

p=(t)malloc(s);\

if(p==NULL)\

{\

printf("insufficient memeory\n"); \
exit;\

//Prototypes

NODE readPoly(NODE);

NODE padd(NODE a,NODE b);

float evalPoly();

NODE attach(int, int, int, int,NODE);

void printPoly(NODE);

//main function

void main()

NODE p1,p2,p3;

int done=0,choice;

float result;

int expx,expy,expz;

while(!done)

printf("1.Polynomial Evaluation\t2.Add Polynomial\t3.Exit\n");

printf("Enter your choice\n");

scanf("%d",&choice);

switch(choice)

case 1: result=evalPoly();

printf("The Polynomial Result=%8.2f\n",result);

break;

case 2: MALLOC(p1,sizeof(struct polylist),NODE);

MALLOC(p2,sizeof(struct polylist),NODE);

p1->link=p1;

p2->link=p2;
printf("Enter polynomial a \n");

p1=readPoly(p1);

printf("Enter polynomial b\n");

p2=readPoly(p2);

p3=padd(p1,p2);

printf("Resultant polynomial c\n");

printPoly(p3);

break;

case 3: done=1;

break;

default:printf("Invalid Choice\n");

break;

// To read the polynomial- coeff, expx, expy, expz

NODE readPoly(NODE p)

int ce, expx, expy,expz;

while(1)

printf("Enter the coeff and expx, expy, expz\n");

scanf("%d%d%d%d", &ce, &expx, &expy, &expz);

if(expx==-1)

break;

p=attach(ce,expx,expy,expz,p);

}
return p;

//To add the new polynomial

NODE padd(NODE p1, NODE p2)

NODE p3;

NODE starta=p1;

NODE startb=p2;

int sum;

MALLOC(p3,sizeof(struct polylist),NODE);

p3->link=p3;

p1=p1->link;

p2=p2->link;

while(p1!=starta && p2!= startb)

if(p1->x==p2->x && p1->y==p2->y && p1->z==p2->z)

sum=p1->coef+p2->coef;

if(sum)

p3=attach(sum,p1->x,p1->y,p1->z,p3);

p1=p1->link;

p2=p2->link;

else if(p1->x<p2->x)

p3=attach(p2->coef,p2->x,p2->y,p2->z,p3);

p2=p2->link;

else if(p1->x>p2->x)
{

p3=attach(p1->coef,p1->x,p1->y,p1->z,p3);

p1=p1->link;

else if(p1->y<p2->y)

p3=attach(p2->coef,p2->x,p2->y,p2->z,p3);

p2=p2->link;

else if(p1->y>p2->y)

p3=attach(p1->coef,p1->x,p1->y,p1->z,p3);

p1=p1->link;

else if(p1->z<p2->z)

p3=attach(p2->coef, p2->x,p2->y,p2->z,p3);

p2=p2->link;

else if(p1->z>p2->z)

p3=attach(p1->coef,p1->x,p1->y,p1->z,p3);

p1=p1->link;

for(;p1!=starta;p1=p1->link)

p3=attach(p1->coef,p1->x,p1->y,p1->z,p3);

for(;p2!=startb;p2=p2->link)
p3=attach(p2->coef,p2->x,p2->y,p2->z,p3);

return p3;

//To attach the polynomial

NODE attach(int c, int expx, int expy, int expz, NODE header)

NODE temp,q;

MALLOC(temp,sizeof(struct polylist),NODE);

temp->coef=c;

temp->x=expx;

temp->y=expy;

temp->z=expz;

q=header->link;

while(q->link!=header)

q=q->link;

q->link=temp;

temp->link=header;

return header;

// To evaluate the polunomial

float evalPoly()

float sum=0;

NODE p, start;

float xval,yval,zval;

int k,n=20;

int terms[20]={6,2,2,1,-4,0,1,5,3,3,1,1,2,1,5,1,-2,1,1,3};
MALLOC(p,sizeof(struct polylist),NODE);

p->link=p;

printf("Enter values for x,y,z\n");

scanf("%f%f%f", &xval, &yval, &zval);

for(k=0;k<n;k+=4)

p=attach(terms[k],terms[k+1],terms[k+2],terms[k+3],p);

start=p;

p=p->link;

do

sum+=p->coef*pow(xval,p->x)*pow(yval,p->y)*pow(zval,p->z);

p=p->link;

while(p!=start);

return sum;

//To display the polynomial

void printPoly(NODE header)

NODE q;

q=header->link;

do

printf("%dx^%dy^%dz^%d+", q->coef,q->x,q->y,q->z);

q=q->link;

while(q!=header);
printf("\n");

You might also like