Polynomial Addition using
linked list
Why Use Linked Lists for Polynomials?
• Polynomials have varying numbers of terms
• Efficient memory usage - only allocate space for existing terms
• Easy insertion and deletion of terms
• Natural representation: each term = one node
Polynomials Example:
3x² + 2x + 5 would be represented as:
[3,2] → [2,1] → [5,0] → NULL
struct node {
int coefficient;
int exponent;
struct Term* next;
};
• Polynomial Addition Algorithm
• Traverse both polynomials simultaneously
• Compare exponents of current terms:
• If equal: add coefficients, create new term
• If poly1 exponent > poly2 exponent: copy poly1 term
• If poly2 exponent > poly1 exponent: copy poly2 term
• Continue until both polynomials are exhausted
Example: Adding Polynomials
Polynomial 1: 3x² + 2x + 5
Polynomial 2: 4x³ + 2x² + x
Result: 4x³ + 5x² + 3x + 5
Advantages of Linked List Approach
• Efficient memory usage for sparse polynomials
• Easy to implement operations (addition, subtraction, multiplication)
• Dynamic - can handle polynomials of any size
• Simple to extend for multivariate polynomials
#include <stdio.h>
#include <stdlib.h>
struct node {
int coeff;
int pow;
struct node *next;
};
struct node* createNode(int coeff, int pow) {
struct node* newNode = (struct
node*)malloc(sizeof(struct node));
newNode->coeff = coeff;
newNode->pow = pow;
newNode->next = NULL;
return newNode;
}
// Insert term at end (to keep polynomial sorted in
descending powers)
void insertTerm(struct node** poly, int coeff, int pow)
{
struct node* newNode = createNode(coeff, pow);
if (*poly == NULL) {
*poly = newNode;
} else {
struct node* temp = *poly;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}
// Add two polynomials and return result polynomial
struct node* addPolynomials(struct node* poly1, struct node* poly2) {
struct node* result = NULL;
struct node *p1 = poly1, *p2 = poly2;
while (p1 != NULL && p2 != NULL) {
if (p1->pow == p2->pow) {
insertTerm(&result, p1->coeff + p2->coeff, p1->pow);
p1 = p1->next;
p2 = p2->next;
}
else if (p1->pow > p2->pow) {
insertTerm(&result, p1->coeff, p1->pow);
p1 = p1->next;
}
else {
insertTerm(&result, p2->coeff, p2->pow);
p2 = p2->next;
}
}
// If any terms left in poly1
while (p1 != NULL) {
insertTerm(&result, p1->coeff, p1->pow);
p1 = p1->next;
}
// If any terms left in poly2
while (p2 != NULL) {
insertTerm(&result, p2->coeff, p2->pow);
p2 = p2->next;
}
return result;
}
// Display polynomial
void display(struct node* poly) {
while (poly != NULL) {
printf("%dx^%d", poly->coeff, poly->pow);
poly = poly->next;
if (poly != NULL)
printf(" + ");
}
printf("n");
}
int main() {
struct node* poly1 = NULL;
struct node* poly2 = NULL;
struct node* result = NULL;
insertTerm(&poly1, 7, 3); // First polynomial: 7x^3 + 13x^2 + 2
insertTerm(&poly1, 13, 2);
insertTerm(&poly1, 2, 0);
// First polynomial: 7x^3 + 13x^2 + 2
insertTerm(&poly1, 7, 3);
insertTerm(&poly1, 13, 2);
insertTerm(&poly1, 2, 0);
// Second polynomial: 3x^3 + 2x + 1
insertTerm(&poly2, 3, 3);
insertTerm(&poly2, 2, 1);
insertTerm(&poly2, 1, 0);
printf("First Polynomial: ");
display(poly1);
printf("Second Polynomial: ");
display(poly2);
result = addPolynomials(poly1, poly2);
printf("Resultant Polynomial after Addition: ");
display(result);
return 0;
}
Polynomial Addition using linked list-new.pptx
Polynomial Addition using linked list-new.pptx
Polynomial Addition using linked list-new.pptx
Polynomial Addition using linked list-new.pptx
Polynomial Addition using linked list-new.pptx
Polynomial Addition using linked list-new.pptx
Polynomial Addition using linked list-new.pptx

Polynomial Addition using linked list-new.pptx

  • 1.
  • 2.
    Why Use LinkedLists for Polynomials? • Polynomials have varying numbers of terms • Efficient memory usage - only allocate space for existing terms • Easy insertion and deletion of terms • Natural representation: each term = one node
  • 3.
    Polynomials Example: 3x² +2x + 5 would be represented as: [3,2] → [2,1] → [5,0] → NULL struct node { int coefficient; int exponent; struct Term* next; };
  • 4.
    • Polynomial AdditionAlgorithm • Traverse both polynomials simultaneously • Compare exponents of current terms: • If equal: add coefficients, create new term • If poly1 exponent > poly2 exponent: copy poly1 term • If poly2 exponent > poly1 exponent: copy poly2 term • Continue until both polynomials are exhausted Example: Adding Polynomials Polynomial 1: 3x² + 2x + 5 Polynomial 2: 4x³ + 2x² + x Result: 4x³ + 5x² + 3x + 5
  • 5.
    Advantages of LinkedList Approach • Efficient memory usage for sparse polynomials • Easy to implement operations (addition, subtraction, multiplication) • Dynamic - can handle polynomials of any size • Simple to extend for multivariate polynomials
  • 6.
    #include <stdio.h> #include <stdlib.h> structnode { int coeff; int pow; struct node *next; }; struct node* createNode(int coeff, int pow) { struct node* newNode = (struct node*)malloc(sizeof(struct node)); newNode->coeff = coeff; newNode->pow = pow; newNode->next = NULL; return newNode; } // Insert term at end (to keep polynomial sorted in descending powers) void insertTerm(struct node** poly, int coeff, int pow) { struct node* newNode = createNode(coeff, pow); if (*poly == NULL) { *poly = newNode; } else { struct node* temp = *poly; while (temp->next != NULL) temp = temp->next; temp->next = newNode; } }
  • 7.
    // Add twopolynomials and return result polynomial struct node* addPolynomials(struct node* poly1, struct node* poly2) { struct node* result = NULL; struct node *p1 = poly1, *p2 = poly2; while (p1 != NULL && p2 != NULL) { if (p1->pow == p2->pow) { insertTerm(&result, p1->coeff + p2->coeff, p1->pow); p1 = p1->next; p2 = p2->next; } else if (p1->pow > p2->pow) { insertTerm(&result, p1->coeff, p1->pow); p1 = p1->next; } else { insertTerm(&result, p2->coeff, p2->pow); p2 = p2->next; } } // If any terms left in poly1 while (p1 != NULL) { insertTerm(&result, p1->coeff, p1->pow); p1 = p1->next; } // If any terms left in poly2 while (p2 != NULL) { insertTerm(&result, p2->coeff, p2->pow); p2 = p2->next; } return result; }
  • 8.
    // Display polynomial voiddisplay(struct node* poly) { while (poly != NULL) { printf("%dx^%d", poly->coeff, poly->pow); poly = poly->next; if (poly != NULL) printf(" + "); } printf("n"); } int main() { struct node* poly1 = NULL; struct node* poly2 = NULL; struct node* result = NULL; insertTerm(&poly1, 7, 3); // First polynomial: 7x^3 + 13x^2 + 2 insertTerm(&poly1, 13, 2); insertTerm(&poly1, 2, 0); // First polynomial: 7x^3 + 13x^2 + 2 insertTerm(&poly1, 7, 3); insertTerm(&poly1, 13, 2); insertTerm(&poly1, 2, 0); // Second polynomial: 3x^3 + 2x + 1 insertTerm(&poly2, 3, 3); insertTerm(&poly2, 2, 1); insertTerm(&poly2, 1, 0); printf("First Polynomial: "); display(poly1); printf("Second Polynomial: "); display(poly2); result = addPolynomials(poly1, poly2); printf("Resultant Polynomial after Addition: "); display(result); return 0; }