-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay9.cpp
179 lines (151 loc) · 3.63 KB
/
Day9.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
/* Link list Node */
struct Node
{
int coeff;
int pow;
struct Node* next;
Node(int c, int p){
coeff = c;
pow = p;
next = NULL;
}
};
void append(struct Node** head_ref, struct Node **tail_ref,
int new_data, int new_data1)
{
struct Node* new_node = new Node(new_data, new_data1);
if (*head_ref == NULL)
*head_ref = new_node;
else
(*tail_ref)->next = new_node;
*tail_ref = new_node;
}
void printList(struct Node *head)
{
struct Node *temp = head;
while (temp != NULL)
{
printf("%d %d ", temp->coeff, temp->pow);
temp = temp->next;
}
}
Node* addPolynomial(Node *p1, Node *p2);
void create_node(int x, int y, struct Node **temp)
{
struct Node *r, *z;
z = *temp;
if(z == NULL)
{
r = new Node(x, y);
*temp = r;
r->next = NULL;
}
else
{
r->next = new Node(x, y);
r = r->next;
r->next = NULL;
}
}
/* Driver program to test above function*/
// } Driver Code Ends
/* Structure of Node used
struct Node
{
int coeff;
int pow;
struct Node* next;
Node(int c, int p){
coeff = c;
pow = p;
next = NULL;
}
};
*/
class Solution{
public:
/* The below method print the required sum of polynomial
p1 and p2 as specified in output */
Node* addPolynomial(Node *p1, Node *p2)
{
//Your code here
Node *temp, *ans;
if (p1->pow == p2->pow) {
ans=temp=new Node(p1->coeff+p2->coeff, p1->pow);
p1 = p1->next;
p2 = p2->next;
} else if(p1->pow > p2->pow) {
ans=temp=new Node(p1->coeff, p1->pow);
p1 = p1->next;
} else {
ans=temp=new Node(p2->coeff, p2->pow);
p2 = p2->next;
}
while (p1!=nullptr and p2!=nullptr) {
if (p1->pow==p2->pow) {
temp->next = new Node(p1->coeff+p2->coeff, p1->pow);
p1 = p1->next;
p2 = p2->next;
} else if(p1->pow > p2->pow) {
temp->next=new Node(p1->coeff, p1->pow);
p1 = p1->next;
} else {
temp->next=new Node(p2->coeff, p2->pow);
p2 = p2->next;
}
temp = temp->next;
}
while (p1!=nullptr) {
temp->next=new Node(p1->coeff, p1->pow);
p1 = p1->next;
temp = temp->next;
}
while (p2!=nullptr) {
temp->next=new Node(p2->coeff, p2->pow);
p2 = p2->next;
temp = temp->next;
}
return ans;
}
//};
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;
struct Node *tail1 = NULL,*tail2 = NULL;
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int x,y;
cin>>x>>y;
append(&poly1,&tail1,x,y);
}
int m;
cin>>m;
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
append(&poly2,&tail2,x,y);
}
Solution obj;
Node *sum = obj.addPolynomial(poly1,poly2);
for(Node* ptr=sum ; ptr ; ptr=ptr->next )
{
// printing polynomial
cout<< ptr->coeff << "x^" << ptr->pow;
if(ptr->next) cout<<" + ";
}
cout<<endl;
}
}
// } Driver Code Ends