-
Notifications
You must be signed in to change notification settings - Fork 0
/
poly LL(incomplete)
112 lines (89 loc) · 2.59 KB
/
poly LL(incomplete)
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
109
110
111
112
#include <stdio.h>
#include <stdlib.h>
// Node structure for a term in a polynomial
struct Node {
int coeff; // Coefficient of the term
int exp; // Exponent of the term
struct Node* next; // Pointer to the next term
};
// Function to create a new node
struct Node* createNode(int coeff, int exp) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->coeff = coeff;
newNode->exp = exp;
newNode->next = NULL;
return newNode;
}
// Function to insert a term into a polynomial
void insertTerm(struct Node** poly, int coeff, int exp) {
struct Node* newNode = createNode(coeff, exp);
if (*poly == NULL) {
*poly = newNode;
} else {
struct Node* last = *poly;
while (last->next != NULL) {
last = last->next;
}
last->next = newNode;
}
}
// Function to add two polynomials
struct Node* addPolynomials(struct Node* poly1, struct Node* poly2) {
struct Node* result = NULL;
while (poly1 != NULL || poly2 != NULL) {
int coeff1 = (poly1 != NULL) ? poly1->coeff : 0;
int exp1 = (poly1 != NULL) ? poly1->exp : 0;
int coeff2 = (poly2 != NULL) ? poly2->coeff : 0;
int exp2 = (poly2 != NULL) ? poly2->exp : 0;
int sumCoeff = coeff1 + coeff2;
insertTerm(&result, sumCoeff, exp1);
if (poly1 != NULL) {
poly1 = poly1->next;
}
if (poly2 != NULL) {
poly2 = poly2->next;
}
}
return result;
}
// Function to display a polynomial
void displayPolynomial(struct Node* poly) {
while (poly != NULL) {
printf("%dx^%d", poly->coeff, poly->exp);
if (poly->next != NULL) {
printf(" + ");
}
poly = poly->next;
}
printf("\n");
}
// Main function
int main() {
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
struct Node* result = NULL;
// Insert terms into the first polynomial
insertTerm(&poly1, 3, 2);
insertTerm(&poly1, 4, 1);
insertTerm(&poly1, 5, 0);
// Insert terms into the second polynomial
insertTerm(&poly2, 1, 3);
insertTerm(&poly2, 2, 1);
insertTerm(&poly2, 3, 0);
// Display the first polynomial
printf("Polynomial 1: ");
displayPolynomial(poly1);
// Display the second polynomial
printf("Polynomial 2: ");
displayPolynomial(poly2);
// Add the polynomials
result = addPolynomials(poly1, poly2);
// Display the result
printf("Sum: ");
displayPolynomial(result);
// Free allocated memory
free(poly1);
free(poly2);
free(result);
return 0;
}