hello everyone
After representing polynomials with linked lists, Aarav suddenly said, “Okay now let’s add them!” Rahul said, “Fine, Professor Math let’s do this.”
What’s Going On Here?
Each term of a polynomial (like 3x², 5x¹, 2x⁰) is stored as a node with its coefficient and power. We create two linked lists, one for each polynomial. Then, we go through both lists together
-
If powers match we add the coefficients.
-
If one power is bigger we keep that term as it is.
Finally, we build a brand-new linked list that stores the sum polynomial neatly,
like:(3x² + 2x¹ + 1x⁰) + (4x² + 1x¹ + 3x⁰) = 7x² + 3x¹ + 4x⁰
How It Works?
Rahul handled the part where powers matched he’s all about balance . Aarav took care of adding coefficients but almost added wrong powers too . And I (Daksh) just made sure the final output looked beautiful all clean and math-y. By the end, our linked list didn’t just store math it did the math. Honestly, teamwork never looked this algebraic!
Code:
#include <stdio.h> #include <stdlib.h> struct Node { int coefficient, power; struct Node* next; }; struct Node* createNode(int coefficient, int power) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->coefficient = coefficient; newNode->power = power; newNode->next = NULL; return newNode; } void insertNode(struct Node** head, int coefficient, int power) { struct Node* newNode = createNode(coefficient, power); if (*head == NULL) *head = newNode; else { struct Node* current = *head; while (current->next) current = current->next; current->next = newNode; } } struct Node* addPolynomials(struct Node* firstPolynomial, struct Node* secondPolynomial) { struct Node* resultPolynomial = NULL; while (firstPolynomial && secondPolynomial) { if (firstPolynomial->power > secondPolynomial->power) { insertNode(&resultPolynomial, firstPolynomial->coefficient, firstPolynomial->power); firstPolynomial = firstPolynomial->next; } else if (firstPolynomial->power < secondPolynomial->power) { insertNode(&resultPolynomial, secondPolynomial->coefficient, secondPolynomial->power); secondPolynomial = secondPolynomial->next; } else { insertNode(&resultPolynomial, firstPolynomial->coefficient + secondPolynomial->coefficient, firstPolynomial->power); firstPolynomial = firstPolynomial->next; secondPolynomial = secondPolynomial->next; } } while (firstPolynomial) { insertNode(&resultPolynomial, firstPolynomial->coefficient, firstPolynomial->power); firstPolynomial = firstPolynomial->next; } while (secondPolynomial) { insertNode(&resultPolynomial, secondPolynomial->coefficient, secondPolynomial->power); secondPolynomial = secondPolynomial->next; } return resultPolynomial; } void display(struct Node* polynomial) { while (polynomial) { printf("%dx^%d", polynomial->coefficient, polynomial->power); if (polynomial->next) printf(" + "); polynomial = polynomial->next; } printf("\n"); } int main() { struct Node *firstPolynomial = NULL, *secondPolynomial = NULL, *sumPolynomial = NULL; insertNode(&firstPolynomial, 3, 2); insertNode(&firstPolynomial, 5, 1); insertNode(&firstPolynomial, 6, 0); insertNode(&secondPolynomial, 6, 1); insertNode(&secondPolynomial, 8, 0); printf("First Polynomial: "); display(firstPolynomial); printf("Second Polynomial: "); display(secondPolynomial); sumPolynomial = addPolynomials(firstPolynomial, secondPolynomial); printf("Sum of Polynomials: "); display(sumPolynomial); return 0; }
#include <stdio.h> #include <stdlib.h> struct Node { int coefficient, power; struct Node* next; }; struct Node* createNode(int coefficient, int power) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->coefficient = coefficient; newNode->power = power; newNode->next = NULL; return newNode; } void insertNode(struct Node** head, int coefficient, int power) { struct Node* newNode = createNode(coefficient, power); if (*head == NULL) *head = newNode; else { struct Node* current = *head; while (current->next) current = current->next; current->next = newNode; } } struct Node* addPolynomials(struct Node* firstPolynomial, struct Node* secondPolynomial) { struct Node* resultPolynomial = NULL; while (firstPolynomial && secondPolynomial) { if (firstPolynomial->power > secondPolynomial->power) { insertNode(&resultPolynomial, firstPolynomial->coefficient, firstPolynomial->power); firstPolynomial = firstPolynomial->next; } else if (firstPolynomial->power < secondPolynomial->power) { insertNode(&resultPolynomial, secondPolynomial->coefficient, secondPolynomial->power); secondPolynomial = secondPolynomial->next; } else { insertNode(&resultPolynomial, firstPolynomial->coefficient + secondPolynomial->coefficient, firstPolynomial->power); firstPolynomial = firstPolynomial->next; secondPolynomial = secondPolynomial->next; } } while (firstPolynomial) { insertNode(&resultPolynomial, firstPolynomial->coefficient, firstPolynomial->power); firstPolynomial = firstPolynomial->next; } while (secondPolynomial) { insertNode(&resultPolynomial, secondPolynomial->coefficient, secondPolynomial->power); secondPolynomial = secondPolynomial->next; } return resultPolynomial; } void display(struct Node* polynomial) { while (polynomial) { printf("%dx^%d", polynomial->coefficient, polynomial->power); if (polynomial->next) printf(" + "); polynomial = polynomial->next; } printf("\n"); } int main() { struct Node *firstPolynomial = NULL, *secondPolynomial = NULL, *sumPolynomial = NULL; insertNode(&firstPolynomial, 3, 2); insertNode(&firstPolynomial, 5, 1); insertNode(&firstPolynomial, 6, 0); insertNode(&secondPolynomial, 6, 1); insertNode(&secondPolynomial, 8, 0); printf("First Polynomial: "); display(firstPolynomial); printf("Second Polynomial: "); display(secondPolynomial); sumPolynomial = addPolynomials(firstPolynomial, secondPolynomial); printf("Sum of Polynomials: "); display(sumPolynomial); return 0; }
Output:
| ← Previous | 🏠 Homepage | Next Chapter → |
No comments:
Post a Comment