Problem with pointer addition?

My program is mostly written, but the hardest part for me is the addition of 2 polynomials which are in a linked list. I'm trying to compare the powers of polynomials in a linked list in order to see if I should add them together. I'm hopelessly lost on this subject and this is as far as I've gotten. I apologize for lack of comments, I usually go back through and do that after the program is written.

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
#include <iostream>
#include <cmath>
#include <cstddef>

using namespace std;

typedef float polyType;

struct polyInfo
{
	int number;
	int power;
	polyInfo* link;
};

//typedef polyInfo* polyPtr;
//polyPtr head;
//polyPtr currPtr;
//polyPtr newNodePtr;

class Polynomial
{
	private:
		polyInfo *head;
		int maxpow = 0;

	public:

		Polynomial()
		{
		}


		void createPoly(int n, int p)
		{
			polyInfo *temp = new polyInfo();
			temp->power = p;
			temp->number = n;
			temp->link = head;
			head = temp;
			if (maxpow < p)
			{
				maxpow = p;
			}
		
		}

		void getPoly()
		{
			polyInfo *temp = head;
			while (temp != NULL)
			{
				if (temp->number != 0)
				{
					cout << temp->number << "x^" << temp->power << " ";
				}
				temp = temp->link;
			}
			cout << "\n";
		}

		void addPoly()
		{
			polyInfo *temp = head;
			while (temp != NULL)
			{
				if (temp->number != 0)
				{
					polyInfo *temp2 = head;
					temp2 = temp2->link;
					if (temp2->link != NULL)
					{
						if (temp->power == temp2->power)
						{
							temp->number = (temp->number)+(temp2->number);
							temp2->number = 0;
							temp = temp->link;

						}
					}
					else
					{
						temp = NULL;
					}
				}
			}

		
		}

		void subtractPoly()
		{

		}


};


int main()
{
	int menuNum;//used to see which menu option user chooses
	int mainPower;
	int mainNumber;

	Polynomial mainPoly;
	bool menu = true;

	while (menu)
	{
		cout << "Enter 1 to create new polynomial"
			<< "\nEnter 2 to read a polynomial"
			<< "\nEnter 3 to add polynomials"
			<< "\nEnter 4 to subtract polynomials"
			<< "\nEnter 5 to end program :";

		cin >> menuNum;

		switch(menuNum)
		{
			case 1:
				cout << "\nEnter the coefficient: ";
				cin >> mainNumber;
				cout << "\nEnter the exponent of X: ";
				cin >> mainPower;
				mainPoly.createPoly(mainNumber,mainPower);
				break;
			case 2:
				mainPoly.getPoly();
				break;
			case 3:
				mainPoly.addPoly();
				break;
			case 4:
				mainPoly.subtractPoly();
				break;
			case 5:
				cout << "End of program\n";
				menu = false;
			default:
				cout << "\nInvalid Input, please try again\n\n";
				break;
		}
	}

	return 0;

}
this may take some rewriting but one easy solution is to do it inverted and zeroed.
that is, say you have this:

2x^5 -3x^2

consider storing that as
0*x^0 (the constant term is x^0, that is, if you had +3 that is 3*(x^0) )
+
0* x^1 (the X term)
+
-3 x^2 (a nonzero term at last)
+
0 x^4
+
2x^5
(stop here, but if you had more terms, you can add them, the last one is the highest power and all the rest are assumed zeroed out)

so for that 2 term polynomial you have 5 nodes in your linked list. Wasteful, but now you can do this:
first node of list 1 + first node of list 2 (they might both be zero, but its valid)
second node of list 1 + second node of list 2 ...

etc

that is, wasting space to make it simple to code.

To do that I would have to make a second list, but I am supposed to only have one linked list which contains all of the terms. It was suggested to go down the list's power section and compare every term and add if equal to each other, but I can't get that down..
Are you sure?? I would expect class poly to hold 1 polynomial. You would have 2 variables of the class, so you can add or multiply or something 2 of them together. If each of them is stored as I said, that becomes trivial. If you prefer to do it by comparing the power number, you can do that too, but you have to sync the list iteration checking to get it right. eg... (powers only)

the algorithm do do it by powers is:

go to first element in both lists for the 2 polynomials.
start:
are the powers equal? If yes, perform the addition (for example) and move both lists to the next node.
if no, choose the lowest (or highest) power (pick one, work from high to low or low to high but low to high seems safer/easier to me). so you pick the lowest, and add that to the result, and iterate ONLY the list that held the lowest one to its next value.
goto start:

(repeat until both lists have reached their null/terminal/final value and consumed all the terms. )

If you do it the way I said, you just increment both lists and add each time, at the cost of extra storage space. Either way will work.

If you want to store multiple polys in one instance of the class, I don't understand your code. And, I will argue it all day, doing it all in a single list is just plain wrong. I can show you a way to do it, but its still an unmitigated hack to do it that way. If you insist, I would do it AS IF 2 lists by having a pointer to the first poly (the head of the list) and a pointer to the second poly (somewhere in the middle of your big meta list). Then to know when the end of the first poly is, you can either insert a dummy record or you can check that the next is not equal to the start of the second polynomial.



Last edited on
If you look at my code, I believe there is only one list. For each link, there is a number and power variable that should be stored together. I did not store them in separate lists. In order to do this I tried to make a temp that searched the first one and then a temp 2 that pointed ahead to the second one, but it did not do anything at all...
Topic archived. No new replies allowed.