-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyAdd_list.cpp
More file actions
151 lines (139 loc) · 4.09 KB
/
polyAdd_list.cpp
File metadata and controls
151 lines (139 loc) · 4.09 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
#include <iostream>
#include "polyAdd_list.h"
bool isEmpty(NPTR &list)
{
if(list == nullptr)
return true;
return false;
}
NPTR createNode(const int &coef, const int °)
{
NPTR plist = new NODE;
plist->coefficient = coef;
plist->degree = deg;
plist->next = nullptr;
return plist;
}
NPTR insertEl(NPTR &list, const int &coef, const int °, NPTR &tempNode)
{
NPTR newNode = createNode(coef, deg);
if(isEmpty(list))
{
tempNode = newNode;
list = newNode;
return tempNode;
}
tempNode->next = newNode;
tempNode = newNode;
return tempNode;
}
NPTR polyAdd(NPTR &newList, NPTR &firstList, NPTR &secondList, NPTR &tempNode)
{
NPTR sumList = nullptr;
int updtCoef = 0;
while(firstList != nullptr && secondList != nullptr)
{
if(firstList->degree == secondList->degree)
{
updtCoef = firstList->coefficient + secondList->coefficient;
sumList = insertEl(newList, updtCoef, firstList->degree, tempNode);
firstList = firstList->next;
secondList = secondList->next;
}
else if(firstList->degree > secondList->degree)
{
updtCoef = firstList->coefficient;
sumList = insertEl(newList, updtCoef, firstList->degree, tempNode);
firstList = firstList->next;
}
else if(secondList->degree > firstList->degree)
{
updtCoef = secondList->coefficient;
sumList = insertEl(newList, updtCoef, secondList->degree, tempNode);
secondList = secondList->next;
}
}
while(firstList == nullptr && secondList != nullptr)
{
sumList = insertEl(newList, secondList->coefficient, secondList->degree, tempNode);
secondList = secondList->next;
}
while(secondList == nullptr && firstList != nullptr)
{
sumList = insertEl(newList, firstList->coefficient, firstList->degree, tempNode);
firstList = firstList->next;
}
sumList = newList;
return sumList;
}
void display(NPTR &newList, NPTR &firstList, NPTR &secondList, NPTR &tempNode)
{
NPTR polySum = nullptr, bak = nullptr;
polySum = polyAdd(newList, firstList, secondList, tempNode);
bak = polySum;
while(polySum != nullptr)
{
if(polySum->next == nullptr)
std::cout << polySum->coefficient <<"x^" << polySum->degree;
else
std::cout << polySum->coefficient <<"x^" << polySum->degree << "+";
polySum = polySum->next;
}
std::cout << "\n";
delList(bak);
}
void delList(NPTR &list)
{
NPTR delNode = nullptr;
while(list != nullptr)
{
delNode = list;
list = list->next;
delete delNode;
}
list = nullptr;
}
int main()
{
int coefficient, degree, ch, ch1 = 0;
NPTR firstList = nullptr;
NPTR secondList = nullptr;
NPTR newList = nullptr;
NPTR tempNode = nullptr;
NPTR firsttempNode = nullptr;
NPTR secondtempNode = nullptr;
std::cout << "Kindly enter the elements of the polynomials you want to add" << std::endl;
do
{
std::cout << "Please enter the degree of each element of the polynomial followed by the coefficients" << std::endl;
std::cout << "1. To enter the elements sequentially of the first polynomial" << std::endl;
std::cout << "2. To enter the elements sequentially of the second polynomial" << std::endl;
std::cout << "3. Display the resultant polynomial after the both polynomials are entered" << std::endl;
std::cout << "4. Exit" << std::endl;
std::cout << "Please enter your choice" << std::endl;
std::cin >> ch;
switch(ch)
{
case 1 : std::cout << "Enter the coefficient of the polynomial" << std::endl;
std::cin >> coefficient;
std::cout << "Enter the degree of the polynomial" << std::endl;
std::cin >> degree;
insertEl(firstList, coefficient, degree, firsttempNode);
break;
case 2 : std::cout << "Enter the coefficient of the polynomial" << std::endl;
std::cin >> coefficient;
std::cout << "Enter the degree of the polynomial" << std::endl;
std::cin >> degree;
insertEl(secondList, coefficient, degree, secondtempNode);
break;
case 3 : display(newList, firstList, secondList, tempNode);
break;
case 4 : delList(newList);
delList(secondList);
delList(firstList);
break;
default : std::cout << "Invalid Choice" << std::endl;
}
}while(ch != 4);
std::cout << "Thank You!" << std::endl;
}