-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdding_Polynomials_Using_Linked_List.c
More file actions
108 lines (91 loc) · 2.7 KB
/
Adding_Polynomials_Using_Linked_List.c
File metadata and controls
108 lines (91 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
struct Node {
int coeff;
int exp;
struct Node* next;
};
typedef struct Node Node;
Node* createNode(int coeff, int exp) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->coeff = coeff;
newNode->exp = exp;
newNode->next = NULL;
return newNode;
}
void insertTerm(Node** head, int coeff, int exp) {
Node* newNode = createNode(coeff, exp);
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void display(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%dx^%d", current->coeff, current->exp);
current = current->next;
if (current != NULL) {
printf(" + ");
}
}
printf("\n");
}
Node* addPolynomials(Node* poly1, Node* poly2) {
Node* result = NULL;
while (poly1 != NULL && poly2 != NULL) {
if (poly1->exp > poly2->exp) {
insertTerm(&result, poly1->coeff, poly1->exp);
poly1 = poly1->next;
} else if (poly1->exp < poly2->exp) {
insertTerm(&result, poly2->coeff, poly2->exp);
poly2 = poly2->next;
} else {
insertTerm(&result, poly1->coeff + poly2->coeff, poly1->exp);
poly1 = poly1->next;
poly2 = poly2->next;
}
}
while (poly1 != NULL) {
insertTerm(&result, poly1->coeff, poly1->exp);
poly1 = poly1->next;
}
while (poly2 != NULL) {
insertTerm(&result, poly2->coeff, poly2->exp);
poly2 = poly2->next;
}
return result;
}
int main() {
Node* poly1 = NULL;
Node* poly2 = NULL;
Node* result = NULL;
int coeff, exp, n;
printf("Enter the number of terms in the first polynomial: ");
scanf("%d", &n);
printf("Enter the terms of the first polynomial (coeff exp):\n");
for (int i = 0; i < n; i++) {
scanf("%d %d", &coeff, &exp);
insertTerm(&poly1, coeff, exp);
}
printf("Enter the number of terms in the second polynomial: ");
scanf("%d", &n);
printf("Enter the terms of the second polynomial (coeff exp):\n");
for (int i = 0; i < n; i++) {
scanf("%d %d", &coeff, &exp);
insertTerm(&poly2, coeff, exp);
}
printf("First polynomial: ");
display(poly1);
printf("Second polynomial: ");
display(poly2);
result = addPolynomials(poly1, poly2);
printf("Resultant polynomial (sum of input polynomials): ");
display(result);
return 0;
}