Comp Sci Implement Polynom in C++ w/o Linked List

  • Thread starter Thread starter erezb84
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
To implement polynomials in C++ without using linked lists, one approach is to create a custom linked list structure where each node contains the term factor and the exponent. It is recommended to maintain the order of terms from x^0 to x^n for easier addition and comparison of polynomials. When adding a new term, it should be inserted in the correct position to preserve this order. While zero terms can be included for future manipulation, they are not strictly necessary. This method facilitates the merging of two polynomials into a new polynomial efficiently.
erezb84
Messages
43
Reaction score
0
Hi,
I need to implement polynoms in c++.
I thought of doing this with lined list,i am not alowed using c++ linked list, so i will built it myself.
I started thinking of adding and substructing,
should i first sort the polynoms i want to sum by its exponents? (in this case it will be easerear to compare between 2 exponents),
or is there a way doing the adding without sorting?...

Thanks!
 
Physics news on Phys.org
Try using a simple array of double with the index being the power.
 
The instructions were to use a linked list...
 
erezb84 said:
The instructions were to use a linked list...

Sorry I saw your comment that said you weren't allowed to use a linked list I took to mean you weren't allowed to write one either.

Okay so write one with content of the node containing the term factor and the power of the independent variable. Add nodes to the list in the order of the polynomial from x0 to xn.
 
In order to make the adding easier i need to save the polynom in this order right?
so if i need to add to a given polynom another node (with new coefficient and exponent) i nedd to insert it on the right place?
 
Yes by preserving the order you easily print or compare or evaluate a polynomial. If you want to add two polynomials together you'd create a new polynomial by merging the two.

You might also wan to put in zero terms as well as placeholders for future manipulation of the polynomial but that's not necessary. Zero term is like x0 + 0x1 + 3x2 ... For 3x^2 + 1
 
Ok,
Many Thanks!
 

Similar threads

Replies
2
Views
1K
Replies
2
Views
4K
Replies
5
Views
3K
Replies
3
Views
2K
Replies
10
Views
2K
Replies
3
Views
2K
Replies
2
Views
2K
Back
Top