0% found this document useful (0 votes)
49 views6 pages

Representing A Polynomial Using A Linked List

The document discusses representing polynomials using linked lists. It explains that linked lists make it easier to perform operations like addition and multiplication compared to arrays. It provides an example of adding two polynomials represented as linked lists by traversing the lists and either appending unmatched terms or adding coefficients of matching terms to the resulting list.

Uploaded by

Balaji Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views6 pages

Representing A Polynomial Using A Linked List

The document discusses representing polynomials using linked lists. It explains that linked lists make it easier to perform operations like addition and multiplication compared to arrays. It provides an example of adding two polynomials represented as linked lists by traversing the lists and either appending unmatched terms or adding coefficients of matching terms to the resulting list.

Uploaded by

Balaji Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Representing a polynomial using a linked

list
A polynomial can be represented in an array or in a linked
list by simply storing the coefficient and exponent of each
term.
However, for any polynomial operation , such as addition
or multiplication of polynomials , you will find that the
linked list representation is more easier to deal with.
First of all note that in a polynomial all the terms may not
be present, especially if it is going to be a very high order
polynomial. Consider

5 x12 + 2 x9 + 4x7 + 6x5 + x2 + 12 x

Now this 12th order polynomial does not have all the 13
terms (including the constant term).
It would be very easy to represent the polynomial using a
linked list structure, where each node can hold information
pertaining to a single term of the polynomial.

Each node will need to store


the variable x,
the exponent and
the coefficient for each term.
It often does not matter whether the polynomial is in x or y.
This information may not be very crucial for the intended
operations on the polynomial.

Thus we need to define a node structure to hold two


integers , viz. exp and coff

Compare this representation with storing the same


polynomial using an array structure.
In the array we have to have keep a slot for each exponent
of x,
thus if we have a polynomial of order 50 but containing
just 6 terms, then a large number of entries will be zero in
the array.

You will also see that it would be also easy to manipulate a


pair of polynomials if they are represented using linked
lists.

Addition of two polynomials

Consider addition of the following polynomials

5 x12 + 2 x9 + 4x7 + 6x6 + x3

7 x8 + 2 x7 + 8x6 + 6x4 + 2x2 + 3 x + 40

The resulting polynomial is going to be

5 x12 + 2 x9 + 7 x8 + 6 x7 + 14x6 + 6x4 +x3


2x2 + 3 x + 40

Now notice how the addition was carried out. Let us say the
result of addition is going to be stored in a third list.
We started with the highest power in any polynomial.

If there was no item having same exponent , we simply


appended the term to the new list, and continued with the
process.

Wherever we found that the exponents were matching, we


simply added the coefficients and then stored the term in
the new list.

If one list gets exhausted earlier and the other list still
contains some lower order terms, then simply append the
remaining terms to the new list.

Now we are in a position to write our algorithm for adding


two polynomials.

Let phead1 , phead2 and phead3 represent the pointers of


the three lists under consideration.

Let each node contain two integers exp and coff .

Let us assume that the two linked lists already contain


relevant data about the two polynomials.

Also assume that we have got a function append to insert a


new node at the end of the given list.
p1 = phead1;
p2 = phead2;

Let us call malloc to create a new node p3 to build the


third list
p3 = phead3;

/* now traverse the lists till one list gets exhausted */

while ((p1 != NULL) || (p2 != NULL))


{

/ * if the exponent of p1 is higher than that of p2 then


the next term in final list is going to be the node of p1* /

while (p1 ->exp > p2 -> exp )


{
p3 -> exp = p1 -> exp;
p3 -> coff = p1 -> coff ;
append (p3, phead3);

/* now move to the next term in list 1*/

p1 = p1 -> next;
}

/ * if p2 exponent turns out to be higher then make p3


same as p2 and append to final list * /
while (p1 ->exp < p2 -> exp )
{
p3 -> exp = p2 -> exp;
p3 -> coff = p2 -> coff ;
append (p3, phead3);
p2 = p2 -> next;
}

/* now consider the possibility that both exponents are


same , then we must add the coefficients to get the term for
the final list */

while (p1 ->exp = p2 -> exp )


{
p3-> exp = p1-> exp;
p3->coff = p1->coff + p2-> coff ;
append (p3, phead3) ;
p1 = p1->next ;
p2 = p2->next ;
}
}

/* now consider the possibility that list2 gets exhausted ,


and there are terms remaining only in list1. So all those
terms have to be appended to end of list3. However, you do
not have to do it term by term, as p1 is already pointing to
remaining terms, so simply append the pointer p1 to phead3
*/

if ( p1 != NULL)
append (p1, phead3) ;
else
append (p2, phead3);

Now, you can implement the algorithm in C, and maybe


make it more efficient.

You might also like