I've made a polynomial class using an array-based linked list, but my addition function isn't working. It seems to work if the 2nd polynomial only has one term, but not if it has more than one. I'm not sure if it is a problem with my add function or my insertTerm function.

The class uses a ListNode array.
 ``123456789101112`` ``````struct ListNode { /** A data item on the list. */ Polynomial_coefficient_type coefficient; Polynomial_exponent_type exponent; int next; }; // end ListNode ListNode array[50]; int head; // first used index int free;``````

`free` is the first unused index in the array.
The last used index in the list has a next item of '-1'.

This is a case in my switch statement in my main function, which creates the second polynomial and then adds it to the original one:
 ``123456789101112131415`` ``````case 6: cout << "\nEnter a term to create second polynomial."; do { cout << "\nEnter the new term's coefficient: "; cin >> a; cout << "Enter the new term's exponent: "; cin >> b; p2.insertTerm(a, b); cout << "Enter another term? (y/n): "; cin >> cont; } while (cont == 'y'); p1.add(p2); break;``````

My add function inserts terms from the second polynomial into the original polynomial. The polynomial is then simplified, which isn't shown:
 ``12345678910111213141516`` ``````void Polynomial::add(Polynomial b) { //if b is empty, original polynomial is itself if (!b.isEmpty()) { int bTemp = b.head; while (bTemp != -1) // '-1' is the 'next' item in the listnode of the last item in the list { insertTerm(b.array[bTemp].coefficient, b.array[bTemp].exponent); bTemp = b.array[bTemp].next; } } }``````

insertTerm:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778`` ``````void Polynomial::insertTerm(Polynomial_coefficient_type& newCo, Polynomial_exponent_type& newExpo) { if (newCo != 0) { array[free].coefficient = newCo; array[free].exponent = newExpo; if (free == head) // if list is empty { array[head].next = -1; ++free; } else // if list isn't empty { int prev = head; int cur = head; while (array[cur].next != -1 && array[cur].exponent > newExpo) { prev = cur; cur = array[cur].next; } if (cur == head && // 2nd item, at beginning array[cur].exponent < newExpo) { array[free].next = head; head = free; for (int i=0; i < 50; i++) if (array[i].next == -2) free = i; } else if (array[cur].next == -1) // insert at end { array[cur].next = free; array[free].next = -1; for (int i=0; i < 50; i++) { if (array[i].next == -2) { free = i; break; } } } else // insert in middle { array[free].next = cur; array[prev].next = free; for (int i=0; i < 50; i++) { if (array[i].next == -2) { free = i; break; } } } } int newLength = getLength() + 1; size = newLength; simplify(); } else { cout << "The coefficient is 0! " << "The term will not be added.\n"; } } // end insert ``````

Thanks for the help.
Last edited on
This is really a "linked list" in name only. Inserting a node takes O(n) time, and you're limited to 50 nodes. Why not allocate the nodes in the heap?
I've already created a working pointer-based polynomial linked list, now I'm working on this one. And there is a doubling function which copies the array to a new array that is double the size of the original array, if it gets full.
Last edited on
Topic archived. No new replies allowed.