Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views15 pages

Polynomial Addition Using Linked List-New List

The document explains the use of linked lists for polynomial representation and addition, highlighting advantages such as efficient memory usage and ease of term manipulation. It provides an example of polynomial addition, detailing an algorithm that traverses and combines terms based on their exponents. The document also includes code snippets for inserting terms and adding two polynomials using linked lists.

Uploaded by

namrata.paropate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

Polynomial Addition Using Linked List-New List

The document explains the use of linked lists for polynomial representation and addition, highlighting advantages such as efficient memory usage and ease of term manipulation. It provides an example of polynomial addition, detailing an algorithm that traverses and combines terms based on their exponents. The document also includes code snippets for inserting terms and adding two polynomials using linked lists.

Uploaded by

namrata.paropate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

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>
// Insert term at end (to keep polynomial sorted in
#include <stdlib.h> descending powers)
struct node { void insertTerm(struct node** poly, int coeff, int pow)
int coeff; {
int pow; struct node* newNode = createNode(coeff, pow);
struct node *next; if (*poly == NULL) {
}; *poly = newNode;
struct node* createNode(int coeff, int pow) { } else {
struct node* newNode = (struct struct node* temp = *poly;
node*)malloc(sizeof(struct node)); while (temp->next != NULL)
newNode->coeff = coeff; temp = temp->next;
newNode->pow = pow; temp->next = newNode;
newNode->next = NULL; }
return newNode; }
}
// Add two polynomials and return result polynomial
struct node* addPolynomials(struct node* poly1, struct node*
// If any terms left in poly1
poly2) {
struct node* result = NULL; while (p1 != NULL) {
struct node *p1 = poly1, *p2 = poly2; insertTerm(&result, p1->coeff, p1->pow);
p1 = p1->next;
while (p1 != NULL && p2 != NULL) {
}
if (p1->pow == p2->pow) {
// If any terms left in poly2
insertTerm(&result, p1->coeff + p2->coeff, p1->pow);
p1 = p1->next; while (p2 != NULL) {
p2 = p2->next; insertTerm(&result, p2->coeff, p2->pow);
} p2 = p2->next;
else if (p1->pow > p2->pow) {
}
insertTerm(&result, p1->coeff, p1->pow);
return result;
p1 = p1->next;
} }
else {
insertTerm(&result, p2->coeff, p2->pow);
p2 = p2->next;
}
// Display polynomial // First polynomial: 7x^3 + 13x^2 + 2
void display(struct node* poly) { insertTerm(&poly1, 7, 3);
while (poly != NULL) { insertTerm(&poly1, 13, 2);
printf("%dx^%d", poly->coeff, poly->pow); insertTerm(&poly1, 2, 0);
poly = poly->next; // Second polynomial: 3x^3 + 2x + 1
if (poly != NULL) insertTerm(&poly2, 3, 3);
printf(" + "); insertTerm(&poly2, 2, 1);
} insertTerm(&poly2, 1, 0);
printf("\n"); printf("First Polynomial: ");
} display(poly1);
int main() { printf("Second Polynomial: ");
struct node* poly1 = NULL; display(poly2);
struct node* poly2 = NULL; result = addPolynomials(poly1, poly2);
struct node* result = NULL; printf("Resultant Polynomial after Addition: ");
insertTerm(&poly1, 7, 3); // First polynomial: 7x^3 + 13x^2 + 2 display(result);
insertTerm(&poly1, 13, 2); return 0;
insertTerm(&poly1, 2, 0); }

You might also like