Polynomials using linked lists
Each term becomes a node (coef, expon, link); add two polynomials by walking both lists together.
Syllabus says: “Polynomial representation using Linked List”
On this page · 13 sections
Same merge rule as arrays: BIGGER exponent goes first, EQUAL exponents add, ZERO sum is skipped — just with links instead of indices.
Two lines of students sorted by height walking through one gate: the taller one enters first, twins enter together.
Each node stores a coefficient, an exponent and a link; polynomial addition walks both lists in decreasing exponent order, adding coefficients of equal exponents, in O(m + n).
What you will be able to do
- Represent a polynomial as a linked list.
- Write the addition algorithm (KTU Nov 2025 Q11a, model paper Q12a).
Representation
struct term {
int coef;
int expon;
struct term *link;
};Worked example
| p (A) | q (B) | Action | Result |
|---|---|---|---|
| 2x³ | 4x³ | equal → 2 + 4 = 6 | 6x³ |
| 5x² | 2x² | equal → 5 + 2 = 7 | 6x³ + 7x² |
| −4x | 3x | equal → −4 + 3 = −1 | 6x³ + 7x² − x |
| 5 | −6 | equal → 5 − 6 = −1 | 6x³ + 7x² − x − 1 |
Now watch it run
Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶. Keys: Space play/pause, ← → step.
Algorithm: add two polynomials
Algorithm PolyAdd(A, B)
1. p ← A; q ← B; C ← NULL
2. while p ≠ NULL and q ≠ NULL do
3. if p→expon > q→expon then attach(p→coef, p→expon, C); p ← p→link
4. else if p→expon < q→expon then attach(q→coef, q→expon, C); q ← q→link
5. else
6. sum ← p→coef + q→coef
7. if sum ≠ 0 then attach(sum, p→expon, C)
8. p ← p→link; q ← q→link
9. while p ≠ NULL do attach(p→coef, p→expon, C); p ← p→link
10. while q ≠ NULL do attach(q→coef, q→expon, C); q ← q→link
11. return C
// attach(c, e, C): create a node (c, e) and add it at the END of C (keep a rear pointer)C implementation
polynomial_linked_list.ctested ✓ 200/200 cases/* Polynomial addition using linked lists — KTU PCCST303, Module 2 (KTU model Q12a, Nov 2025 Q11a)
* Each node holds one term: coef, exp and a link to the next (smaller-exponent) term.
* Input : nA coef exp ... nB coef exp ... (decreasing exponents)
* Output: "SUM: coef exp coef exp ..." */
#include <stdio.h>
#include <stdlib.h>
struct node {
int coef, exp;
struct node *next;
};
/* append a term at the end of the list whose last node is *tail */
void attach(int coef, int exp, struct node **head, struct node **tail) {
struct node *n = malloc(sizeof *n);
if (n == NULL) { printf("OUT OF MEMORY\n"); exit(1); }
n->coef = coef; n->exp = exp; n->next = NULL;
if (*head == NULL) *head = n; else (*tail)->next = n;
*tail = n;
}
struct node *readPoly(int *ok) {
struct node *head = NULL, *tail = NULL;
int n, c, e;
if (scanf("%d", &n) != 1 || n < 0) { *ok = 0; return NULL; }
while (n-- > 0) {
if (scanf("%d %d", &c, &e) != 2) { *ok = 0; break; }
attach(c, e, &head, &tail);
}
return head;
}
struct node *addPoly(struct node *p, struct node *q) {
struct node *head = NULL, *tail = NULL;
while (p != NULL && q != NULL) {
if (p->exp > q->exp) { attach(p->coef, p->exp, &head, &tail); p = p->next; }
else if (p->exp < q->exp) { attach(q->coef, q->exp, &head, &tail); q = q->next; }
else {
int sum = p->coef + q->coef;
if (sum != 0) attach(sum, p->exp, &head, &tail);
p = p->next; q = q->next;
}
}
for (; p != NULL; p = p->next) attach(p->coef, p->exp, &head, &tail);
for (; q != NULL; q = q->next) attach(q->coef, q->exp, &head, &tail);
return head;
}
void freeList(struct node *p) { while (p != NULL) { struct node *t = p; p = p->next; free(t); } }
int main(void) {
int ok = 1;
struct node *a = readPoly(&ok), *b = readPoly(&ok), *c, *p;
if (!ok) { printf("BAD INPUT\n"); freeList(a); freeList(b); return 1; }
c = addPoly(a, b);
printf("SUM:");
for (p = c; p != NULL; p = p->next) printf(" %d %d", p->coef, p->exp);
printf("\n");
freeList(a); freeList(b); freeList(c);
return 0;
}
Sample run · model Q12a · real output from gcc
Input (stdin)
4 2 3 5 2 -4 1 5 0 4 4 3 2 2 3 1 -6 0
Output (stdout)
SUM: 6 3 7 2 -1 1 -1 0
Complexity
O(m + n) time for m and n terms; each comparison consumes at least one term. No wasted space for missing powers.
Common mistakes
- Adding coefficients of different exponents.
- Keeping a node with coefficient 0.
- Forgetting the leftover terms of the longer polynomial.
Asked in KTU exams (2)
Quick check — can you get them all?
Practice
Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.
Exam-ready summary
- Node: coef, expon, link; terms in decreasing exponent order.
- Compare exponents: bigger → copy; equal → add (skip if 0); copy leftovers.
- O(m + n).
References
- Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — §4.4 (Polynomials)
Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.
Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)