0% found this document useful (0 votes)
39 views4 pages

Polynomials

This document describes an algorithm for adding two polynomials represented as linked lists. It involves creating nodes for each term, inserting terms into linked lists to represent the polynomials, adding the polynomials by comparing exponents and inserting the results into a new linked list, and displaying the polynomials.

Uploaded by

Shanthi.V
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)
39 views4 pages

Polynomials

This document describes an algorithm for adding two polynomials represented as linked lists. It involves creating nodes for each term, inserting terms into linked lists to represent the polynomials, adding the polynomials by comparing exponents and inserting the results into a new linked list, and displaying the polynomials.

Uploaded by

Shanthi.V
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/ 4

Adding Two Polynomials

struct node
{
int Coeff;
int Exp;
struct node * link;
}

typedef struct node * NODE_PTR;

Algorithm Create_Node (int c, int e)


1. NODE_PTR Temp;
2. Temp = malloc (sizeof (struct node)
3. If (Temp = NULL)
4. Print (No Sufficient Memory)
5. Exit
6. Else
7. Temp->Coeff = c
8. Temp->Exp = e
9. Temp->link = NULL
10. Return Temp;
11. Exit

Algorithm Create_Polynomial ( )
1. NODE_PTR Start = NULL
2. Repeat Steps 3 to 18 while (New Term is added)
3. Read (Coefficient, c; and Exponent, e)
4. Cur = Create_Node (c, e)
5. Start = Insert_Term (Cur, Start)
6. Return Start
Algorithm Insert_Term (NODE_PTR Cur, Start)
1. NODE_PTR Cur, Prev, Next
2. If (Start = NULL or Cur->e < Start->Exp)
3. Cur->link = Start
4. Start = Cur
5. Else If (Start->link = NULL)
6. Start->link = Cur
7. Else
8. Prev = Start
9. Next = Start->link
10. Repeat Steps 14 and 15 while (Next ≠ NULL and e >
Next->Exp)
11. Prev = Next
12. Next = Next->link
13. Prev->link = Cur
14. Cur->link = Next
15. Return Start

Algorithm Add_Polynomials (P1, P2)


1. NODE_PTR P = NULL, Pnext, Temp
2. Repeat Steps 3 to 16 while (P1 ≠ NULL and P2 ≠ NULL)
3. If (P1->Exp > P2->Exp)
4. Temp->Coeff = P1->Coeff
5. Temp->Exp = P1->Exp
6. P1 = P1->link
7. Else If (P2->Exp > P1->Exp)
8. Temp->Coeff = P2->Coeff
9. Temp->Exp = P2->Exp
10. P1 = P1->link
11. Else
12. Temp->Coeff = P1->Coeff + P2->Coeff
13. Temp->Exp = P1->Exp
14. P1 = P1->link
15. P2 = P2->link
16. P = Insert_Term (Temp, P)
17. Repeat Steps 18 and 19 while (P1 ≠ NULL)
18. P = Insert_Term (P1, P)
19. P1 = P1->link
20. Repeat Steps 21 and 22 while (P2 ≠ NULL)
21. P = Insert_Term (P2, P)
22. P1 = P2->link
23. Return P
24. Exit

Algorithm Display (NODE_PTR Start)


1. NODE_PTR Cur
2. Cur = Start
3. Repeat Steps 4, 5 and 6 while (Cur ≠ NULL)
4. Print (Cur->Coeff)
5. Print (Cur->Exp)
6. Cur = Cur-> link
7. Exit

Algorithm Main ( )
1. NODE_PTR Poly1, Poly2, Poly
2. Poly1 = Create_Polynomial ( )
3. Poly2 = Create_Polynomial ( )
4. Poly = Add_polynomials (Poly1, Poly2)
5. Display (Poly1)
6. Display (Poly2)
7. Display (Poly3)
8. Exit

You might also like