-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polylinked
104 lines (91 loc) · 1.77 KB
/
Polylinked
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data,exp;
struct node* link;
};
struct node* poly1,*poly2,*respoly;
void display();
void insertlast(int ,int,int);
void multiplicator();
int main()
{
int i,j,k,data;
printf("enter highest degree of polynomial A:");
scanf("%d",&j);
for(i=j;i>=0;i--)
{ printf("enter the coef of term with exp[%d] :",i);
scanf("%d",&data);
insertlast(i,1,data);
}
printf("\n");
printf("enter highest degree of polynomial b:");
scanf("%d",&k);
for(i=k;i>=0;i--)
{ printf("enter the coef of term with exp[%d] :",i);
scanf("%d",&data);
insertlast(i,2,data);
}
multiplicator();
display();
}
void multiplicator()
{ int coef, sumexp;
struct node*temp1=poly1,*temp2;
while(temp1!=NULL)
{
temp2=poly2;
while(temp2!=NULL)
{
coef=((temp1->data)*(temp2->data));
sumexp=((temp1->exp)+(temp2->exp));
insertlast(sumexp,3,coef);
temp2=temp2->link;
}
temp1=temp1->link;
}
}
void display()
{
struct node* temp;
temp=respoly;
printf("the result is:\n");
while(temp!=NULL)
{
printf("%dx^[%d] ",temp->data,temp->exp);
temp=temp->link;
}
printf("\n");
}
void insertlast(int i,int opt,int data)
{
struct node* newnode,*head,*temp;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->link=NULL;
newnode->exp=i;
newnode->data=data;
if(opt==1)
{
head=poly1;
}
else if(opt ==2)
head=poly2;
else
{ head=respoly;}
temp=head;
if((head==NULL)&&(opt==1))
{
poly1=newnode;
}
else if((head==NULL)&&(opt==2))
poly2=newnode;
else if((head==NULL)&&(opt==3))
respoly=newnode;
else{
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=newnode;}
}