Linked Lists

As with most of my work I have managed to make it through most of the work assigned to me except I always have one problem that I can't fix. Here is a program that takes in two polynomials and then allows you to add or subtract them. My problem is that if the polynomials are put in so that the exponent on one starts out as being higher that the initial exponent of the other the lists will not advance through the nodes. Can anyone point me to where the problem is, give me some ideas on how to fix it, anything?
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <string>
#include <iostream>
using namespace std;
struct TermType
{
	int coefficient;
	int exponent;
	bool NegCoef;

	TermType* next;		
};

class Polynomial		
{
private:
	TermType* head;
	
public:
	// default constructor: create an empty polynomial
	Polynomial();				
// copy constructor: use the content of p1 to create a polynomial
	Polynomial(const Polynomial& p1);		
// release dynamic memory related to the polynomial
	~Polynomial();				
// assign the content of p1 to a polynomial
	void operator=(const Polynomial& p1);
// add a polynomial
	void operator+=(const Polynomial& p1);
// subtract a polynomial
	void operator-=(const Polynomial& p1); 
// read a polynomial
	void read();
// output a polynomial
	void print();
//new input function
	void insertTerm(bool,int,int);
};

Polynomial::Polynomial()
{
	head=NULL;
}
Polynomial::~Polynomial()
{
	TermType* newTerm=head;
	TermType* current=head;
	while(current != NULL)
  {
	  current = current->next;
      delete newTerm;
      newTerm=current;
  }



}
void Polynomial::operator-=(const Polynomial& p1)
{
	TermType* poly1=head;
	TermType* poly2=p1.head;
	int counter=0;
	while ((poly1!=NULL)||(poly2!=NULL))
	{
		if (poly2==NULL&&poly1!=NULL)
		{
			if (poly1->coefficient>0)
				cout<<"+";
			cout<<poly1->coefficient<<"x^"<<poly1->exponent;
			poly1=poly1->next;
		}
		if (poly1==NULL&&poly2!=NULL)
		{
			if (poly2->coefficient>0)
				cout<<"+";
			cout<<poly2->coefficient<<"x^"<<poly2->exponent;
			poly2=poly2->next;
		}
		if (poly1!=NULL&&poly2!=NULL)
		{
			if (poly1->exponent==poly2->exponent)
				{
					if(poly1->coefficient+poly2->coefficient>0)
						cout<<"+";
					cout<<poly1->coefficient-poly2->coefficient<<"x^"<<poly1->exponent;
							if (poly1!=NULL)
								poly1=poly1->next;
							if (poly2!=NULL)
								poly2=poly2->next;
							continue; 
				}
			if (poly1->exponent>poly2->exponent)
				{
					if (poly1->coefficient>0)
						cout<<"+";
					cout<<poly1->coefficient<<"x^"<<poly1->exponent;
						poly2=poly2->next;
					continue;
				}
			if (poly2->exponent>poly1->exponent)
				{
					if (poly2->coefficient>0)
						cout<<"+";
					cout<<poly2->coefficient<<"x^"<<poly2->exponent;
						poly1=poly1->next;				
					continue;
				}			
		}
	}
}
void Polynomial::operator+=(const Polynomial& p1)
{
	TermType* poly1=head;
	TermType* poly2=p1.head;
	int counter=0;
	while ((poly1!=NULL)||(poly2!=NULL))
	{
		if (poly2==NULL&&poly1!=NULL)
		{
			if (poly1->coefficient>0)
				cout<<"+";
	//		if (poly1->coefficient<0)
	//			cout<<"-";
			cout<<poly1->coefficient<<"x^"<<poly1->exponent;
			poly1=poly1->next;
		}
		if (poly1==NULL&&poly2!=NULL)
		{
			if (poly2->coefficient>0)
				cout<<"+";
			cout<<poly2->coefficient<<"x^"<<poly2->exponent;
			poly2=poly2->next;
		}
		if (poly1!=NULL&&poly2!=NULL)
		{
			if (poly1->exponent==poly2->exponent)
				{
					if(poly1->coefficient+poly2->coefficient>0)
						cout<<"+";
					cout<<poly1->coefficient+poly2->coefficient<<"x^"<<poly1->exponent;
							if (poly1!=NULL)
								poly1=poly1->next;
							if (poly2!=NULL)
								poly2=poly2->next;
							continue; 
				}
			if (poly1->exponent>poly2->exponent)
				{
					if (poly1->coefficient>0)
						cout<<"+";
					cout<<poly1->coefficient<<"x^"<<poly1->exponent;
						poly2=poly2->next;
					continue;
				}
			if (poly2->exponent>poly1->exponent)
				{
					if (poly2->coefficient>0)
						cout<<"+";
					cout<<poly2->coefficient<<"x^"<<poly2->exponent;
						poly1=poly1->next;				
					continue;
				}			
		}
	}
}
void Polynomial::insertTerm(bool negCoef, int coef, int exp)
{
	cout << "begin insert!" << endl;
	cout << negCoef << "    " << coef << "    " << exp << endl;

	TermType* newTerm = new TermType;
	if (negCoef == 1)
		newTerm->coefficient = 0 - coef;
	else
		newTerm->coefficient = coef;
	newTerm->exponent = exp;
	newTerm->next = NULL;

	TermType* current = head;
	if (head == NULL)
		head = newTerm;
	else
	{
		while (current->next != NULL)
		current=current->next;
		current->next = newTerm;
	}
	cout << "end insert!" << endl;
}
//read a polynomial
void Polynomial::read()
{
	string s;
	cout << "Enter a term (ex: +4x^3): ";
	cin >> s;
	int index = 0;
	bool firstTerm = 1;
	bool nowExp = 0;
	bool negativeCoef = 0;
	int coef = 0;
	int exp = 0;

	while (s[index] != '#')
	{
		cout << "char : " << s[index] << endl;
		if (s[index] == '+' || s[index] == '-')
		{
			cout << "+-" << endl;
			if (firstTerm == 0)
				{
					if (coef != 0)
					insertTerm(negativeCoef, coef, exp);
				}
			else
			{
				firstTerm = 0;
			}
			nowExp = 0;
			if (s[index] == '-')
				negativeCoef = 1;
			else
					negativeCoef = 0;
				coef = 0;
				exp = 0;
			}
			else if (s[index] == 'x')
			{
				cout << "x" << endl;
				if (s[index+1] != '^')
					{
						cout << "Incorrect formula!" << endl;
						head = NULL;
						break;
					}
			}
			else if (s[index] == '^')
			{
				cout << "^" << endl;
				nowExp = 1;
			}
			else if (s[index]>='0' && s[index]<='9')
			{
				cout << "number" << endl;
				if (nowExp == 0)
				coef = coef*10 + (s[index]-48);
				else
				exp = exp*10 + (s[index]-48);
			}
			else
			{
				cout << "The formula contains invalid characters: " << s[index] << endl;
				head = NULL;
				break;
			}
			index++;
		}
		if (coef != 0)
		insertTerm(negativeCoef, coef, exp);
		cout << "done!"<< endl;
}
void Polynomial::print()
{
	TermType* newTerm=head;
	while (newTerm != NULL)
	{
		if (newTerm->coefficient>0)
			cout<<"+";
		cout<<newTerm->coefficient<<"x^"<<newTerm->exponent;
		newTerm=newTerm->next;
	}
	cout << endl;
}
int main()
{
	Polynomial p, p1;	
	
		char option;
do //setting up a menu
	{
		
		cout << endl << endl << "Choose from:" << endl << endl;
		cout << "1. Enter first polynomial:" << endl;
		cout << "2. Enter second polynomial:" << endl;
		cout << "3. Print first polynomial:" << endl;
		cout << "4. Print second polynomial:"<<endl;
		cout << "5. Add polynomials:"<<endl;
		cout << "6. Subtract Polynomials:"<<endl;
		cout << "9. Exit:"<<endl;
		cin >> option;

		switch (option)
		{
		case '1':
			p.read();
			break;
		case '2':
			p1.read();
			break;
		case '3':
			p.print();
			break;
		case '4':
			p1.print();
			break;
		case '5':
			p+=(p1);
			break;
		case '6':
			p-=(p1);
			break;
		case '9':
			
		default:
			cout << "invalid input!!! Try it again." << endl;
		}
	} while (option != '9');
	return 0;
}
My problem is that if the polynomials are put in so that the exponent on one starts out as being higher that the initial exponent of the other the lists will not advance through the nodes.
Sort them correctly before starting.
I am allowed to assume that the polynomials will be put in in correct order (ie. +4x^4+3x^3) with the largest exponent starting out the polynomial going to the smallest.
So what's the problem, then?
My problem is that if the polynomials are put in so that the exponent on one starts out as being higher that the initial exponent of the other the lists will not advance through the nodes. Can anyone point me to where the problem is, give me some ideas on how to fix it, anything?

I just can't figure out why it's not advancing in this case.
This doesn't directly solve your problem, but IMHO, you really should write some helper functions.

For example, write a function that returns the coefficient for a given term in a polynomial. If the coefficient does not exist, return 0. This will allow you to greatly simplify your code.

Another suggestion: if X and Y are integers, then X - Y can be written as X + (-Y). Therefore, your operator-= could simply negate the coefficients of the right-hand side then call operator+=. This will reduce your code by 40%. (As it stands now, every bug in operator-= most likely exists in operator+= and vice versa because the functions are so similar).

A third suggestion: neither operator+= nor operator-= should output the polynomial. write operator<< or a print function (if you are not familiar with writing operator<<) instead. This will further simplify operator+=.

A fourth suggestion, as a matter of writing code that is intuitive to use: if X, Y, and Z are integers, then in C++ I can write the line of code X = ( Y += Z ); which adds Z to Y, then assigns the new value of Y to X. Your declaration of operator+= and operator-= do not allow me to do this, because both return void. Rather, they should both return a Polynomial& (and be *this).

A fifth suggestion, which I'm sure you aren't allowed to do, but just in case... use std::vector<> rather than a custom linked list to hold the coefficients; this simplifies traversal of the coefficients and eliminates all the memory management you have to deal with by using a custom linked list implementation.

A sixth suggestion, in case the previous one won't work for you: write a dedicated linked list class and have Polynomial contain one. Now the linked list class deals with the memory management and Polynomial deals with adding coefficients. As it stands now, Polynomial has to do both.
Topic archived. No new replies allowed.