forked from MohmedIkram/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application of linked list in C
173 lines (172 loc) · 2.95 KB
/
Application of linked list in 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
168
169
170
171
172
173
//Assignment-2 DS Theory Addition & Multiplication of Polynomial Using Linked List by Aayush Patel 2K20/CO/11
#include <stdio.h>
#include <stdlib.h>
struct poly
{
float coef;
int expo;
struct poly *nxt;
};
void input(struct poly **s)
{
struct poly *p, *q;
int sz;
printf("Enter size of polynomial:");
scanf("%d", &sz);
printf("Enter Polynomial(Degree-Coeffcient):\n");
for (int i = 0; i < sz; i++)
{
p = (struct poly *)malloc(sizeof(struct poly));
p->nxt = NULL;
scanf("%d", &(p->expo));
scanf("%f", &(p->coef));
if (*s == NULL)
*s = p;
else
{
q = *s;
while (q->nxt != NULL)
{
q = q->nxt;
}
q->nxt = p;
}
}
}
void print(struct poly *s)
{
struct poly *p;
p = s;
if (p == NULL)
return;
while (p != NULL)
{
if (p->nxt == NULL)
printf("(%.1f)x^%d", p->coef, p->expo);
else
printf("(%.1f)x^%d+", p->coef, p->expo);
p = p->nxt;
}
}
struct poly *polyadd(struct poly *p1, struct poly *p2) //polynomial addition function
{
struct poly *p3 = NULL, *p, *q;
if (p1 == NULL)
{
p3 = p2;
return p3;
}
else if (p2 == NULL)
{
p3 = p1;
return p3;
}
while (p1 != NULL || p2 != NULL)
{
p = (struct poly *)malloc(sizeof(struct poly));
p->nxt = NULL;
if (p1 != NULL && p2 != NULL)
{
if (p1->expo == p2->expo)
{
p->coef = p1->coef + p2->coef;
p->expo = p1->expo;
p1 = p1->nxt;
p2 = p2->nxt;
}
else if (p1->expo > p2->expo)
{
p->coef = p1->coef;
p->expo = p1->expo;
p1 = p1->nxt;
}
else
{
p->coef = p2->coef;
p->expo = p2->expo;
p2 = p2->nxt;
}
}
else
{
if (p2 == NULL)
{
p->coef = p1->coef;
p->expo = p1->expo;
p1 = p1->nxt;
}
else if (p1 == NULL)
{
p->coef = p2->coef;
p->expo = p2->expo;
p2 = p2->nxt;
}
}
if (p3 == NULL)
p3 = p;
else
{
q = p3;
while (q->nxt != NULL)
{
q = q->nxt;
}
q->nxt = p;
}
}
return (p3);
}
struct poly *polymult(struct poly *p1, struct poly *p2)
{
struct poly *p3 = NULL, *p, *q, *l = NULL, *m;
while (p1 != NULL)
{
p = p2;
l = NULL;
while (p != NULL)
{
q = (struct poly *)malloc(sizeof(struct poly));
q->coef = p1->coef * p->coef;
q->expo = p1->expo + p->expo;
q->nxt = NULL;
if (l == NULL)
{
l = q;
}
else
{
m = l;
while (m->nxt != NULL)
{
m = m->nxt;
}
m->nxt = q;
}
p = p->nxt;
}
p3 = polyadd(p3,l);
p1 = p1->nxt;
}
return (p3);
}
main()
{
//printf("DS LAB Practical-16 Polynomial Addition and Multiplication using Linked List by Aayush Patel 2K20/CO/11\n");
struct poly *p1 = NULL, *p2 = NULL;
printf("First Polynomial(p1):\n");
input(&p1);
printf("Second Polynomial(p2):\n");
input(&p2);
printf("\nAddition of 2 Polynomials p1 & p2:");
print(p1);
printf("\t + \t");
print(p2);
printf("\n = \t");
print(polyadd(p1, p2));
printf("\n\nMultiplication of 2 Polynomials p1 & p2:");
print(p1);
printf("\t * \t");
print(p2);
printf("\n = \t");
print(polymult(p1, p2));
}