-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyAdd.cpp
More file actions
80 lines (70 loc) · 1.11 KB
/
polyAdd.cpp
File metadata and controls
80 lines (70 loc) · 1.11 KB
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
#include<iostream>
using namespace std;
struct node
{
int pow,coff;
node *link;
}*head1,*head2,*answer=NULL;
void ins1(int p,int c) // To insert in 1st eqn
{
node *temp =new node;
temp->pow=p;
temp->coff=c;
temp->link=head1;
head1=temp;
}
void ins2(int p,int c) // To insert in 2nd eqn
{
node *temp =new node;
temp->pow=p;
temp->coff=c;
temp->link=head2;
head2=temp;
}
void disp()
{
//cout<<"hlo "<<answer<<" hi ";
node *ptr=answer;
while(ptr!=NULL)
{
cout<<"power = "<<ptr->pow<<" coff = "<<ptr->coff<<endl;
ptr=ptr->link;
}
}
void add()
{
node *ptr1=head1,*ptr2=head2,*ans;
while(ptr1!=NULL&&ptr2!=NULL)
{
ans=new node;
ans->pow=ptr1->pow;
ans->coff=ptr1->coff+ptr2->coff;
ptr1=ptr1->link;
ptr2=ptr2->link;
ans->link=answer;
answer=ans;
}
}
int main()
{
int n;
cout<<"Enter the maximum degree of eqn : ";
cin>>n;
int x;
for(int i=0;i<=n;i++)
{
cout<<"\nEnter the coff of x^"<<i<<" ";
cin>>x;
ins1(i,x);
}
cout<<"\n\n Enter 2nd eqn \n";
for(int i=0;i<=n;i++)
{
cout<<"\nEnter the coff of x^"<<i<<" ";
cin>>x;
ins2(i,x);
}
add();
disp();
return 0;
}