Skip to content

Commit 49c66d6

Browse files
Create Polynomial_Addition_singly_linked_list.c
1 parent 7460bcb commit 49c66d6

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed
+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Find the sum of two polynomial numbers. Represent the polynomial using single linked list.
2+
3+
// Input Format :
4+
5+
// First line contains the Degree of the 1st polynomial
6+
7+
// Second line contains the Coefficient of 1st polynomial
8+
9+
// Third line contains the degree of the 2nd polynomial
10+
11+
// Fourth line contains the Coefficient of 2nd polynomial
12+
13+
// Sample Input :
14+
15+
// 3
16+
17+
// 5 2 0 1
18+
19+
// 2
20+
21+
// 4 3 1
22+
23+
// Sample Output:
24+
25+
// 5x^3 + 6x^2 + 3x^1 + 2
26+
27+
// Explanation:
28+
29+
// 5x^3 + 2x^2 + 1 + 4x^2 + 3x^1 + 1 = 5x^3 + 6x^2 + 3x^1 + 2
30+
31+
#include<stdio.h>
32+
#include<stdlib.h>
33+
struct node
34+
{
35+
int pow;
36+
int co;
37+
struct node *next;
38+
};
39+
typedef struct node node;
40+
node *position;
41+
void insert(node *p,int c,int po)
42+
{
43+
node *newnode=malloc(sizeof(node));
44+
newnode->next=NULL;
45+
newnode->pow=po;
46+
newnode->co=c;
47+
if(p->next==NULL)
48+
{
49+
p->next=newnode;
50+
position=newnode;
51+
}
52+
else
53+
{
54+
position->next=newnode;
55+
position=newnode;
56+
}
57+
}
58+
void display(node *ans)
59+
{
60+
node *position=ans->next;
61+
while(position!=NULL)
62+
{
63+
if(position->co!=0)
64+
{
65+
printf("%d",position->co);
66+
if(position->pow!=0)
67+
{
68+
printf("x^%d",position->pow);
69+
printf("+");
70+
}
71+
}
72+
position=position->next;
73+
}
74+
}
75+
void add(node *poly1,node *poly2)
76+
{
77+
node *ans=malloc(sizeof(node));
78+
ans->next=NULL;
79+
position=ans;
80+
node *p1=poly1->next;
81+
node *p2=poly2->next;
82+
while(p1!=NULL && p2!=NULL)
83+
{
84+
node *newnode=malloc(sizeof(node));
85+
newnode->next=NULL;
86+
if((p1->pow==p2->pow))
87+
{
88+
newnode->co=p1->co + p2->co;
89+
newnode->pow=p1->pow;
90+
p1=p1->next;
91+
p2=p2->next;
92+
}
93+
else if ((p1->pow > p2->pow))
94+
{
95+
newnode->co=p1->co;
96+
newnode->pow=p1->pow;
97+
p1=p1->next;
98+
}
99+
else if((p1->pow < p2->pow))
100+
{
101+
newnode->co=p2->co;
102+
newnode->pow=p2->pow;
103+
p2=p2->next;
104+
}
105+
position->next=newnode;
106+
position=newnode;
107+
}
108+
while(p1!=NULL || p2!=NULL)
109+
{
110+
if(p1==NULL && p2==NULL)
111+
break;
112+
else{
113+
node *newnode=malloc(sizeof(node));
114+
newnode->next=NULL;
115+
/*if(p1==NULL && p2==NULL)
116+
{
117+
newnode->co=p1->co + p2->co;
118+
newnode->pow=p1->pow;
119+
position->next=newnode;
120+
position=newnode;
121+
break;
122+
}*/
123+
{
124+
if(p1!=NULL)
125+
{
126+
newnode->co=p1->co;
127+
newnode->pow=p1->pow;
128+
p1=p1->next;
129+
}
130+
if(p2!=NULL)
131+
{
132+
newnode->co=p2->co;
133+
newnode->pow=p2->pow;
134+
p2=p2->next;
135+
}
136+
position->next=newnode;
137+
position=newnode;
138+
}
139+
}
140+
}
141+
display(ans);
142+
}
143+
144+
int main()
145+
{
146+
int n,m;
147+
node *poly1=malloc(sizeof(node));
148+
node *poly2=malloc(sizeof(node));
149+
poly1->next=NULL;
150+
poly2->next=NULL;
151+
scanf("%d",&n);
152+
for(int i=0;i<=n;i++)
153+
{
154+
scanf("%d",&m);
155+
insert(poly1,m,n-i);
156+
}
157+
scanf("%d",&n);
158+
for(int i=0;i<=n;i++)
159+
{
160+
scanf("%d",&m);
161+
insert(poly2,m,n-i);
162+
}
163+
add(poly1,poly2);
164+
return 0;
165+
}

0 commit comments

Comments
 (0)