0% found this document useful (0 votes)
17 views2 pages

Pratical 15

The document contains a C program that defines a linked list structure for representing polynomials. It includes functions to create nodes, add nodes to a polynomial, display polynomials, and add two polynomials together. The main function demonstrates the creation of two polynomials, displays them, and shows the result of their addition.

Uploaded by

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

Pratical 15

The document contains a C program that defines a linked list structure for representing polynomials. It includes functions to create nodes, add nodes to a polynomial, display polynomials, and add two polynomials together. The main function demonstrates the creation of two polynomials, displays them, and shows the result of their addition.

Uploaded by

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

**************************pratical:-15*************************

#include<stdio.h>
//#include<conio.h>
#include<stdlib.h>
struct node
{
int coeffi,pow;
struct node* next;
};
struct node*createnode(coeffi,pow)
{
struct node*newnode=(struct node *)malloc(sizeof(struct node));
newnode->coeffi=coeffi;
newnode->pow=pow;
newnode->next=NULL;
return newnode;
}
void addnode(struct node**poly,int coeffi,int pow)
{
struct node* newnode=createnode(coeffi,pow);;
if(*poly==NULL)
{
*poly=newnode;
}
else
{
struct node * temp=*poly;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
void displayPoly(struct node *poly)
{
while (poly!=NULL)
{
printf("%dX^%d",poly->coeffi,poly->pow);
poly=poly->next;
if(poly!=NULL)
printf(".");
}
printf("\n");
}
struct node* addpoly(struct node* poly1,struct node* poly2)
{
struct node *result=NULL;
while (poly1!=NULL & poly2!=NULL)
{
if(poly1->pow >poly2 ->pow)
{
addnode(&result,poly1->coeffi,poly1->pow);
poly1=poly1->next;
}
else if (poly1->pow<poly2->pow)
{
addnode(&result,poly2->coeffi,poly2->pow);
poly2=poly2->next;
}
else
{
addnode(&result,poly1->coeffi,poly1->pow);
poly1=poly1->next;
}
}

while(poly1 !=NULL)
{
addnode (&result,poly1->coeffi,poly1->pow);
poly1=poly1->next;
}
while(poly2!=NULL)
{
addnode (&result,poly2->coeffi,poly2->pow);
poly2=poly2->next;
}
return result;

}
int main()
{
struct node* poly1=NULL;
struct node* poly2=NULL;
struct node* result=NULL;
clrscr();
addnode(&poly1,3,2);
addnode(&poly1,5,1);
addnode(&poly1,6,0);
addnode(&poly2,6,1);
addnode(&poly2,8,0);
printf("1st polynomial is");
displayPoly(poly1);
printf("2nd polynomial is");
displayPoly(poly2);
result=addpoly(poly1,poly2);
printf("final polynomial :");
displayPoly(result);
return 0;
}

output:

You might also like