Skip to content

Commit d38a343

Browse files
Add files via upload
1 parent 6cc3daa commit d38a343

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
typedef struct polynomial
4+
{
5+
int pow;
6+
int coeff;
7+
struct polynomial *next;
8+
}poly;
9+
void display(poly*head)
10+
{
11+
poly*p=head;
12+
printf("The polynomials after adding are : \n");
13+
while(p!=NULL)
14+
{
15+
printf("%dx^%d\t",p->coeff,p->pow);
16+
if(p->pow!=0)
17+
printf("+\t");
18+
p=p->next;
19+
}
20+
printf("\n");
21+
}
22+
poly* create(poly*head)
23+
{
24+
poly*temp=(poly*)malloc(sizeof(poly));
25+
if(temp==NULL)
26+
{
27+
printf("OVERFLOW!!!!\n");
28+
return NULL;
29+
}
30+
head=temp;
31+
do
32+
{
33+
printf("Enter the power of variable : ");
34+
scanf("%d",&temp->pow);
35+
printf("Enter the coefficient : ");
36+
scanf("%d",&temp->coeff);
37+
poly *p=temp;
38+
if(p->pow!=0)
39+
{
40+
temp=(poly*)malloc(sizeof(poly));
41+
if(temp==NULL)
42+
{
43+
printf("OVERFLOW!!!!\n");
44+
return NULL;
45+
}
46+
p->next=temp;
47+
}
48+
}while(temp->pow!=0);
49+
temp->next=NULL;
50+
return head;
51+
}
52+
void add(poly*h1,poly*h2)
53+
{
54+
poly*head=(poly*)malloc(sizeof(poly));
55+
poly*temp=head;
56+
while(h1 && h2)
57+
{
58+
if(h1->pow>h2->pow)
59+
{
60+
temp->pow=h1->pow;
61+
temp->coeff=h1->coeff;
62+
h1=h1->next;
63+
}
64+
else if(h2->pow>h1->pow)
65+
{
66+
temp->pow=h2->pow;
67+
temp->coeff=h2->coeff;
68+
h2=h2->next;
69+
}
70+
else
71+
{
72+
temp->pow=h1->pow;
73+
temp->coeff=h1->coeff+h2->coeff;
74+
h1=h1->next;
75+
h2=h2->next;
76+
}
77+
if(h1 || h2)
78+
{
79+
temp->next=(poly*)malloc(sizeof(poly));
80+
temp=temp->next;
81+
}
82+
}
83+
while(h1 || h2)
84+
{
85+
if(h1)
86+
{
87+
temp->pow=h1->pow;
88+
temp->coeff=h1->coeff;
89+
h1=h1->next;
90+
}
91+
if(h2)
92+
{
93+
temp->pow=h2->pow;
94+
temp->coeff=h2->coeff;
95+
h2=h2->next;
96+
}
97+
if(h1 || h2)
98+
{
99+
temp->next=(poly*)malloc(sizeof(poly));
100+
temp=temp->next;
101+
}
102+
}
103+
temp->next=NULL;
104+
display(head);
105+
}
106+
int main()
107+
{
108+
poly*h1=NULL,*h2=NULL,*p1,*p2;
109+
printf("Enter the first polynomial : \n");
110+
h1=create(h1);
111+
printf("Enter the second polynomial : \n");
112+
h2=create(h2);
113+
add(h1,h2);
114+
return 0;
115+
}

0 commit comments

Comments
 (0)