-
Notifications
You must be signed in to change notification settings - Fork 0
/
polySLL.c
167 lines (148 loc) · 3.66 KB
/
polySLL.c
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <stdlib.h>
struct term {
int coeff;
int expo;
struct term* link;
};
struct term *p1, *p2, *r, *pro;
struct term* polyInit(){
int n, ctemp, etemp;
printf("\nEnter The Number Of Polynomial Terms: ");
scanf("%d", &n);
struct term* ptr = (struct term*)malloc(sizeof(struct term));
struct term *head = ptr;
for (int i = 1; i < n+1; i++){
printf("\nEnter The Coefficient For T%d: ", i);
scanf("%d", &ctemp);
printf("Enter The Exponent For T%d: ", i);
scanf("%d", &etemp);
ptr -> coeff = ctemp;
ptr -> expo = etemp;
if(i != n){
ptr -> link = (struct term*)malloc(sizeof(struct term));
ptr = ptr -> link;
}
else
ptr -> link = NULL;
}
return head;
}
void display(struct term* ptemp){
struct term* ptr = ptemp;
if (ptr == NULL)
printf("Polynomial Does Not Exist!");
else{
while(ptr != NULL){
if(ptr -> expo != 0)
printf("%dx^%d", ptr-> coeff, ptr -> expo);
else
printf("%d", ptr -> coeff);
if(ptr -> link != NULL)
printf(" + ");
ptr = ptr -> link;
}
}
}
void add(){
struct term *ptr1, *ptr2, *ptrR, *ptrr;
ptr1 = p1;
ptr2 = p2;
ptrR = (struct term*)malloc(sizeof(struct term));
r = ptrR;
while (ptr1 != NULL && ptr2 != NULL) {
if ((ptr1 -> expo) < (ptr2 -> expo)) {
ptrR -> coeff = ptr2 -> coeff;
ptrR -> expo = ptr2 -> expo;
ptr2 = ptr2 -> link;
}
else if ((ptr1 -> expo) > (ptr2 -> expo)) {
ptrR -> coeff = ptr1 -> coeff;
ptrR -> expo = ptr1 -> expo;
ptr1 = ptr1 -> link;
}
else{
if(ptr1 -> coeff + ptr2 -> coeff != 0){
ptrR -> coeff = ptr1 -> coeff + ptr2 -> coeff;
ptrR -> expo = ptr1 -> expo;
ptr1 = ptr1 -> link;
ptr2 = ptr2 -> link;
}
else{
ptr1 = ptr1 -> link;
ptr2 = ptr2 -> link;
continue;
}
}
ptrR -> link = (struct term*)malloc(sizeof(struct term));
ptrR = ptrR -> link;
}
if (ptr1 != NULL)
ptrr = ptr1;
else
ptrr = ptr2;
while(ptrr != NULL){
ptrR -> coeff = ptrr -> coeff;
ptrR -> expo = ptrr -> expo;
if(ptrr -> link != NULL){
ptrR -> link = (struct term*)malloc(sizeof(struct term));
ptrR = ptrR -> link;
}
else
ptrR -> link = NULL;
ptrr = ptrr -> link;
}
}
void mult() {
struct term *p = p1, *q = p2, *r = pro, *new, *prev;
while (p != NULL) {
while (q != NULL) {
new = (struct term*)malloc(sizeof(struct term));
new -> coeff = (p -> coeff) * (q -> coeff);
new -> expo = (p -> expo) + (q -> expo);
new -> link = NULL;
if (pro == NULL)
pro = r = new;
else{
r -> link = new;
r = new;
}
q = q -> link;
}
p = p -> link;
q = p2;
}
p = pro;
while (p != NULL) {
prev = p, q = p -> link;
while (q != NULL) {
if (p -> expo == q -> expo){
p -> coeff = (p -> coeff) + (q -> coeff);
prev -> link = q -> link;
free(q);
q = prev -> link;
}
else{
prev = q;
q = q -> link;
}
}
p = p -> link;
}
}
void main(){
printf("\n======= Inititializing P1 =======");
p1 = polyInit();
printf("\n======= Inititializing P2 =======");
p2 = polyInit();
printf("\nPolynomial 1: ");
display(p1);
printf("\nPolynomial 2: ");
display(p2);
add();
printf("\nSum: ");
display(r);
mult();
printf("\nProduct: ");
display(pro);
}