Write an algorithm to add two polynomials using linked list.
Ans:
Algorithm to add two polynomials using linked list is as follows:-
Let p and q be the two polynomials represented by linked lists
1. while p and q are not null, repeat step 2.
2. If powers of the two terms ate equal
then if the terms do not cancel
then insert the sum of the terms into the sum Polynomial
Advance p
Advance q
Else if the power of the first polynomial> power of second
Then insert the term from first polynomial into sum polynomial
Advance p
Else insert the term from second polynomial into sum polynomial
Advance q
3. copy the remaining terms from the non empty polynomial into the
sum polynomial.
The third step of the algorithm is to be processed till the end of the polynomials has
not been reached.
No comments:
Post a Comment