-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolynomialLL
106 lines (105 loc) · 2.07 KB
/
PolynomialLL
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
#include<stdio.h>
#include<stdlib.h>
struct link
{
int cof,exp;
struct link *next;
}*head1,*head2,*head3,*poly=NULL,*poly1=NULL,*poly2=NULL;
void insert(struct link *node);
void show(struct link *node);
void add(struct link *node,struct link *node1);
void main()
{
poly1=(struct link *)malloc(sizeof(struct link));
head1=poly1;
poly2=(struct link *)malloc(sizeof(struct link));
head2=poly2;
poly=(struct link *)malloc(sizeof(struct link));
head3=poly;
printf("First polynomial :-\n");
insert(poly1);
head1=poly1;
printf("Second polynomial :-\n");
insert(poly2);
head2=poly2;
add(poly1,poly2);
printf("\nFirst polynomial is : ");
show(head1);
printf("\nSecond polynomial is : ");
show(head2);
printf("\nResultant polynomial is : ");
show(head3);
printf("\n");
}
void insert(struct link *node)
{
int i,l;
printf("Enter the number of terms : ");
scanf("%d",&l);
for(i=1;i<=l;i++)
{
printf("Enter the cofficent and exponent of term %d : ",i);
scanf("%d %d",&node->cof,&node->exp);
node->next=(struct link *)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
}
}
void show(struct link *node)
{
printf("%dx^%d",node->cof,node->exp);
node=node->next;
while(node->next!=NULL)
{
printf(" + %dx^%d",node->cof,node->exp);
node=node->next;
}
}
void add(struct link *node,struct link *node1)
{
poly1=head1;
poly2=head2;
while((poly1->next!=NULL)&&(poly2->next!=NULL))
{
if(poly1->exp==poly2->exp)
{
poly->exp=poly1->exp;
poly->cof=poly1->cof+poly2->cof;
poly1=poly1->next;
poly2=poly2->next;
}
else if((poly1->exp)>(poly2->exp))
{
poly->exp=poly1->exp;
poly->cof=poly1->cof;
poly1=poly1->next;
}
else
{
poly->exp=poly2->exp;
poly->cof=poly2->cof;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next!=NULL)
{
poly->exp=poly1->exp;
poly->cof=poly1->cof;
poly1=poly1->next;
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly2->next!=NULL)
{
poly->exp=poly2->exp;
poly->cof=poly2->cof;
poly2=poly2->next;
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}