C program to add two polynomials using linked list

Welcome to nkcoderz.com In this article we will discuss how to create a c program to add two polynomials using linked list.

Code for c program to add two polynomials using linked list

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int coefficient;
    int exponent;
    struct Node *next;
};

struct Node *createNode(int coefficient, int exponent) {
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->coefficient = coefficient;
    newNode->exponent = exponent;
    newNode->next = NULL;
    return newNode;
}

struct Node *insertAtEnd(struct Node *head, int coefficient, int exponent) {
    struct Node *newNode = createNode(coefficient, exponent);
    if (head == NULL) {
        return newNode;
    }
    struct Node *current = head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = newNode;
    return head;
}

struct Node *addPolynomials(struct Node *p1, struct Node *p2) {
    struct Node *result = NULL;
    while (p1 != NULL && p2 != NULL) {
        if (p1->exponent > p2->exponent) {
            result = insertAtEnd(result, p1->coefficient, p1->exponent);
            p1 = p1->next;
        } else if (p2->exponent > p1->exponent) {
            result = insertAtEnd(result, p2->coefficient, p2->exponent);
            p2 = p2->next;
        } else {
            result = insertAtEnd(result, p1->coefficient + p2->coefficient, p1->exponent);
            p1 = p1->next;
            p2 = p2->next;
        }
    }
    while (p1 != NULL) {
        result = insertAtEnd(result, p1->coefficient, p1->exponent);
        p1 = p1->next;
    }
    while (p2 != NULL) {
        result = insertAtEnd(result, p2->coefficient, p2->exponent);
        p2 = p2->next;
    }
    return result;
}

void displayPolynomial(struct Node *head) {
    while (head != NULL) {
        printf("%dx^%d", head->coefficient, head->exponent);
        head = head->next;
        if (head != NULL) {
            printf(" + ");
        }
    }
    printf("\n");
}

int main() {
    struct Node *p1 = NULL, *p2 = NULL, *result = NULL;
    p1 = insertAtEnd(p1, 2, 0);
    p1 = insertAtEnd(p1, 3, 1);
    p1 = insertAtEnd(p1, 4, 2);

    p2 = insertAtEnd(p2, 5, 0);
    p2 = insertAtEnd(p2, 6, 1);
    p2 = insertAtEnd(p2, 7, 2);

    printf("Polynomial 1: ");
    displayPolynomial(p1);
    printf("Polynomial 2: ");
    displayPolynomial(p2);

    result = addPolynomials(p1, p2);
    printf("Result: ");
    displayPolynomial(result);
    return 0;
}

Output

Polynomial 1: 2x^0 + 3x^1 + 4x^2
Polynomial 2: 5x^0 + 6x^1 + 7x^2
Result: 7x^0 + 9x^1 + 11x^2

Explanation

In this implementation, the struct Node represents a single term in a polynomial, with a coefficient and an exponent as its properties. The createNode function creates a new node with the given coefficient and exponent, and the insertAtEnd function inserts a new node at the end of the linked list.

The addPolynomials function takes two polynomials as input and returns a new polynomial that is the sum of the input polynomials. The displayPolynomial function displays the polynomial in the form of ax^b + cx^d + ...

The main function creates two polynomials p1 and p2 by inserting nodes at the end of the linked list using the insertAtEnd function. The polynomials are then displayed using the displayPolynomial function.

Finally, the addPolynomials function is called to add the two polynomials, and the result is displayed using the displayPolynomial function.

The addPolynomials function uses two pointers p1 and p2 to traverse the two input polynomials. The function initializes a result polynomial to an empty linked list (represented by NULL).

The function then uses a while loop to traverse both polynomials, comparing the exponents of each term.

If the exponent of the term pointed to by p1 is greater than that of p2, the term is added to the result polynomial and p1 is advanced to the next term.

Similarly, if the exponent of the term pointed to by p2 is greater than that of p1, the term is added to the result polynomial and p2 is advanced to the next term. If the exponents are equal, the sum of the coefficients is added to the result polynomial, and both p1 and p2 are advanced to the next terms.

Once one of the input polynomials has been fully processed, the remaining terms of the other polynomial are added to the result polynomial.

The displayPolynomial function takes a polynomial as input and displays it in the form of ax^b + cx^d + ... by traversing the linked list and printing the coefficient and exponent of each term, separated by x^.

Conclusion

If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Share on:

NK Coderz is a Computer Science Portal. Here We’re Proving DSA, Free Courses, Leetcode Solutions, Programming Languages, Latest Tech Updates, Blog Posting Etc.

Leave a Comment