-
Notifications
You must be signed in to change notification settings - Fork 0
/
LINKED LIST POLYNOMIAL ADDITION.c
121 lines (106 loc) · 2.27 KB
/
LINKED LIST POLYNOMIAL ADDITION.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
// LINKED LIST POLYNOMIAL ADDITION
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
struct node{
int coef;
int exp;
struct node* next;
};
struct node* Getnode()
{
struct node* p;
p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;
return p;
};
void InsBegin(struct node **list,int x,int y){
struct node*P;
P=Getnode();
P->coef=x;
P->exp=y;
P->next=*list;
(*list)=P;
}
void Traverse(struct node *list){
struct node*p;
p=list;
while(p!=NULL){
printf("%dx^%d + ",p->coef,p->exp);
p=p->next;
}
}
struct node* Multiply(struct node*poly1,struct node*poly2)
{
};
struct node *AddPoly(struct node*poly1,struct node*poly2)
{
struct node*p,*q;
p=poly1;
q=poly2;
struct node*poly3=NULL;
while(p!=NULL && q!=NULL)
{
if(p->exp==q->exp)
{
int x=p->exp;
int y=p->coef+q->coef;
InsBegin(&poly3,y,x);
p=p->next;
q=q->next;
}
else
{
if(p->exp>q->exp)
{
int x=p->exp;
int y=p->coef;
InsBegin(&poly3,x,y);
p=p->next;
}
else
{
int x=q->exp;
int y=q->coef;
InsBegin(&poly3,x,y);
q=q->next;
}
}
}
return poly3;
}
struct node *Subtract(struct node*poly1,struct node*poly2)
{
struct node*p,*q,*poly3;
p=poly1;
q=poly2;
while(poly1!=NULL)
{
poly1->coef=poly1->coef*(-1);
}
p=poly1;
poly3=AddPoly(p,q);
return poly3;
}
int main(){
struct node *poly1=NULL, *poly2=NULL,*poly3=NULL;
InsBegin(&poly1,1,1);
InsBegin(&poly1,2,2);
InsBegin(&poly1,3,3);
InsBegin(&poly1,4,4);
InsBegin(&poly2,1,1);
InsBegin(&poly2,5,2);
InsBegin(&poly2,3,4);
InsBegin(&poly2,4,5);
Traverse(poly1);
printf("\n\n\n");
Traverse(poly2);
poly3=AddPoly(poly1,poly2);
printf("\n\n\n");
Traverse(poly3);
printf("\n\n\n");
poly3=Subtract(poly1,poly2);
Traverse(poly3);
return 0;
}