Linked lists are well suited to polynomials, which may have many terms with gaps in their powers. Adding two polynomials is a classic example.
Representing a Polynomial
A polynomial such as 5x³ + 4x² + 2 is a sum of terms, each with a coefficient and an exponent. In a linked list, each node holds the coefficient, the exponent and a next pointer, and the nodes are kept in decreasing order of exponent. Only non-zero terms are stored, so x¹⁰⁰ + 1 needs just two nodes, while an array indexed by power would need 101 cells, most of them zero. The list can also grow as terms are added.
Definition: Polynomial node. A linked list node holding the coefficient and exponent of one term of a polynomial, together with a pointer to the next term.
Algorithm for Addition
Let p and q point to the first terms of the two polynomials, and let the result be an empty list. While both p and q are not NULL: if the exponent of p is greater than that of q, copy p's term to the result and move p; if it is smaller, copy q's term and move q; if they are equal, add the coefficients, and if the sum is not zero, add a term with that sum and exponent, then move both p and q. When one list ends, copy the remaining terms of the other. The algorithm is like merging two sorted lists, and it takes O(m + n) time for polynomials with m and n terms.
Other Polynomial Operations
Subtraction is the same with the coefficients of the second polynomial negated. Multiplication multiplies every term of one by every term of the other, adding coefficients of like powers, taking O(mn). Evaluation at a value of x adds coefficient × x^exponent for each term.
Table 2.4: Array and list for polynomials
| Feature | Array by power | Linked list of terms |
|---|---|---|
| Storage | One cell per power | One node per non-zero term |
| x¹⁰⁰ + 1 | 101 cells | 2 nodes |
| Adding terms | Fixed size | Grows freely |
| Addition | O(max degree) | O(m + n) |
Example 2.4: Adding two polynomials
Problem: Add P = 5x³ + 4x² + 2 and Q = 3x³ + 2x + 7.
Solution:
- Exponents 3 and 3 are equal: 5 + 3 = 8, giving 8x³.
- P has 4x², Q has 2x. 2 > 1, so copy 4x².
- Q has 2x, P has 2 (power 0). 1 > 0, so copy 2x.
- Both have constants: 2 + 7 = 9.
Result: P + Q = 8x³ + 4x² + 2x + 9.
#include <stdio.h>
#include <stdlib.h>
struct term { int c, e; struct term *next; };
struct term *t(int c, int e, struct term *nx) {
struct term *p = malloc(sizeof *p);
p->c = c; p->e = e; p->next = nx; return p;
}
void append(struct term **tail, int c, int e) {
*tail = (*tail)->next = t(c, e, NULL);
}
int main(void) {
struct term *p = t(5, 3, t(4, 2, t(2, 0, NULL)));
struct term *q = t(3, 3, t(2, 1, t(7, 0, NULL)));
struct term head = {0, 0, NULL}, *tail = &head;
while (p && q) {
if (p->e > q->e) {
append(&tail, p->c, p->e); p = p->next;
} else if (p->e < q->e) {
append(&tail, q->c, q->e); q = q->next;
}
else {
if (p->c + q->c) append(&tail, p->c + q->c, p->e);
p = p->next; q = q->next;
}
}
for (; p; p = p->next) append(&tail, p->c, p->e);
for (; q; q = q->next) append(&tail, q->c, q->e);
for (struct term *r = head.next; r; r = r->next)
printf("%+dx^%d ", r->c, r->e);
printf("\n");
return 0;
}
Output
+8x^3 +4x^2 +2x^1 +9x^0
Common mistakes
- Not keeping the terms in decreasing order of exponent, which breaks the merge.
- Keeping a term whose sum of coefficients is zero.
- Forgetting to copy the remaining terms when one polynomial ends.
Key takeaways
- Each node holds a coefficient and an exponent.
- Terms are kept in decreasing order of exponent.
- Addition works like merging two sorted lists, in O(m + n).
- Equal exponents: add coefficients and drop zero results.
Practice questions
- Why is a linked list good for polynomials?
- What happens when two terms have the same exponent?
- What is the time to add polynomials of m and n terms?
Answers to the practice questions
- It stores only non-zero terms and grows as needed.
- Their coefficients are added, and the term is kept if the sum is not zero.
- O(m + n).
Sources
- Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit II
