-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular_Linked_List.cpp
More file actions
274 lines (258 loc) · 6.04 KB
/
Circular_Linked_List.cpp
File metadata and controls
274 lines (258 loc) · 6.04 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Name: Chirantan Joshi.
Div: G
Batch: G1
Roll No.: PG13
POLYNOMIAL OPERATIONS WITH CLL
*/
#include <iostream>
#include <math.h>
using namespace std;
class term //Class for a term in polynomial
{
int coef,expo;
term *next;
public:
friend class poly; //Making polynomial class as friend of term class
};
class poly //Class polynomial for operations on polynomial
{
term *head=new term;
public:
poly() //constructor for initialization
{
head->next=head;
head->coef=0;
head->expo=-1;
}
void create(); //Functions that are to be applied
void display();
void addpoly(poly p1,poly p2);
void eval(int x,poly p);
};
void poly::create() //To create new polynomial
{
int cnt=0;
char choice;
term *temp,*curr,*temp1,*temp2;
temp=head;
do //loop for creating different terms
{
curr=new term; //allocate memory by "new"
cout<<"Enter the coefficient: ";
cin>>curr->coef;
cout<<"Enter the exponent: "; //storing values to current pointer
cin>>curr->expo;
temp->next=curr;
curr->next=head; //temporory pointer to traverse through polynomial
temp=temp->next;
temp1=head->next;
for(int i=0;i<cnt;i++)
{
if((curr->expo > temp1->expo)&&(temp1!=head))
{
temp2->coef=curr->coef;
temp2->expo=curr->expo;
curr->coef=temp1->coef;
curr->expo=temp1->expo;
temp1->coef=temp2->coef;
temp1->expo=temp2->expo;
}
temp1=temp1->next;
}
cnt++;
cout<<"Do you want to add more terms?(y/n): ";
cin>>choice;
}while(choice=='y');
}
void poly::display() //To display polynomial
{
term *temp; //temporary pointer to traverse through polynomial
temp=head->next;
cout<<endl<<"The polynomial is: ";
while(temp!=head)
{
cout<<temp->coef; //displaying polynomial
if(temp->expo!=0 && temp->expo!=1)
cout<<"x^"<<temp->expo;
else if(temp->expo==1)
cout<<"x";
if(temp->next != head && temp->next->coef >= 0)
cout<<" + ";
else if(temp->next->coef < 0)
cout<<" ";
temp=temp->next;
}
cout<<endl;
}
void poly::addpoly(poly p1,poly p2) //To add 2 polynomials
{
poly p3;
term *t1,*t2,*t3,*t4; //t1,t2 pointers to traverse 2 polynomial
t1=p1.head->next;
t2=p2.head->next;
t4=p3.head; //t3,t4 pointers to maintain 3rd(result)polynomial
while(t1!=p1.head || t2!=p2.head)
{
t3=new term;
if(t1->expo==t2->expo) //taking different conditions w.r.t. exponents
{
t3->coef=t1->coef + t2->coef;
t3->expo=t1->expo;
t4->next=t3;
t3->next=p3.head;
t4=t4->next;
t1=t1->next;
t2=t2->next;
}
else if(t1->expo > t2->expo)
{
t3->coef=t1->coef;
t3->expo=t1->expo;
t4->next=t3;
t3->next=p3.head;
t4=t4->next;
t1=t1->next;
}
else if(t1->expo < t2->expo)
{
t3->coef=t2->coef;
t3->expo=t2->expo;
t4->next=t3;
t3->next=p3.head;
t4=t4->next;
t2=t2->next;
}
}
cout<<endl<<"After addition,";
p3.display(); //Displaying result polynomial
}
void poly::eval(int val,poly p) //To evaluate the polynomial by given value
{
int sum=0,res;
term *curr=p.head->next;
while(curr!=head)
{
res=(curr->coef)*(pow(val,curr->expo)); //Evaluating terms one by one,with pow inbuilt function
sum+=res; //summing all the terms
curr=curr->next;
}
cout<<"The result after polynomial evaluation is: "<<sum<<endl;
}
int main()
{
int val,choice;
while(1)
{
poly p1,p2,p3;
cout<<"\n-----------MENU-----------\n\n1.Create and Display.\n2.Add.\n3.Evaluate.\n4.Exit.\nEnter the choice: ";
cin>>choice; //Taking choice for different operations
switch(choice)
{
case 1: cout<<"Enter the polynomial."<<endl;
p1.create();
p1.display();
break;
case 2: cout<<"Enter the first polynomial."<<endl;
p1.create();
cout<<endl<<"Enter the second polynomial."<<endl;
p2.create();
p1.display();
p2.display();
p3.addpoly(p1,p2);
break;
case 3: cout<<"Enter the polynomial."<<endl;
p1.create();
p1.display();
cout<<"Enter the value for variable: ";
cin>>val;
p1.eval(val,p1);
break;
case 4: exit(1);
break;
default:cout<<"Wrong choice!!\nEnter the valid choice.\n";
}
}
return 0;
}
/*
OUTPUT:
-----------MENU-----------
1.Create and Display.
2.Add.
3.Evaluate.
4.Exit.
Enter the choice: 1
Enter the polynomial.
Enter the coefficient: 3
Enter the exponent: 2
Do you want to add more terms?(y/n): y
Enter the coefficient: 4
Enter the exponent: 1
Do you want to add more terms?(y/n): y
Enter the coefficient: 7
Enter the exponent: 0
Do you want to add more terms?(y/n): n
The polynomial is: 3x^2 + 4x + 7
-----------MENU-----------
1.Create and Display.
2.Add.
3.Evaluate.
4.Exit.
Enter the choice: 2
Enter the first polynomial.
Enter the coefficient: 2
Enter the exponent: 5
Do you want to add more terms?(y/n): y
Enter the coefficient: 3
Enter the exponent: 2
Do you want to add more terms?(y/n): y
Enter the coefficient: 1
Enter the exponent: 0
Do you want to add more terms?(y/n): n
Enter the second polynomial.
Enter the coefficient: 2
Enter the exponent: 6
Do you want to add more terms?(y/n): y
Enter the coefficient: 5
Enter the exponent: 5
Do you want to add more terms?(y/n): y
Enter the coefficient: 3
Enter the exponent: 3
Do you want to add more terms?(y/n): y
Enter the coefficient: 9
Enter the exponent: 0
Do you want to add more terms?(y/n): n
The polynomial is: 2x^5 + 3x^2 + 1
The polynomial is: 2x^6 + 5x^5 + 3x^3 + 9
After addition,
The polynomial is: 2x^6 + 7x^5 + 3x^3 + 3x^2 + 10
-----------MENU-----------
1.Create and Display.
2.Add.
3.Evaluate.
4.Exit.
Enter the choice: 3
Enter the polynomial.
Enter the coefficient: 4
Enter the exponent: 3
Do you want to add more terms?(y/n): y
Enter the coefficient: 6
Enter the exponent: 2
Do you want to add more terms?(y/n): y
Enter the coefficient: -2
Enter the exponent: 1
Do you want to add more terms?(y/n): y
Enter the coefficient: 7
Enter the exponent: 0
Do you want to add more terms?(y/n): n
The polynomial is: 4x^3 + 6x^2 -2x + 7
Enter the value for variable: 3
The result after polynomial evaluation is: 163
-----------MENU-----------
1.Create and Display.
2.Add.
3.Evaluate.
4.Exit.
Enter the choice: 4
*/