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.
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 ...
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.
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...