-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolynomial_Addition_singly_linked_list.c
132 lines (116 loc) · 2.51 KB
/
Polynomial_Addition_singly_linked_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Find the sum of two polynomial numbers. Represent the polynomial using single linked list.
// Input Format :
// First line contains the Degree of the 1st polynomial
// Second line contains the Coefficient of 1st polynomial
// Third line contains the degree of the 2nd polynomial
// Fourth line contains the Coefficient of 2nd polynomial
// Sample Input :
// 3
// 5 2 0 1
// 2
// 4 3 1
// Sample Output:
// 5x^3 + 6x^2 + 3x^1 + 2
// Explanation:
// 5x^3 + 2x^2 + 1 + 4x^2 + 3x^1 + 1 = 5x^3 + 6x^2 + 3x^1 + 2
#include<stdio.h>
#include<stdlib.h>
struct node
{
int pow;
int co;
struct node *next;
};
typedef struct node node;
node *position;
void insert(node *p,int c,int po)
{
node *newnode=malloc(sizeof(node));
newnode->next=NULL;
newnode->pow=po;
newnode->co=c;
if(p->next==NULL)
{
p->next=newnode;
position=newnode;
}
else
{
position->next=newnode;
position=newnode;
}
}
void display(node *ans)
{
node *position=ans->next;
while(position!=NULL)
{
if(position->co!=0)
{
printf("%d",position->co);
if(position->pow!=0)
{
printf("x^%d",position->pow);
printf("+");
}
}
position=position->next;
}
}
void add(node *poly1,node *poly2)
{
node *ans=malloc(sizeof(node));
ans->next=NULL;
position=ans;
node *p1=poly1->next;
node *p2=poly2->next;
while(p1!=NULL && p2!=NULL)
{
node *newnode=malloc(sizeof(node));
newnode->next=NULL;
if((p1->pow==p2->pow))
{
newnode->co=p1->co + p2->co;
newnode->pow=p1->pow;
p1=p1->next;
p2=p2->next;
}
else if ((p1->pow > p2->pow))
{
newnode->co=p1->co;
newnode->pow=p1->pow;
p1=p1->next;
}
else if((p1->pow < p2->pow))
{
newnode->co=p2->co;
newnode->pow=p2->pow;
p2=p2->next;
}
position->next=newnode;
position=newnode;
}
display(ans);
}
int main()
{
int n,m;
node *poly1=malloc(sizeof(node));
node *poly2=malloc(sizeof(node));
poly1->next=NULL;
poly2->next=NULL;
scanf("%d",&n);
for(int i=0;i<=n;i++)
{
scanf("%d",&m);
insert(poly1,m,n-i);
}
scanf("%d",&n);
for(int i=0;i<=n;i++)
{
scanf("%d",&m);
insert(poly2,m,n-i);
}
add(poly1,poly2);
return 0;
}