-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathPolynomial_Addition(LinkedList).cpp
132 lines (122 loc) · 2.8 KB
/
Polynomial_Addition(LinkedList).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
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node
{
int exp;
int coeff;
struct node *link;
};
struct node* createLL(struct node *head)
{
head = NULL;
return head;
}
struct node* insertLL(struct node *head, int num,int expo)
{
struct node *c = head;
struct node *curr = new(node);
curr->coeff = num;
curr->exp = expo;
curr->link = NULL;
if(head==NULL || expo>head->exp)
{
curr->link = head;
head = curr;
}
else{
while(c->link != NULL && c->link->exp >= expo)
c = c->link;
curr->link = c->link;
c->link = curr;
}
return head;
}
struct node* cLL(struct node* head)
{
int n,i;
int coeff;
int expo;
cout<<"Enter number of terms: ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter coefficient for term "<<i+1<<": ";
cin>>coeff;
cout<<"Enter exponent for term "<<i+1<<": ";
cin>>expo;
head = insertLL(head,coeff,expo);
}
return head;
}
void travLL(struct node *head)
{
struct node *ptr = NULL;
ptr = head;
if(ptr==NULL)
{
cout<<"LL is empty\n";
}
else{
while(ptr!=NULL)
{
cout<<ptr->coeff<<"x^"<<ptr->exp;
ptr = ptr->link;
if(ptr!=NULL)
cout<<" + ";
}
cout<<"\n";
}
}
void polyAdd(struct node* head1, struct node* head2)
{
struct node* ptr1 = head1;
struct node* ptr2 = head2;
struct node* head3 = NULL;
while(ptr1!=NULL && ptr2!=NULL)
{
if(ptr1->exp == ptr2->exp)
{
head3 = insertLL(head3,ptr1->coeff + ptr2->coeff,ptr1->exp);
ptr1 = ptr1->link;
ptr2 = ptr2->link;
}
else if(ptr1->exp > ptr2->exp)
{
head3 = insertLL(head3,ptr1->coeff,ptr1->exp);
ptr1 = ptr1->link;
}
else if(ptr1->exp < ptr2->exp)
{
head3 = insertLL(head3,ptr2->coeff,ptr2->exp);
ptr2 = ptr2->link;
}
}
while(ptr1!=NULL)
{
head3 = insertLL(head3,ptr1->coeff,ptr1->exp);
ptr1 = ptr1->link;
}
while(ptr2 != NULL)
{
head3 = insertLL(head3,ptr2->coeff,ptr2->exp);
ptr2 = ptr2->link;
}
cout<<"Added polynomial is: ";
travLL(head3);
}
int main()
{
struct node* head1 = NULL;
struct node* head2 = NULL;
cout<<"Enter first polynomial\n";
head1 = cLL(head1);
cout<<"Enter second polynomial\n";
head2 = cLL(head2);
cout<<"First polynomial: ";
travLL(head1);
cout<<"Second polynomial: ";
travLL(head2);
polyAdd(head1,head2);
return 0;
}