0% found this document useful (0 votes)
31 views4 pages

Experiment 9

Uploaded by

Pankaj Sangale
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)
31 views4 pages

Experiment 9

Uploaded by

Pankaj Sangale
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/ 4

//Experiment-9

include<stdio.h>
include<stdlib.h>
struct node
{
int coef,expo;
struct node* next;
};
struct node* insertpoly(struct node* thead,int c,int e);
struct node* append(struct node* thead,int c,int e);
struct node* polyaddition(struct node* p1thead,struct node* p2thead);
void display(struct node* thead);
void main()
{
int a,b,n,i;
struct node *p1head, *p2head, *p3head;
p1head=p2head=NULL;
// Inputing the first polynomial..
printf("Enter the no. of terms of polynomial 1");
scanf("%d",&n); // n=3
printf("\nEnter the polynomial..");
for(i=0;i<n;i++)// i=3, 3 < 3 false
{
printf("\nEnter the coefficient and exponent of the term");
scanf("%d%d",&a,&b);// a = 2 , b = 1
p1head=insertpoly(p1head,a,b);// (1000,2,1)
}
// Inputing the second polynomial..
printf("\nEnter the no of terms of polynomial 2..");
scanf("%d",&n);// n = 2
printf("\nEnter the polynomial..");
for(i=0;i<n;i++) // i =2,2< 2 false
{
printf("\nEnter the coefficient and exponent of the term..");
scanf("%d%d",&a,&b); // a = 5, b =0
p2head=insertpoly(p2head,a,b); // 4000, 5 , 0
}
//Performing Addition..
p3head=polyaddition(p1head,p2head); // p3head = 100
//Displaying the polynomial..
printf("\nThe polynomial 1 is..");
display(p1head);
printf("\nThe polynomial 2 is..");
display(p2head);
printf("\nThe sum of the two polynomials is..");
display(p3head);
}
struct node* append(struct node* thead,int c,int e) // thead = 100, c = 7, e = 0
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->coef=c;
newnode->expo=e;
if(thead==NULL)// true
{// Corner Case to handle if the list is empty...
newnode->next=NULL;
return newnode;
}
struct node* trav=thead;
while(trav->next!=NULL) // false
{
trav=trav->next;
} // Traversing to point to the last node...

trav->next=newnode;
newnode->next=NULL;
return thead;
}
struct node* insertpoly(struct node* thead,int c,int e)// thead = 1000, c=2, e=1
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->coef=c;
newnode->expo=e;
if(thead==NULL)// false
{ // for inserting the first node..
newnode->next=NULL;
return newnode;
}
struct node *prev, *curr;
prev=curr=thead;
while(curr!=NULL && curr->expo>e)// false && true
{
prev=curr;
curr=curr->next;
}
if(curr==thead)// false
{ // for inserting before the first node...
newnode->next=curr;
return newnode;
}
else if(curr==NULL) // true
{ //for inserting after the last node....
prev->next=newnode;
newnode->next=NULL;
}
else
{
newnode->next=curr;
prev->next=newnode;
}
return thead;
}
struct node* polyaddition(struct node* p1thead,struct node* p2thead) // p1thead = 1000 , p2thead = 3000
{
struct node* ans=NULL;
struct node* t1,* t2;
t1=p1thead;
t2=p2thead;
while(t1!=NULL && t2!=NULL)
{
if(t1->expo > t2->expo) //false
{
ans=append(ans,t1->coef,t1->expo); // (NULL , 5, 2)
t1=t1->next;
}
else if(t1->expo < t2->expo)// false
{
ans=append(ans,t2->coef,t2->expo);
t2=t2->next;
}
else
{
ans=append(ans,(t1->coef)+(t2->coef),t1->expo);// 100 , 7, 0
t1=t1->next;
t2=t2->next;
}
}

while(t1!=NULL){ //coping the remaining terms of polynomial 1...


ans=append(ans,t1->coef,t1->expo);
t1=t1->next;
}
while(t2!=NULL){ //coping the remaining terms of polynomial 2...
ans=append(ans,t2->coef,t2->expo);
t2=t2->next;
}
return ans;
}
void display(struct node* thead)
{
struct node* temp=thead;
if(temp==NULL) // false
{
printf("\nEmpty..");
}
else
{
while(temp->next!=NULL)// false
{
printf(" %dx^%d +",temp->coef,temp->expo);// 5x^2 + 9x^1 +
temp=temp->next;
}
printf(" %dx^%d ",temp->coef,temp->expo); // 5x^2 + 9x^1 + 7x^0
}
}

OUTPUT:--
Example Output for the Provided Code:
Input Polynomial 1:
```
Enter the no. of terms of polynomial 1: 3
Enter the polynomial:
Enter the coefficient and exponent of the term: 5 2
Enter the coefficient and exponent of the term: 3 1
Enter the coefficient and exponent of the term: 2 0
```
Input Polynomial 2:
```
Enter the no. of terms of polynomial 2: 2
Enter the polynomial:
Enter the coefficient and exponent of the term: 4 1
Enter the coefficient and exponent of the term: 7 0
```
Output:
```
The polynomial 1 is:
5x^2 + 3x^1 + 2x^0

The polynomial 2 is:


4x^1 + 7x^0
The sum of the two polynomials is:
5x^2 + 7x^1 + 9x^0
```

You might also like