-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpoly.cpp
More file actions
162 lines (115 loc) · 2.86 KB
/
poly.cpp
File metadata and controls
162 lines (115 loc) · 2.86 KB
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
#include<iostream>
#include<stack>
#include<cmath>
using namespace std;
class Node{
public:
int pow;
int coeff;
Node *next;
};
class poly{
public:
Node *head, *tail;
poly(){
head=NULL;
tail=NULL;
}
void create_node(int c,int p){
Node* temp=new Node;
temp->coeff=c;
temp->pow=p;
temp->next=NULL;
if(head==NULL){
head=temp;
tail=temp;
}
else{
tail->next=temp;
tail=tail->next;
}
}
void show()
{
Node* node=head;
while(node!= NULL)
{
cout<<node->coeff<<"x^"<<node->pow;
node = node->next;
if(node!= NULL)
cout<<" + ";
}
}
};
void polyadd(poly p1,poly p2,poly p3){
Node *poly1=p1.head;
Node *poly2=p2.head;
Node *poly3=p3.head;
while(poly1->next!=NULL && poly2->next!=NULL)
{
// If power of 1st polynomial is greater then 2nd, then store 1st as it is
// and move its pointer
if(poly1->pow > poly2->pow)
{
poly3->pow = poly1->pow;
poly3->coeff = poly1->coeff;
poly1 = poly1->next;
}
// If power of 2nd polynomial is greater then 1st, then store 2nd as it is
// and move its pointer
else if(poly1->pow < poly2->pow)
{
poly3->pow = poly2->pow;
poly3->coeff = poly2->coeff;
poly2 = poly2->next;
}
// If power of both polynomial numbers is same then add their coefficients
else
{
poly3->pow = poly1->pow;
poly3->coeff = poly1->coeff+poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}
// Dynamically create new node
poly3->next=new Node();
poly3 = poly3->next;
}
while(poly1!=NULL || poly2!=NULL)
{
if(poly1!=NULL)
{
poly3->pow = poly1->pow;
poly3->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2!=NULL)
{
poly3->pow = poly2->pow;
poly3->coeff = poly2->coeff;
poly2 = poly2->next;
}
poly3=new Node();
poly3 = poly3->next;
}
}
int main(){
poly p1,p2,p3;
p1.create_node(5,2);
p1.create_node(4,1);
p1.create_node(2,0);
// Create second list of 5x^1 + 5x^0
p2.create_node(7,1);
p2.create_node(8,0);
p3.create_node(NULL,NULL);
cout<<"1st number: ";
p1.show();
cout<<"\n";
cout<<"2nd number: ";
p2.show();
// Function add two polynomial numbers
polyadd(p1,p2,p3);
// Display resultant List
cout<<"\nadded polynomial: ";
p3.show();
}