polynomial addition
#include <stdio.h>
#include <stdlib.h>
// Define a structure for each block in the polynomial
struct Block {
int coefficient; // Coefficient of the variable
int exponent; // Power of the variable
struct Block* next; // Pointer to the next block in the linked list
};
typedef struct Block Node;
// Function to add two polynomials represented as linked lists
Node* addPolynomials(Node* poly1, Node* poly2) {
Node* result = NULL;
Node* temp = NULL;
// Traverse both polynomials
while (poly1 != NULL && poly2 != NULL) {
// Create a new block for the result
Node* newNode = (Node*)malloc(sizeof(Node));
// If the exponents are the same, add the coefficients
if (poly1->exponent == poly2->exponent) {
newNode->coefficient = poly1->coefficient + poly2->coefficient;
newNode->exponent = poly1->exponent;
// Move to the next blocks in both polynomials
poly1 = poly1->next;
poly2 = poly2->next;
} else if (poly1->exponent > poly2->exponent) {
// If the exponent of the first polynomial is greater,
// add its block to the result
newNode->coefficient = poly1->coefficient;
newNode->exponent = poly1->exponent;
poly1 = poly1->next;
} else {
// If the exponent of the second polynomial is greater,
// add its block to the result
newNode->coefficient = poly2->coefficient;
newNode->exponent = poly2->exponent;
poly2 = poly2->next;
}
// Add the new block to the result polynomial
newNode->next = NULL;
if (result == NULL) {
result = newNode;
temp = result;
} else {
temp->next = newNode;
temp = temp->next;
}
}
// If any polynomial is left, add its remaining blocks to the result
while (poly1 != NULL) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->coefficient = poly1->coefficient;
newNode->exponent = poly1->exponent;
newNode->next = NULL;
poly1 = poly1->next;
temp->next = newNode;
temp = temp->next;
}
while (poly2 != NULL) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->coefficient = poly2->coefficient;
newNode->exponent = poly2->exponent;
newNode->next = NULL;
poly2 = poly2->next;
temp->next = newNode;
temp = temp->next;
}
return result;
}
// Function to display a polynomial
void displayPoly(Node* poly) {
while (poly != NULL) {
printf("%dx^%d ", poly->coefficient, poly->exponent);
if (poly->next != NULL) {
printf("+ ");
}
poly = poly->next;
}
printf("\n");
}
// Function to create a new block with given coefficient and exponent
Node* createBlock(int coef, int exp) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->coefficient = coef;
newNode->exponent = exp;
newNode->next = NULL;
return newNode;
}
int main() {
// Creating two polynomials: 3x^2 + 2x + 5 and 2x^2 + 4x + 1
Node* poly1 = createBlock(3, 2);
poly1->next = createBlock(2, 1);
poly1->next->next = createBlock(5, 0);
Node* poly2 = createBlock(2, 2);
poly2->next = createBlock(4, 1);
poly2->next->next = createBlock(1, 0);
printf("First polynomial: ");
displayPoly(poly1);
printf("Second polynomial: ");
displayPoly(poly2);
// Adding the polynomials
Node* result = addPolynomials(poly1, poly2);
printf("Resultant polynomial after addition: ");
displayPoly(result);
// Freeing the memory used by the linked lists
Node* temp;
while (poly1 != NULL) {
temp = poly1;
poly1 = poly1->next;
free(temp);
}
while (poly2 != NULL) {
temp = poly2;
poly2 = poly2->next;
free(temp);
}
while (result != NULL) {
temp = result;
result = result->next;
free(temp);
}
return 0;
}