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

Assignment No.10: Roll No 55 Batch: - S3

The document describes a C program that implements polynomial operations using linked lists. It defines functions to create nodes, create polynomials from user input, display polynomials, evaluate polynomials at a given value, multiply two polynomials by iterating through them and adding terms, and add two polynomials by iterating through them and adding or carrying over terms based on the exponents. The main function allows the user to select these operations and displays the results, such as displaying the added polynomial 6x^2 + 5x^1 + 4x^0 + 4x^0.

Uploaded by

EYE MAKE
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Assignment No.10: Roll No 55 Batch: - S3

The document describes a C program that implements polynomial operations using linked lists. It defines functions to create nodes, create polynomials from user input, display polynomials, evaluate polynomials at a given value, multiply two polynomials by iterating through them and adding terms, and add two polynomials by iterating through them and adding or carrying over terms based on the exponents. The main function allows the user to select these operations and displays the results, such as displaying the added polynomial 6x^2 + 5x^1 + 4x^0 + 4x^0.

Uploaded by

EYE MAKE
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment No.

10
Implement polynomial using CLL and perform
i. Addition of polynomials
ii. Multiplication of polynomials
iii. Evaluation of polynomails
ROLL NO 55
Batch: - S3

________________________________________________________________________
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct poly


{
int c;
int e;
struct poly *link;
} POLY;
// create node
POLY* createnewnode()
{
POLY *nn=NULL;
nn=(POLY*)malloc(sizeof(POLY));
if(nn==NULL)
{
printf("\n Insufficient memory ! ");
exit(0);
}
return(nn);
}//end createnewnode

//Function Use- to create no of node

POLY* create()
{
POLY *hn=NULL,*nn=NULL,*cn=NULL;
int i,n;
printf("\n Enter how many terms- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
nn=createnewnode();
printf("\n Enter coefficient & exponent - ");
scanf("%d %d",&nn->c,&nn->e);
if(hn==NULL)
{
hn=nn;
cn=nn;
}
else
{
cn->link=nn;
cn=nn;
}
nn->link=hn;
}
return(hn);
}//end createpoly

//Function Name-display

void display(POLY *hn)


{
POLY *cn=NULL;
cn=hn;
printf("\n entered polynomial is - ");
do
{
printf(" %dx^%d ",cn->c,cn->e);
if(cn->link != hn)
{
printf(" + ");
}
cn=cn->link;
}
while(cn!=hn);
}//end display

// Function Name- evaluate

void evaluate(POLY *hn)


{
POLY *cn=hn;
int x ,sum =0;
printf("\n Enter value of x - ");
scanf("%d",&x);
do
{
sum=sum+(cn->c*pow(x,cn->e));
cn=cn->link;
}
while(cn!=hn);
printf("\n Evaluation is- %d",sum);
}//end evaluate

//Function Name- sameexpo

POLY* sameexpo(int c,int e,POLY *h3)


{
POLY *cn=NULL;
cn=h3;
if(h3==NULL)
{
return(NULL);
}

do
{
if(e==cn->e)
{
cn->c=c+cn->c;
return(cn);
}
cn=cn->link;
}while(cn!=h3);
return(NULL);

}//end sameexpo

//Function Name- addlastnade

POLY* addlastnode(POLY *nn,POLY *h3,POLY *last)


{
if(h3==NULL)
{
h3=nn;

}
else
{
last->link=nn;

}
nn->link=h3;
return(h3);
}//end addlast
//Function Name- multipoly

POLY* multipoly(POLY *h1,POLY *h2)


{
POLY *p=NULL,*q=NULL,*nn=NULL;
int c=0,e=0;
POLY *h3=NULL;
POLY *last=NULL;
p=h1;
q=h2;
do
{
do
{
c=p->c*q->c;
e=p->e+q->e;
nn=sameexpo(c,e,h3);
if(nn!=NULL)
{
nn->c=nn->c +c;
}
else
{
nn=createnewnode();
h3=addlastnode(nn,h3,last);
last=nn;
nn->c=c;
nn->e=e;
}
q=q->link;
} while(q!=h2);
p=p->link;
} while(p!=h1);
return(h3);
}//end multipoly

//Function Name - addpoly

POLY *addpoly(POLY *h1, POLY *h2)


{
POLY *p=NULL,*q=NULL,*nn=NULL;
int c,e;
POLY *h3=NULL;
POLY *last=NULL;
p=h1;
q=h2;
do
{
nn=createnewnode();
h3=addlastnode(nn,h3,last);
last=nn;
if(p->e==q->e)
{
nn->c=p->c+q->c;
nn->e=p->e;
p=p->link;
q=q->link;
}
else if(p->e > q->e)
{
nn->c=p->c;
nn->e=p->e;
p=p->link;
}
else
{
nn->c=q->c;
nn->e=q->e;
q=q->link;
}
} while(p->link!=h1 && q->link!=h2);

//end while

while(p!=h1)
{ nn= createnewnode();
h3=addlastnode(nn,h3,last);
last=nn;
nn->c=p->c;
nn->e=p->e;
p=p->link;
}
while(q!=h2)
{
nn=createnewnode();
h3=addlastnode(nn,h3,last);
last=nn;
nn->c=q->c;
nn->e=q->e;
q=q->link;
}
return(h3);
}//end add poly
int main()
{
POLY *hn=NULL, *h1=NULL, *h2=NULL;
int ch;
printf("\n---------------POLYNOMIAL OPERATION---------------------");
do
{
printf("\n\n 1.CREATE \n 2.DISPLAY \n 3.EVALUATE \n 4.MULTIPLY \n 5.ADD \n 6.EXIT");
printf("\n Enter YOUR Choice- ");
scanf("%d",&ch);
switch(ch)
{
case 1:hn=create();
break;
case 2:display(hn);
break;
case 3:evaluate(hn);
break;
case 4:h1=create();
h2=create();
hn=multipoly(h1,h2);
display(hn);
break;
case 5:h1=create();
h2=create();
hn=addpoly(h1,h2);
display(hn);
break;
case 6:exit(0);;
}//end switch
}//end do
while(ch!=6);
}//end main
OUTPUT :

---------------POLYNOMIAL OPERATION---------------------

1.CREATE
2.DISPLAY
3.EVALUATE
4.MULTIPLY
5.ADD
6.EXIT
Enter YOUR Choice- 1
Enter how many terms- 3

Enter coefficient & exponent - 3 2

Enter coefficient & exponent - 2 1

Enter coefficient & exponent - 5 0

1.CREATE
2.DISPLAY
3.EVALUATE
4.MULTIPLY
5.ADD
6.EXIT
Enter YOUR Choice- 2

entered polynomial is - 3x^2 + 2x^1 + 5x^0

1.CREATE
2.DISPLAY
3.EVALUATE
4.MULTIPLY
5.ADD
6.EXIT
Enter YOUR Choice- 3

Enter value of x - 2

Evaluation is- 21

1.CREATE
2.DISPLAY
3.EVALUATE
4.MULTIPLY
5.ADD
6.EXIT
Enter YOUR Choice- 5

Enter how many terms- 3

Enter coefficient & exponent - 4 2

Enter coefficient & exponent - 3 1

Enter coefficient & exponent - 4 0


Enter how many terms- 3

Enter coefficient & exponent - 2 2

Enter coefficient & exponent - 2 1

Enter coefficient & exponent - 4 0

entered polynomial is - 6x^2 + 5x^1 + 4x^0 + 4x^0

You might also like