This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp21_1511.c
152 lines (138 loc) · 2.69 KB
/
p21_1511.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
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 21/1511
// Polynomial representation and addition using SLL
// Dynamic Memory Allocation
#include <stdio.h>
#include <stdlib.h>
struct poly{
int co;
int ex;
struct poly *link;
};
struct poly *c = NULL, *temp_res;
struct poly *createPoly();
void sort(struct poly *start);
void polyAdd(struct poly *a, struct poly *b);
void result(int coe ,int exp);
void display(struct poly *a);
void exit_free(struct poly *start);
void main(){
struct poly *a = createPoly();
printf("\tPolynomial A = ");
display(a);
struct poly *b = createPoly();
printf("\tPolynomial B = ");
display(b);
polyAdd(a, b);
printf("\n\nSUM = ");
display(c);
printf("\n");
exit_free(a);
exit_free(b);
exit_free(c);
}
struct poly *createPoly(){
struct poly *start = NULL, *temp;
int n;
printf("\n\nEnter Number of Non-Zero terms: ");
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
struct poly *p = malloc(sizeof(struct poly));
p->link = NULL;
printf("\t(%d/%d) Enter power: ", i, n);
scanf("%d", &(p->ex));
printf("\tEnter coeffecient: ");
scanf("%d", &(p->co));
printf("\n");
if(start == NULL){
start = p;
temp = p;
}
else{
temp->link = p;
temp = p;
}
}
sort(start);
return start;
}
void sort(struct poly *start){
int temp_co, temp_ex;
for(struct poly *i = start; i != NULL; i = i->link){
for(struct poly *j = start; j->link != NULL; j = j->link){
if(j->ex < j->link->ex){
temp_co = j->co;
j->co = j->link->co;
j->link->co = temp_co;
temp_ex = j->ex;
j->ex = j->link->ex;
j->link->ex = temp_ex;
}
}
}
}
void display(struct poly *start){
for(struct poly *temp = start; temp != NULL; temp = temp->link){
printf(" %d(x ^ %d) + ", temp->co, temp->ex);
}
printf(" 0");
}
void polyAdd(struct poly *a, struct poly *b){
struct poly *p = a, *q = b;
while(p != NULL && q != NULL){
if(p->ex == q->ex){
int sum = p->co + q->co;
result(sum, p->ex);
p = p->link;
q = q->link;
}
else if(p->ex > q->ex){
result(p->co, p->ex);
p = p->link;
}
else{
result(q->co, q->ex);
q = q->link;
}
}
while(p != NULL){
result(p->co, p->ex);
p = p->link;
}
while(q != NULL){
result(q->co, q->ex);
q = q->link;
}
}
void result(int coe, int exp){
struct poly *r = malloc(sizeof(struct poly));
r->co = coe;
r->ex = exp;
r->link = NULL;
if(c == NULL){
c = r;
temp_res = r;
}
else{
temp_res->link = r;
temp_res = r;
}
}
// Final de-allocation of SLL
// Choice 4
void exit_free(struct poly *start){
struct poly *loc, *temp;
if(start != NULL){
temp = start;
loc = temp->link; // link node
while(loc != NULL){
free(temp);
temp = loc;
loc = loc->link;
}
free(temp);
}
}