C Implementation: Circular Linked List for Polynomials

C Implementation: 3D Polynomials with Circular Linked Lists

This code defines structures and functions to manage multivariate polynomials (in terms of $x, y, z$) using a circular linked list to store individual terms.

Data Structure Definition

struct node {
    int coeff;
    int x_exp, y_exp, z_exp;
    struct node *next;
};

A pointer to the first term is initialized:

struct node *poly1 = NULL;

Term Insertion Function

insertTerm Function

Inserts a new term into the circular linked list. If the list is empty, the new node becomes the head and points to itself. Otherwise, it is appended before the head.

struct node* insertTerm(int coeff, int x_exp, int y_exp, int z_exp, struct node *poly) {
    struct node *newTerm = (struct node *)malloc(sizeof(struct node));
    newTerm->coeff = coeff;
    newTerm->x_exp = x_exp;
    newTerm->y_exp = y_exp;
    newTerm->z_exp = z_exp;

    if (poly == NULL) {
        poly = newTerm;
        newTerm->next = poly;
    } else {
        struct node *temp = poly;
        while (temp->next != poly) {
            temp = temp->next;
        }
        temp->next = newTerm;
        newTerm->next = poly;
    }
    return poly;
}

Polynomial Operations

displayPolynomial Function

Prints all terms of the polynomial in the format: +/coeffx^x_expy^y_expz^z_exp.

void displayPolynomial(struct node *poly) {
    if (poly == NULL) {
        printf("Polynomial is empty.\n");
        return;
    }
    struct node *temp = poly;
    do {
        printf("%+dx^%dy^%dz^%d ", temp->coeff, temp->x_exp, temp->y_exp, temp->z_exp);
        temp = temp->next;
    } while (temp != poly);
    printf("\n");
}

evaluatePolynomial Function

Calculates the value of the polynomial $P(x, y, z)$ for given inputs $x, y, z$.

void evaluatePolynomial(int x, int y, int z, struct node *poly) {
    if (poly == NULL) {
        printf("Polynomial is empty.\n");
        return;
    }
    struct node *temp = poly;
    int result = 0;
    do {
        int termValue = temp->coeff;
        for (int i = 0; i < temp->x_exp; i++) termValue *= x;
        for (int i = 0; i < temp->y_exp; i++) termValue *= y;
        for (int i = 0; i < temp->z_exp; i++) termValue *= z;
        result += termValue;
        temp = temp->next;
    } while (temp != poly);
    printf("The value of the polynomial is: %d\n", result);
}

addPolynomials Function

Adds two polynomials ($ ext{poly1} + ext{poly2}$). Terms with matching exponents are summed; unmatched terms are inserted.

struct node* addPolynomials(struct node *poly1, struct node *poly2) {
    struct node *polySum = NULL;
    struct node *temp1 = poly1;
    struct node *temp2 = poly2;

    // Insert all terms from poly1 into polySum
    do {
        polySum = insertTerm(temp1->coeff, temp1->x_exp, temp1->y_exp, temp1->z_exp, polySum);
        temp1 = temp1->next;
    } while (temp1 != poly1);

    // Process terms from poly2
    do {
        struct node *search = polySum;
        int found = 0;
        do {
            if (temp2->x_exp == search->x_exp && temp2->y_exp == search->y_exp && temp2->z_exp == search->z_exp) {
                search->coeff += temp2->coeff;
                found = 1;
                break;
            }
            search = search->next;
        } while (search != polySum);

        if (!found) {
            polySum = insertTerm(temp2->coeff, temp2->x_exp, temp2->y_exp, temp2->z_exp, polySum);
        }
        temp2 = temp2->next;
    } while (temp2 != poly2);

    return polySum;
}

Main Execution Flow

main Function

Sets up an interactive menu for testing the polynomial operations.

int main() {
    int choice, x, y, z;
    struct node *poly2 = NULL;

    // Initialize poly1
    poly1 = insertTerm(6, 2, 2, 1, poly1);   // 6x^2y^2z^1
    poly1 = insertTerm(-4, 0, 1, 5, poly1);  // -4x^0y^1z^5
    poly1 = insertTerm(3, 3, 1, 1, poly1);   // 3x^3y^1z^1

    while (1) {
        printf("\nMenu:\n");
        printf("1. Display Polynomial P(x, y, z)\n");
        printf("2. Evaluate Polynomial P(x, y, z)\n");
        printf("3. Add Polynomial to another Polynomial\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Polynomial P(x, y, z):\n");
                displayPolynomial(poly1);
                break;
            case 2:
                printf("Enter the values of x, y, z: ");
                scanf("%d%d%d", &x, &y, &z);
                evaluatePolynomial(x, y, z, poly1);
                break;
            case 3: {
                poly2 = NULL;
                printf("\nInserting terms for POLY2\n");
                poly2 = insertTerm(2, 1, 5, 1, poly2);   // 2x^1y^5z^1
                poly2 = insertTerm(-2, 1, 1, 3, poly2);  // -2x^1y^1z^3

                printf("POLY1:\n");
                displayPolynomial(poly1);
                printf("POLY2:\n");
                displayPolynomial(poly2);

                printf("POLYSUM:\n");
                struct node* polySum = addPolynomials(poly1, poly2);
                displayPolynomial(polySum);
                break;
            }
            case 4:
                exit(0);
            default:
                printf("Invalid choice.\n");
        }
    }
    return 0;
}