|
| 1 | + |
| 2 | +// C program to add two polynomials |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +struct Node |
| 7 | +{ |
| 8 | + int coeff; |
| 9 | + int pow; |
| 10 | + struct Node *next; |
| 11 | +}; |
| 12 | + |
| 13 | +struct Node *createNode(int c, int p); |
| 14 | + |
| 15 | +struct Node *addPolynomial(struct Node *head1, struct Node *head2) |
| 16 | +{ |
| 17 | + struct Node *dummy = createNode(0, 0); |
| 18 | + |
| 19 | + // Node to append other nodes to the end |
| 20 | + // of list |
| 21 | + struct Node *prev = dummy; |
| 22 | + struct Node *curr1 = head1, *curr2 = head2; |
| 23 | + |
| 24 | + while (curr1 != NULL && curr2 != NULL) |
| 25 | + { |
| 26 | + // if curr2.pow > curr1.pow, then |
| 27 | + // append curr2 to list |
| 28 | + |
| 29 | + if (curr1->pow < curr2->pow) |
| 30 | + { |
| 31 | + prev->next = curr2; // dummy next = curr2 |
| 32 | + prev = curr2; // now make prev as curr2, so after this next for curr2 changes. |
| 33 | + curr2 = curr2->next; // now curr 2 is next value |
| 34 | + } |
| 35 | + // if curr1.pow > curr2.pow, then |
| 36 | + // append curr2 to list |
| 37 | + else if (curr1->pow > curr2->pow) |
| 38 | + { |
| 39 | + prev->next = curr1; |
| 40 | + prev = curr1; |
| 41 | + curr1 = curr1->next; |
| 42 | + } |
| 43 | + |
| 44 | + // else, add the sum of curr1->coeff and |
| 45 | + // curr2->coeff to curr1->coeff, and append |
| 46 | + // curr1 to the list |
| 47 | + else |
| 48 | + { |
| 49 | + curr1->coeff = curr1->coeff + curr2->coeff; |
| 50 | + prev->next = curr1; |
| 51 | + prev = curr1; |
| 52 | + curr1 = curr1->next; |
| 53 | + curr2 = curr2->next; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // if curr1 is not null, then append the rest |
| 58 | + // to the list |
| 59 | + if (curr1 != NULL) |
| 60 | + { |
| 61 | + prev->next = curr1; |
| 62 | + } |
| 63 | + |
| 64 | + // if curr2 is not null, then append the rest |
| 65 | + // to the list |
| 66 | + if (curr2 != NULL) |
| 67 | + { |
| 68 | + prev->next = curr2; |
| 69 | + } |
| 70 | + |
| 71 | + return dummy->next; |
| 72 | +} |
| 73 | + |
| 74 | +void printList(struct Node *head) |
| 75 | +{ |
| 76 | + struct Node *curr = head; |
| 77 | + |
| 78 | + while (curr != NULL) |
| 79 | + { |
| 80 | + printf("%d,%d ", curr->coeff, curr->pow); |
| 81 | + curr = curr->next; |
| 82 | + } |
| 83 | + |
| 84 | + printf("\n"); |
| 85 | +} |
| 86 | + |
| 87 | +struct Node *createNode(int c, int p) |
| 88 | +{ |
| 89 | + struct Node *newNode = |
| 90 | + (struct Node *)malloc(sizeof(struct Node)); |
| 91 | + newNode->coeff = c; |
| 92 | + newNode->pow = p; |
| 93 | + newNode->next = NULL; |
| 94 | + return newNode; |
| 95 | +} |
| 96 | + |
| 97 | +int main() |
| 98 | +{ |
| 99 | + |
| 100 | + // 1st polynomial: 5x^2+4x^1+2x^0 |
| 101 | + struct Node *head1 = createNode(5, 2); |
| 102 | + head1->next = createNode(4, 1); |
| 103 | + head1->next->next = createNode(2, 0); |
| 104 | + |
| 105 | + // 2nd polynomial: -5x^1-5x^0 |
| 106 | + struct Node *head2 = createNode(-5, 1); |
| 107 | + head2->next = createNode(-5, 0); |
| 108 | + |
| 109 | + struct Node *head = addPolynomial(head1, head2); |
| 110 | + |
| 111 | + printList(head); |
| 112 | + |
| 113 | + return 0; |
| 114 | +} |
0 commit comments