Lesson 11 +25 XP

Polynomial Representation & Arithmetic

Polynomial Representation & Arithmetic

A single-variable polynomial such as P(x) = 7x4 + 7x3 + 9x2 + 5x + 1 can be stored either in a dense array (index = exponent) or in a Linked List (storing only non-zero terms).

RepresentationBest ForMemory for 5x1000 + 1Addition Cost
ArrayDense polynomials (most terms non-zero)1001 integersO(max(deg P1, deg P2))
Linked ListSparse polynomials (large exponent gaps)2 nodesO(m + n) (m, n = non-zero terms)

1. Linked List Node Structure

Each node represents one non-zero term (coeff, exp) sorted in descending order of exp:

C
struct PolyNode {
    int coeff;
    int exp;
    struct PolyNode *next;
};

2. Polynomial Addition & Subtraction (Two-Pointer Merge)

Given P1 (m terms) and P2 (n terms) sorted by descending exp:

  • Walk p1 and p2:
  • p1->exp > p2->exp: append (p1->coeff, p1->exp), advance p1.
  • p1->exp < p2->exp: append (±p2->coeff, p2->exp) (negated for subtraction), advance p2.
  • p1->exp == p2->exp: compute c = p1->coeff ± p2->coeff; if c != 0, append (c, exp); advance both.
  • Worked Trace: Add P1 = 7x4 + 5x2 + 1 and P2 = 3x4 - 5x2 + 2x:
  • exp 4: 7 + 3 = 10 ⇒ 10x4
  • exp 2: 5 + (-5) = 0 (cancelled-omit!)
  • exp 1: 2x
  • exp 0: 1
  • Result: 10x4 + 2x + 1.
C
struct PolyNode* addPoly(struct PolyNode *p1, struct PolyNode *p2) {
    struct PolyNode dummy = {0, 0, NULL}, *tail = &dummy;
    while (p1 && p2) {
        if (p1->exp > p2->exp) { append(&tail, p1->coeff, p1->exp); p1 = p1->next; }
        else if (p1->exp < p2->exp) { append(&tail, p2->coeff, p2->exp); p2 = p2->next; }
        else {
            int sum = p1->coeff + p2->coeff;
            if (sum != 0) append(&tail, sum, p1->exp);
            p1 = p1->next; p2 = p2->next;
        }
    }
    while (p1) { append(&tail, p1->coeff, p1->exp); p1 = p1->next; }
    while (p2) { append(&tail, p2->coeff, p2->exp); p2 = p2->next; }
    return dummy.next;
}

3. Polynomial Multiplication

Multiply every term of P1 with every term of P2 (coeff1 * coeff2, exp1 + exp2) and insert into the result list in descending exp order, combining like exponents.

  • Example: (2x2 + 3) × (x + 4) = 2x3 + 8x2 + 3x + 12.
  • Complexity: O(m · n) term products; O(m · n · (m + n)) with sorted-list insertion.

Polynomial Addition with a Circular Header Linked List

The notes (and Lipschutz) store a polynomial as a circular list with a header node whose exponent is −1. The header acts as a sentinel: the loop stops when it comes back to the header, so there is no NULL check and an empty polynomial is simply a header pointing to itself.

P(x) = 3x^4 + 2x^2 + 5
 HEAD → [−, −1] → [3, 4] → [2, 2] → [5, 0] ─┐
   ↑────────────────────────────────────────┘

Because the header's exponent is −1 (smaller than any real exponent), once one list is exhausted its header never &quot;wins&quot; the comparison, so the remaining terms of the other list are copied automatically.

Trace: P = 3x⁴ + 2x² + 5, Q = 4x³ − 2x² + 1

  • 4 &gt; 3 → copy 3x⁴; 2 &lt; 3 → copy 4x³; 2 = 2 → 2 − 2 = 0, skip; 0 = 0 → 5 + 1 = 6.
  • P + Q = 3x⁴ + 4x³ + 6
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. How are Polynomials represented using a Linked List? Write a C function to add two polynomials. (7.5 Marks)
  • Write: Array vs Linked List comparison table, struct PolyNode { int coeff, exp; struct PolyNode *next; }, addPoly() C function, and a worked addition trace.
  • Q2. Explain Polynomial Subtraction and Multiplication using Linked Lists with an example. (5 Marks)
  • Write: Subtraction (negate second polynomial's coefficients during merge) and Multiplication (term-by-term product + like-exponent combining).