Implement a linked list to represent polynomials and perform addition in C




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

// Structure for a node representing a term in the polynomial
struct Node {
    int coeff; // Coefficient of the term
    int exp; // Exponent of the term
    struct Node* next;
};

// Function to create a new node
struct Node* createNode(int coeff, int exp) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Memory allocation failed!\n");
        exit(1);
    }
    newNode->coeff = coeff;
    newNode->exp = exp;
    newNode->next = NULL;
    return newNode;
}

// Function to insert a term into the polynomial in descending order of exponents
void insertTerm(struct Node** head, int coeff, int exp) {
    // Skip terms with zero coefficient
    if (coeff == 0) {
        return;
    }
    struct Node* newNode = createNode(coeff, exp);
    // If list is empty or new term has higher exponent
    if (*head == NULL || (*head)->exp < exp) {
        newNode->next = *head;
        *head = newNode;
        return;
    }
    struct Node* current = *head;
    struct Node* prev = NULL;
    // Find the correct position to insert
    while (current != NULL && current->exp > exp) {
        prev = current;
        current = current->next;
    }
    // If term with same exponent exists, add coefficients
    if (current != NULL && current->exp == exp) {
        current->coeff += coeff;
        free(newNode); // Free the new node as it's not needed
        // If coefficient becomes zero, remove the term
        if (current->coeff == 0) {
            if (prev == NULL) {
                *head = current->next;
            } else {
                prev->next = current->next;
            }
            free(current);
        }
        return;
    }
    // Insert the new node
    newNode->next = current;
    if (prev == NULL) {
        *head = newNode;
    } else {
        prev->next = newNode;
    }
}

// Function to display the polynomial
void displayPolynomial(struct Node* head) {
    if (head == NULL) {
        printf("0\n");
        return;
    }
    struct Node* temp = head;
    while (temp != NULL) {
        // Print coefficient
        if (temp->coeff > 0 && temp != head) {
            printf(" + ");
        } else if (temp->coeff < 0) {
            printf(" - ");
        }
        // Print absolute value of coefficient
        if (abs(temp->coeff) != 1 || temp->exp == 0) {
            printf("%d", abs(temp->coeff));
        } else if (temp->coeff == -1 && temp->exp != 0) {
            printf("1");
        }
        // Print variable and exponent
        if (temp->exp > 0) {
            printf("x");
            if (temp->exp > 1) {
                printf("^%d", temp->exp);
            }
        }
        temp = temp->next;
    }
    printf("\n");
}

// Function to add two polynomials
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->exp > p2->exp) {
            insertTerm(&result, p1->coeff, p1->exp);
            p1 = p1->next;
        } else if (p1->exp < p2->exp) {
            insertTerm(&result, p2->coeff, p2->exp);
            p2 = p2->next;
        } else {
            // Add coefficients for same exponent
            int sumCoeff = p1->coeff + p2->coeff;
            if (sumCoeff != 0) {
                insertTerm(&result, sumCoeff, p1->exp);
            }
            p1 = p1->next;
            p2 = p2->next;
        }
    }
    // Add remaining terms from poly1
    while (p1 != NULL) {
        insertTerm(&result, p1->coeff, p1->exp);
        p1 = p1->next;
    }
    // Add remaining terms from poly2
    while (p2 != NULL) {
        insertTerm(&result, p2->coeff, p2->exp);
        p2 = p2->next;
    }
    return result;
}

// Main function to test polynomial addition
int main() {
    struct Node* poly1 = NULL;
    struct Node* poly2 = NULL;
    struct Node* result = NULL;

    // Create first polynomial: 3x^3 + 2x^2 + 1
    insertTerm(&poly1, 3, 3);
    insertTerm(&poly1, 2, 2);
    insertTerm(&poly1, 1, 0);

    // Create second polynomial: 4x^2 + 5x^1 + 2
    insertTerm(&poly2, 4, 2);
    insertTerm(&poly2, 5, 1);
    insertTerm(&poly2, 2, 0);

    // Display polynomials
    printf("Polynomial 1: ");
    displayPolynomial(poly1);
    printf(" polynomial 2: ");
    displayPolynomial(poly2);

    // Add polynomials
    result = addPolynomials(poly1, poly2);
    printf("Result of addition: ");
    displayPolynomial(result);

    return 0;
}

Output:


Comments