-
Notifications
You must be signed in to change notification settings - Fork 0
/
poly_add.c
206 lines (201 loc) · 4.54 KB
/
poly_add.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include<stdio.h>
#include<stdlib.h>
struct node{
int coef;
int exp;
struct node*link;
};
struct node*getnode(struct node*p);
struct node*create(struct node*start,int n);
struct node*delete(struct node *start,int data);
void display(struct node*start);
struct node* insert_s(struct node* start,int data,int ex);
struct node *poly_add(struct node *start1,struct node *start2);
struct node *poly_mult(struct node *start1,struct node *start2);
int main(){
int choice, data, i,n,pos,max = -1,min = -1;
struct node *start = NULL,*start1 = NULL,*start3 = NULL;
printf("1. Create\n");
printf("2. create 2nd\n3.display 1st\n4.Display 2nd\n5. delete\n ");
printf("7. Add poly.\n8.mult poly\n");
while (1)
{
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1 : printf("Enter the no of elements: ");
scanf("%d",&n);
start = NULL;
start = create(start,n);
break;
case 2 : printf("Enter the no of elements: ");
scanf("%d",&n);
start1 = NULL;
start1 = create(start1,n);
break;
case 3: display(start);
break;
case 4: display(start1);
break;
case 5 : printf("Enter the element to be deleted : ");
scanf("%d", &data);
start = delete(start, data);
break;
case 6 : printf("Enter the element to be inserted : ");
scanf("%d", &data);
printf("Enter the exponent of the element : ");
scanf("%d", &pos);
start = insert_s(start, data, pos);
break;
case 7 : start3 = poly_add(start,start1);
display(start3);
break;
case 8: start3 = poly_mult(start,start1);
display(start3);
break;
default : printf("Wrong choice\n");
exit(0);
}
}
return 0;
}
struct node*getnode(struct node*p){
p = (struct node*)malloc(sizeof(struct node));
return p;
}
struct node*create(struct node*start,int n){
int data,i,ex;
if(n == 0)
return start;
printf("Enter the coef n exp: ");
for(i = 1; i<=n ; i++){
scanf("%d%d",&data,&ex);
start = insert_s(start,data,ex);
}
return start;
}
struct node*delete(struct node *start,int data){
struct node *temp = start,*p;
if(start == NULL){
printf("empty\n");
return start;
}
if(start->coef == data){
printf("%d is deleted\n",data);
if(start->link == NULL ){
free(start->link);
start = NULL;
}
start = start->link;
return start;
}
while(temp->link->link != NULL && temp->link->coef != data){
temp = temp->link;
}
if(temp->link->coef == data){
p = temp->link;
temp->link = p->link;
free(p);
return start;
}
printf("not found %d\n",data);
return start;
}
void display(struct node*start){
if(start == NULL){
printf("empty\n");
return;
}
struct node*p = start;
while(p !=NULL){
printf("%dx ^ %d + ",p->coef,p->exp);
p = p->link;
}
printf("NULL\n");
}
struct node* insert_s(struct node* start,int data,int ex){
struct node*temp,*p = start;
int t;
temp = getnode(temp);
temp->coef = data;
temp->exp =ex;
temp->link = NULL;
if(start == NULL || start->exp < ex){
temp->link = start;
start = temp;
return start;
}
if(start->link == NULL && start->exp > ex){
start->link = temp;
return start;
}
if( start->exp==ex){
start->coef = start->coef + data;
return start;
}
while(p->link != NULL && p->link->exp >= ex)
p = p->link;
if(p->exp == ex ){
p->coef = p->coef + data;
return start;
}
temp->link = p->link;
p->link = temp;
return start;
}
struct node *poly_add(struct node *start1,struct node *start2){
struct node*temp,*p = start1, *q=start2, *start=NULL;
int t;
if(start1 == NULL )
return start2;
if(start2 == NULL )
return start1;
temp = getnode(temp);
temp->link = NULL;
start = temp;
while(p != NULL && q != NULL){
if(p->exp == q->exp){
start = insert_s(start, q->coef + p->coef,p->exp);
p = p->link;
q = q->link;
}
else if(p->exp > q->exp ){
start = insert_s(start,p->coef, p->exp);
p = p->link ;
}
else{
start = insert_s(start,q->coef, q->exp);
q = q->link;
}
}
while(p != NULL){
start = insert_s(start,p->coef,p->exp);
p = p->link;
}
while(q != NULL){
start = insert_s(start, q->coef, q->exp);
q = q->link;
}
return start;
}
struct node *poly_mult(struct node *start1,struct node *start2){
struct node*temp,*p = start1, *q=start2, *start=NULL;
int t;
if(start1 == NULL )
return start1;
if(start2 == NULL )
return start2;
temp = getnode(temp);
temp->link = NULL;
start = temp;
while(p != NULL ){
q = start2;
while(q != NULL){
start = insert_s(start,p->coef*q->coef, p->exp + q->exp);
q = q->link;
}
p = p->link;
}
return start;
}