-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyLL.c
More file actions
170 lines (160 loc) · 2.59 KB
/
polyLL.c
File metadata and controls
170 lines (160 loc) · 2.59 KB
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int coeff,expo;
struct node* next;
};
struct node* create(struct node* head, int coeff, int expo)
{
struct node *temp, *flag;
//if poly empty node to head node
if(head==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->coeff=coeff;
temp->expo=expo;
temp->next=NULL;
head=temp;
}
else
{
//go to last node and append
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
flag=(struct node*)malloc(sizeof(struct node));
flag->coeff=coeff;
flag->expo=expo;
flag->next=NULL;
temp->next=flag;
}
return head;
}
//add polynomial
struct node* polyAdd(struct node *p1,struct node *p2,struct node *sum)
{
struct node *poly1=p1, *poly2=p2,*res;
if(poly1!=NULL && poly2==NULL)
{
sum=poly1;
return sum;
}
else if(poly1==NULL && poly2!=NULL)
{
sum=poly2;
return sum;
}
while (poly1!=NULL && poly2!=NULL)
{
if(sum==NULL)
{
sum=(struct node*)malloc(sizeof(struct node));
res=sum;
}
else
{
res->next=(struct node*)malloc(sizeof(struct node));
res=res->next;
}
if(poly1->expo > poly2->expo)
{
res->coeff=poly1->coeff;
res->expo=poly1->expo;
poly1=poly1->next;
}
else if(poly1->expo < poly2->expo)
{
res->coeff=poly2->coeff;
res->expo=poly2->expo;
poly2=poly2->next;
}
else if(poly1->expo = poly2->expo)
{
res->coeff=poly1->coeff+poly2->coeff;
res->expo=poly1->expo;
poly1=poly1->next;
poly2=poly2->next;
}
}
while(poly1!=NULL)
{
res->next=(struct node*)malloc(sizeof(struct node));
res=res->next;
res->coeff=poly1->coeff;
res->expo=poly1->expo;
poly1=poly1->next;
}
while(poly2!=NULL)
{
res->next=(struct node*)malloc(sizeof(struct node));
res=res->next;
res->coeff=poly2->coeff;
res->expo=poly2->expo;
poly2=poly2->next;
}
res->next=NULL;
return sum;
}
void display(struct node* head)
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%dx^%d",temp->coeff,temp->expo);
temp=temp->next;
if(temp!=NULL)
{
printf("+");
}
}
printf("\n");
}
void main()
{
struct node *p1=NULL,*p2=NULL,*sum=NULL;
int ch,coeff,expo;
int i=1;
while(i)
{
printf("1. Add to Polynomial 1\n");
printf("2. Add to Polynomial 2\n");
printf("3. Perform Addition\n");
printf("4. Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter coefficient\n");
scanf("%d", & coeff);
printf("Enter exponent\n");
scanf("%d",&expo);
p1=create(p1,coeff,expo);
break;
case 2:
printf("Enter coefficient\n");
scanf("%d", & coeff);
printf("Enter exponent\n");
scanf("%d",&expo);
p2=create(p2,coeff,expo);
break;
case 3:
sum=polyAdd(p1,p2,sum);
printf("Polynomial 1\n");
display(p1);
printf("Polynomial 2\n");
display(p2);
printf("\n SUM\n");
display(sum);
break;
case 4:
i=0;
break;
default:
printf("Wrong Choice!!!");
break;
}
}
}