-
Notifications
You must be signed in to change notification settings - Fork 0
/
addPolyLL.c
156 lines (155 loc) · 2.97 KB
/
addPolyLL.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
#include<stdio.h>
#include<stdlib.h>
void main(){
int no1,no2,i,coef1,expo1;
struct node{
int coef,expo;
struct node *link;
}*rhead=NULL,*phead=NULL,*qhead=NULL,*r,*p,*q,*temp,*p1,*q1;
printf("enter the number of elements in polynomial 1\n");
scanf("%d",&no1);
printf("enter the number of elements in polynomial 2\n");
scanf("%d",&no2);
printf("enter the elements of 1st poly in decending order\n");
for(i=0;i<no1;i++)
{
printf("enter the coef of %d term:",i+1);
scanf("%d",&coef1);
printf("enter the expo of %d term:",i+1);
scanf("%d",&expo1);
temp=(struct node*)malloc(sizeof(struct node));
temp->coef=coef1;
temp->expo=expo1;
temp->link=NULL;
if(phead==NULL){
phead=temp;
p1=temp;
}
else{
p1->link=temp;
p1=temp;
}
}
printf("enter the elements of 2nd poly in decending order\n");
for(i=0;i<no1;i++)
{
printf("enter the coef of %d term:",i+1);
scanf("%d",&coef1);
printf("enter the expo of %d term:",i+1);
scanf("%d",&expo1);
temp=(struct node*)malloc(sizeof(struct node));
temp->coef=coef1;
temp->expo=expo1;
temp->link=NULL;
if(qhead==NULL){
qhead=temp;
q1=temp;
}
else{
q1->link=temp;
q1=temp;
}
}
printf("First polynomial:");
p=phead;
while(p!=NULL)
{
if(p->link==NULL){
printf("%dX^%d ",p->coef,p->expo);
}
else{
printf("%dX^%d + ",p->coef,p->expo);
}
p=p->link;
}
printf("\n");
printf("Second polynomial:");
q=qhead;
while(q!=NULL)
{
if(q->link==NULL){
printf("%dX^%d ",q->coef,q->expo);
}
else{
printf("%dX^%d + ",q->coef,q->expo);
}
q=q->link;
}
printf("\n");
p=phead;
q=qhead;
while (p!=NULL && q!=NULL){
temp=(struct node*)malloc(sizeof(struct node));
if(p->expo == q->expo){
temp->coef=p->coef+q->coef;
temp->expo=p->expo;
temp->link=NULL;
p=p->link;
q=q->link;
}
else if(p->expo > q->expo){
temp->expo=p->expo;
temp->coef=p->coef;
temp->link=NULL;
p=p->link;
}
else{
temp->expo=q->expo;
temp->coef=q->coef;
temp->link=NULL;
q=q->link;
}
if(rhead==NULL){
rhead=temp;
r=temp;
}
else{
r->link=temp;
r=temp;
}
}
while(p!=NULL){
temp=(struct node*)malloc(sizeof(struct node));
temp->expo=p->expo;
temp->coef=p->coef;
temp->link=NULL;
p=p->link;
if(rhead==NULL){
rhead=temp;
r=temp;
}
else{
r->link=temp;
r=temp;
}
}
while(q!=NULL){
temp=(struct node*)malloc(sizeof(struct node));
temp->expo=q->expo;
temp->coef=q->coef;
temp->link=NULL;
q=q->link;
if(rhead==NULL){
rhead=temp;
r=temp;
}
else{
r->link=temp;
r=temp;
}
}
printf("Resultant polynomial:");
r=rhead;
i=1;
while(r!=NULL)
{
if(r->link==NULL){
printf("%dX^%d ",r->coef,r->expo);
}
else{
printf("%dX^%d + ",r->coef,r->expo);
}
r=r->link;
}
printf("\n");
}