-
Notifications
You must be signed in to change notification settings - Fork 1
/
polylink.c
181 lines (173 loc) · 3.57 KB
/
polylink.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
174
175
176
177
178
179
180
181
#include<stdio.h>
#include<stdlib.h>
struct node
{
int coeff,expo;
struct node *link;
} *head1, *head2, *head3, *head4;
struct node * readpoly();
void display(struct node *head);
struct node * addpoly();
struct node * multiply();
void main(){
printf("First polynomial :\n");
head1 = readpoly();
display(head1);
printf("\nSecond polynomial :\n");
head2 = readpoly();
display(head2);
printf("\n\nSum of polynomials is :\n");
head3 = addpoly();
display(head3);
printf("\n\nMultiplication of polynomials is :\n");
head4 = multiply();
display(head4);
}
//Functions
struct node * readpoly(){
int n,i;
printf("Enter the highest exponent of the polynomial\n");
scanf("%d",&n);
struct node *new, *ptr, *head = NULL;
for (i=n;i>=0;i--)
{
new = (struct node *)malloc(sizeof(struct node *));
printf("Enter the coefficient of X^%d :",i);
scanf("%d",&new->coeff);
new->expo = i;
new->link = NULL;
if(head == NULL){
head = new;
ptr = head;
//For first node
}
else{
ptr->link = new;
ptr = new;
//For remaining nodes
}
}
return(head);
}
void display(struct node *head){
struct node *ptr = head;
if(ptr == NULL)
printf("Polynomial is empty\n");
else {
while(ptr->link != NULL){
printf("%d(X^%d)+",ptr->coeff,ptr->expo);
ptr = ptr->link;
}
printf("%d(X^%d)",ptr->coeff,ptr->expo);
}
}
struct node * addpoly(){
struct node *new, *p, *q, *r, *head = NULL;
p = head1;
q = head2;
while(p != NULL && q != NULL){
if(p->expo == q->expo){
new = (struct node *)malloc(sizeof(struct node *));
new->coeff = p->coeff+q->coeff;
new->expo = p->expo;
p = p->link;
q = q->link;
}
else if(p->expo > q->expo){
new = (struct node *)malloc(sizeof(struct node *));
new->coeff = p->coeff;
new->expo = p->expo;
p = p->link;
}
else{
new = (struct node *)malloc(sizeof(struct node *));
new->coeff = q->coeff;
new->expo = q->expo;
q = q->link;
}
if(head == NULL){
head = new;
new->link = NULL;
r=head;
}
else{
r->link = new;
new->link = NULL;
r = new;
}
}
while(p != NULL){
new = (struct node *)malloc(sizeof(struct node *));
new->coeff = p->coeff;
new->expo = p->expo;
p = p->link;
new->link = NULL;
if(head == NULL){
head = new;
r=head;
}
else{
r->link = new;
r = new;
}
}
while(q != NULL){
new = (struct node *)malloc(sizeof(struct node *));
new->coeff = q->coeff;
new->expo = q->expo;
q = q->link;
new->link = NULL;
if(head == NULL){
head = new;
r=head;
}
else{
r->link = new;
r = new;
}
}
return(head);
}
struct node * multiply(){
struct node *new, *p, *q, *r, *head = NULL, *prev;
p = head1;
q = head2;
while(p != NULL){
while(q != NULL){
new = (struct node *)malloc(sizeof(struct node *));
new->coeff = p->coeff * q->coeff;
new->expo = p->expo + q->expo;
new->link = NULL;
if(head == NULL){
head = new;
r = head;
}
else {
r->link = new;
r = new;
}
q = q->link;
}
p = p->link;
q = head2;
}
p = head;
while(p != NULL){
prev = p;
q = p->link;
while(q != NULL){
if(q->expo == p->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;
}
return(head);
}