25.10.25

15. Polynomial Representation, The One Where Friends Turn Math into Linked Lists


hello everyone 

After mastering normal linked lists, Aarav said, “We can store numbers… but can we store math?”  Rahul grinned and replied, “Challenge accepted!” and that’s how we built Polynomials using Linked Lists in C!

What’s Happening Here?

Each term of the polynomial (like 3x², 2x¹, 5x⁰) is stored as a node, every node has a coefficient and an exponent. All the terms are linked together to form a full polynomial equation! You can enter two polynomials, term by term, and the program dynamically creates nodes for each part. Finally, it displays them neatly like:

3x^2 + 2x^1 + 5x^0

How It Works 

Rahul handled the input part typing in coefficients and exponents like a math wizard . Aarav connected each term into a linked list he called it “Math Chain 2.0” . And I (Daksh) made sure the display looked clean and readable, just like a proper equation. By the end, our program made polynomials look cool and manageable who knew linked lists and algebra could actually get along! 

Code:

 #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;
 }
 
 void insertTerm(struct Node** head, int coefficient, int exponent) {
     struct Node* newNode = createNode(coefficient, exponent);
     if (*head == NULL) {
         *head = newNode;
     } else {
         struct Node* temp = *head;
         while (temp->next != NULL)
             temp = temp->next;
         temp->next = newNode;
     }
 }
 
 void displayPolynomial(struct Node* head) {
     struct Node* temp = head;
     while (temp != NULL) {
         printf("%dx^%d", temp->coefficient, temp->exponent);
         if (temp->next != NULL)
             printf(" + ");
         temp = temp->next;
     }
     printf("\n");
 }
 
 int main() {
     struct Node* polynomial1 = NULL;
     struct Node* polynomial2 = NULL;
     int numberOfTerms, coefficient, exponent;
 
     printf("Enter number of terms in Polynomial 1: ");
     scanf("%d", &numberOfTerms);
 
     for (int index = 0; index < numberOfTerms; index++) {
         printf("Enter coefficient and exponent for term %d: ", index + 1);
         scanf("%d %d", &coefficient, &exponent);
         insertTerm(&polynomial1, coefficient, exponent);
     }
 
     printf("\nEnter number of terms in Polynomial 2: ");
     scanf("%d", &numberOfTerms);
 
     for (int index = 0; index < numberOfTerms; index++) {
         printf("Enter coefficient and exponent for term %d: ", index + 1);
         scanf("%d %d", &coefficient, &exponent);
         insertTerm(&polynomial2, coefficient, exponent);
     }
 
     printf("\nPolynomial 1: ");
     displayPolynomial(polynomial1);
     printf("Polynomial 2: ");
     displayPolynomial(polynomial2);
 
     return 0;
 }

Output:


← Previous 🏠 Homepage Next Chapter →

No comments:

Post a Comment

rating System

Loading...

A Friendship Story of Learning Data Structures with C

Sr. No. DSU BLOGS CHAPTERS 1 Array Operations in C, The Group of Friendship (Create, Insert, Delete ...