-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomial_multiplication_new.c
139 lines (125 loc) · 2.39 KB
/
polynomial_multiplication_new.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
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int coef;
int power;
struct node *next;
}node;
node *createNode(int coef,int power)
{
node *temp=(node *)malloc(sizeof(node));
temp->coef=coef;
temp->power=power;
temp->next=NULL;
return temp;
}
node *insertAtEnd(node *head,int coef,int power)
{
node *temp=createNode(coef,power);
if(head==NULL)
return temp;
node *cur=head;
while(cur->next!=NULL)
{
cur=cur->next;
}
cur->next=temp;
return head;
}
node *addition(node *head)
{
node *cur=head;
node *temp;
while(cur->next!=NULL)
{
temp=cur->next;
while(temp!=NULL)
{
if(cur->power==temp->power)
{
cur->coef=cur->coef+temp->coef;
temp->coef=0;
temp->power=0;
}
temp=temp->next;
}
cur=cur->next;
}
return head;
}
node *multiply(node *head1,node *head2)
{
node *head3=createNode(-1,-1);
node *cur3=head3;
node *cur1=head1;
node *cur2=head2;
while(cur1!=NULL)
{
while(cur2!=NULL)
{
cur3->next=createNode(cur1->coef*cur2->coef,cur1->power+cur2->power);
cur3=cur3->next;
cur2=cur2->next;
}
cur1=cur1->next;
cur2=head2;
}
head3=head3->next;
head3=addition(head3);
return head3;
}
void display(node *head)
{
if(head==NULL)
{
printf("0");
return;
}
node *cur=head;
int flag=0;
while(cur!=NULL)
{
if(cur->coef!=0)
{
if(cur!=head && cur->coef>0)
printf("+");
printf("%dx^%d",cur->coef,cur->power);
flag=1;
}
cur=cur->next;
}
if(flag==0)
printf("0");
printf("\n");
}
int main()
{
node *head1=NULL;
int coef,power;
node *head2=NULL;
int n;
printf("Enter number of temrms in poly-1\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the coef and power\n");
scanf("%d %d",&coef,&power);
head1=insertAtEnd(head1,coef,power);
}
printf("Poly-1 :\n");
display(head1);
printf("Enter number of temrms in poly-2\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the coef and power of polynomial\n");
scanf("%d %d",&coef,&power);
head2=insertAtEnd(head2,coef,power);
}
printf("Poly-2 :\n");
display(head2);
node *head3=multiply(head1,head2);
printf("Poly-3 :\n");
display(head3);
}