-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolynomialAddition(1).cpp
227 lines (215 loc) · 4.81 KB
/
PolynomialAddition(1).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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct node
{
int pow;
int coeff;
struct node *next;
}Node;
int power = 0,coefficient = 0;
//declaring heads of all 3 linked lists
Node *head1 = NULL;
Node *head2 = NULL;
Node *head3 = NULL;
Node *ptr123 = NULL;
//declaring functions
struct node *newTerm(struct node*);
struct node *insertAndSort(struct node*);
struct node *AddPoly(struct node*,struct node*,struct node*);
void display(struct node*);
int main()
{
int n1,n2;
cout<<"Enter the number of terms in equation 1"<<endl;
cin>>n1;
cout<<"Enter the number of terms in equation 2"<<endl;
cin>>n2;
cout<<"Enter the values for equation 1"<<endl;
for(int i=0;i<n1;i++)
{
head1 = insertAndSort(head1);
display(head1);
}
cout<<"\nEnter the values for equation 2"<<endl;
for(int i=0;i<n2;i++)
{
head2 = insertAndSort(head2);
display(head2);
}
cout<<"\n\nLIST 1\n"<<endl;
display(head1);
cout<<"\n\nLIST 2\n"<<endl;
display(head2);
cout<<"The added polynomial is :-"<<endl;
head3 = AddPoly(head1,head2,head3);
display(head3);
}
struct node *newTerm(struct node *head)
{
Node *newnode, *ptr, *temp;
newnode = (struct node*)malloc(sizeof(struct node));
printf("\n\n\nEnter the coefficient and power of a term to be inserted:\n");
scanf("%d %d",&coefficient,&power);
newnode->coeff = coefficient;
newnode->pow = power;
if(head==NULL)
{
head = newnode;
return head;
}
else
{
ptr = newnode;
return ptr;
}
}
struct node *insertAndSort(struct node *head)
{
int flag = 0,i=0;
if(head==NULL)
{
head = newTerm(head);
head->next = NULL;
return head;
}
else
{
Node *temp, *ptr, *previous;
temp = head;
ptr = newTerm(head);
if(power>temp->pow)
{
head = ptr;
ptr->next = temp;
return head;
}
else
{
do
{
if(temp->pow == power)
{
temp->coeff = temp->coeff + coefficient;
flag = 1;
return head;
}
else
{
previous = temp;
temp = temp->next;
}
}while((temp!=NULL)&&(power<=temp->pow));
if(flag==0)
{
ptr->next = previous->next;
previous->next = ptr;
return head;
}
}
}
}
struct node* AddPoly(struct node* head1,struct node* head2,struct node* head3)
{
Node *temp1, *temp2, *newnode, *temp3;
temp1 = head1;
temp2 = head2;
temp3 = head3;
int i=0;
while(temp1!=NULL&&temp2!=NULL)
{
if(temp1->pow==temp2->pow)
{
if(head3==NULL)
{
newnode = (struct node*)malloc(sizeof(struct node));
head3 = newnode;
newnode->pow = temp1->pow;
newnode->coeff = temp1->coeff + temp2->coeff;
newnode->next = NULL;
temp3 = newnode;
}
else
{
newnode = (struct node*)malloc(sizeof(struct node));
newnode->next = NULL;
newnode->pow = temp1->pow;
newnode->coeff = temp1->coeff + temp2->coeff;
temp3->next = newnode;
temp3 = temp3->next;
}
temp1 = temp1->next;
temp2 = temp2->next;
}
else
{
if(temp1->pow>temp2->pow)
{
newnode = (struct node*)malloc(sizeof(struct node));
if(head3==NULL)
{
head3 = newnode;
temp3 = head3;
}
newnode->next = NULL;
newnode->pow = temp1->pow;
newnode->coeff = temp1->coeff;
temp3->next = newnode;
temp3 = temp3->next;
temp1 = temp1->next;
}
else
{
newnode = (struct node*)malloc(sizeof(struct node));
if(head3==NULL)
{
head3 = newnode;
temp3 = head3;
}
newnode->next = NULL;
newnode->pow = temp2->pow;
newnode->coeff = temp2->coeff;
temp3->next = newnode;
temp3 = temp3->next;
temp2 = temp2->next;
}
}
}
while(temp1!=NULL)
{
newnode = (struct node*)malloc(sizeof(struct node));
newnode->next = NULL;
newnode->pow = temp1->pow;
newnode->coeff = temp1->coeff;
temp3->next = newnode;
temp3 = temp3->next;
temp1 = temp1->next;
}
while(temp2!=NULL)
{
newnode = (struct node*)malloc(sizeof(struct node));
newnode->next = NULL;
newnode->pow = temp2->pow;
newnode->coeff = temp2->coeff;
temp3->next = newnode;
temp3 = temp3->next;
temp2 = temp2->next;
}
return head3;
}
void display(struct node *head)
{
//system("cls");
Node *ptr;
ptr = head;
printf("\n\n");
printf("----------------------------------------------------------------------------------\n");
while(ptr->next!=NULL)
{
printf("|%d %d| --> ",ptr->coeff,ptr->pow);
ptr = ptr->next;
}
printf("|%d %d| --> NULL \n",ptr->coeff,ptr->pow);
printf("----------------------------------------------------------------------------------\n");
}