Tuesday, July 2, 2024

Representation of polynomials and the addition of two such polynomials using circular linked list: Source Code in C

C-program for the representation of polynomials using circular linked list and for the addition of two such polynomials.

/*This program covers the representation and addition of polynomials using circular linked lists, demonstrating dynamic memory allocation, circular linked list operations, and polynomial arithmetic.*/

#include <stdio.h>

#include <stdio.h>

#include <stdlib.h>


// Node structure for the circular linked list

struct Node {

    int coefficient;

    int exponent;

    struct Node* next;

};


// Function to create a new node

struct Node* createNode(int coefficient, int exponent) {

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->coefficient = coefficient;

    newNode->exponent = exponent;

    newNode->next = newNode; // Pointing to itself for circular nature

    return newNode;

}


// Function to insert a node in the circular linked list

struct Node* insertNode(struct Node* head, int coefficient, int exponent) {

    struct Node* newNode = createNode(coefficient, exponent);


    if (head == NULL) {

        return newNode;

    } else {

        struct Node* temp = head;

        while (temp->next != head) {

            temp = temp->next;

        }

        temp->next = newNode;

        newNode->next = head;

    }

    return head;

}


// Function to display the polynomial in descending order of exponents

void displayPolynomial(struct Node* head) {

    if (head == NULL) {

        printf("0\n");

        return;

    }


    struct Node* temp = head;

    do {

        if (temp->coefficient != 0) {

            if (temp != head && temp->coefficient > 0) {

                printf(" + ");

            }

            printf("%dx^%d", temp->coefficient, temp->exponent);

        }

        temp = temp->next;

    } while (temp != head);

    printf("\n");

}


// Function to add two polynomials

struct Node* addPolynomials(struct Node* poly1, struct Node* poly2) {

    struct Node* result = NULL;

    struct Node* temp1 = poly1;

    struct Node* temp2 = poly2;


    if (poly1 == NULL) return poly2;

    if (poly2 == NULL) return poly1;


    do {

        result = insertNode(result, temp1->coefficient, temp1->exponent);

        temp1 = temp1->next;

    } while (temp1 != poly1);


    do {

        struct Node* temp = result;

        int found = 0;

        do {

            if (temp->exponent == temp2->exponent) {

                temp->coefficient += temp2->coefficient;

                found = 1;

                break;

            }

            temp = temp->next;

        } while (temp != result);

        if (!found) {

            result = insertNode(result, temp2->coefficient, temp2->exponent);

        }

        temp2 = temp2->next;

    } while (temp2 != poly2);


    return result;

}


// Function to get polynomial input from the user in descending order of exponents

struct Node* inputPolynomial() {

    struct Node* poly = NULL;

    int coefficient, exponent, prevExponent = -1;

    char choice;


    do {

        int valid = 0;

        while (!valid) {

            printf("Enter coefficient: ");

            scanf("%d", &coefficient);

            printf("Enter exponent: ");

            scanf("%d", &exponent);

            if (prevExponent != -1 && exponent >= prevExponent) {

                printf("Error: Exponents must be entered in descending order.\n");

            } else {

                valid = 1;

            }

        }

        poly = insertNode(poly, coefficient, exponent);

        prevExponent = exponent;


        if (exponent == 0) {

            break;

        }


        printf("Do you want to add another term? (y/n): ");

        scanf(" %c", &choice);

    } while (choice == 'y' || choice == 'Y');


    return poly;

}


// Function to free the memory allocated for the polynomial

void freePolynomial(struct Node* head) {

    if (head == NULL) return;

    struct Node* temp = head;

    do {

        struct Node* next = temp->next;

        free(temp);

        temp = next;

    } while (temp != head);

}


int main() {

    struct Node* poly1 = NULL;

    struct Node* poly2 = NULL;

    struct Node* result = NULL;

    char choice;

    printf("****Representation and Addition of Polynomials using Circular Linked List****\n");

    printf("=============================================================================\n");

    do {

        printf("Enter the first polynomial:\n");

        poly1 = inputPolynomial();


        printf("\nEnter the second polynomial:\n");

        poly2 = inputPolynomial();


        printf("\nFirst Polynomial: ");

        displayPolynomial(poly1);


        printf("Second Polynomial: ");

        displayPolynomial(poly2);


        result = addPolynomials(poly1, poly2);


        printf("Resultant Polynomial after Addition: ");

        displayPolynomial(result);


        printf("\nDo you want to add another pair of polynomials? (y/n): ");

        scanf(" %c", &choice);


        // Free memory allocated for the polynomials to avoid memory leaks

        freePolynomial(poly1);

        freePolynomial(poly2);

        freePolynomial(result);


        poly1 = poly2 = result = NULL;

    } while (choice == 'y' || choice == 'Y');

    return 0;

}

Input/Output:👍


****Representation and Addition of Polynomials using Circular Linked List****

=============================================================================

Enter the first polynomial:

Enter coefficient: 7

Enter exponent: 4

Do you want to add another term? (y/n): y

Enter coefficient: -5

Enter exponent: 3

Do you want to add another term? (y/n): y

Enter coefficient: 6

Enter exponent: 2

Do you want to add another term? (y/n): y

Enter coefficient: -4

Enter exponent: 2

Error: Exponents must be entered in descending order.

Enter coefficient: -4

Enter exponent: 1

Do you want to add another term? (y/n): y

Enter coefficient: 2

Enter exponent: 0


Enter the second polynomial:

Enter coefficient: 9

Enter exponent: 4

Do you want to add another term? (y/n): y

Enter coefficient: -5

Enter exponent: 2

Do you want to add another term? (y/n): n


First Polynomial: 7x^4-5x^3 + 6x^2-4x^1 + 2x^0

Second Polynomial: 9x^4-5x^2

Resultant Polynomial after Addition: 16x^4-5x^3 + 1x^2-4x^1 + 2x^0


Do you want to add another pair of polynomials? (y/n): y

Enter the first polynomial:

Enter coefficient: 3

Enter exponent: 1

Do you want to add another term? (y/n): n


Enter the second polynomial:

Enter coefficient: 4

Enter exponent: 1

Do you want to add another term? (y/n): n


First Polynomial: 3x^1

Second Polynomial: 4x^1

Resultant Polynomial after Addition: 7x^1


Do you want to add another pair of polynomials? (y/n): n


--------------------------------

Process exited after 191 seconds with return value 0

Press any key to continue . . .

No comments:

Post a Comment

Unit-1 Data Science Notes