-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly_eqn.cpp
92 lines (88 loc) · 1.85 KB
/
poly_eqn.cpp
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
#include<iostream>
using namespace std;
struct Node
{
int val;
int val1;
Node *next;
};
class linked
{ int num;
public:
Node *start;
linked()
{start = NULL;}
Node* createNode(int value,int value1)
{
Node *temp;
temp = new(struct Node);
temp->val = value;
temp->val1 = value1;
temp->next = NULL;
return temp;
}
void make_list()
{
int value,value1,n;
cout<<"Enter the number of terms: ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter the cofficient and power : ";
cin>>value>>value1;
Node *temp = createNode(value,value1);
temp->next = start;
start = temp;
}
}
friend void add(linked ,linked);
void print_list()
{
Node *ptr;
ptr = start;
while(ptr != NULL)
{
cout<<ptr->val<<"x^"<<ptr->val1<<" ";
ptr = ptr->next;
}
cout<<endl<<endl;
}
};
void add(linked a,linked b)
{
Node *ptr,*ptr1;
ptr=a.start;
ptr1=b.start;
while(ptr!=NULL)
{
while(ptr1!=NULL)
{
if(ptr->val1==ptr1->val1)
{
ptr->val=ptr->val+ptr1->val;
break;
}
ptr1=ptr1->next;
}
ptr=ptr->next;
}
}
int main()
{
cout<<"NAME : Abhishek Rana"<<endl;
cout<<"ROLL_NO. : 1706386 "<<endl;
cout<<"CLASS : CSE-A1 "<<endl;
cout<<"----------------------------------------"<<endl<<endl;
linked l1,l2;
cout<<"Enter the 1st equation : "<<endl;
cout<<"--------------------------"<<endl;
l1.make_list();
cout<<"Enter the 2nd equation : "<<endl;
cout<<"--------------------------"<<endl;
l2.make_list();
add(l1,l2);
cout<<"\n-------------------------"<<endl;
cout<<"\nSUM : ";
l1.print_list();
return 0;
}