-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQues-10.cpp
175 lines (175 loc) · 4.08 KB
/
Ques-10.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
QUESTION-10:
Write a program to scan a polynomial using linked list and add two polynomial.
SOLUTION:
*/
#include<iostream>
using namespace std;
class node
{
public:
int power;
int coeff;
node *next;
};
class poly
{
node *first;
int n;
public:
poly()
{
first=NULL;
}
void create()
{
char ch;
first=new node;
cout<<"Enter power and coeff for first node respectively:\n";
cin>>first->power;
cin>>first->coeff;
first->next=NULL;
do
{
cout<<"Want to enter more data press 'y':\n";
cin>>ch;
if(ch=='y')
{
insert();
}
}
while(ch=='y');
}
void insert()
{
node *current,*temp,*prev;
temp=new node;
cout<<"Enter power and coeff respectively:\n";
cin>>temp->power;
cin>>temp->coeff;
int b=temp->power;
temp->next=NULL;
prev=NULL;
current=first;
while((current!=NULL)&&((current->power)<b))
{
prev=current;
current=current->next;
}
if(prev==NULL)
{
temp->next=first;
first=temp;
}
else if(current==NULL)
{
prev->next=temp;
//temp->next=NULL;
}
else
{
temp->next=current;
prev->next=temp;
}
}
void add(poly p1,poly p2)
{
//poly p3;
node *curr,*curr1,*temp,*curr2;
curr=p1.first;
curr1=p2.first;
//curr2=p3.first;
while((curr!=NULL)&&(curr1!=NULL))
{
if(curr->power==curr1->power)
{
temp=new node;
temp->power=curr->power;
temp->coeff=curr->coeff+curr1->coeff;
temp->next=NULL;
curr=curr->next;
curr1=curr1->next;
}
else if(curr->power<curr1->power)
{
temp=new node;
temp->power=curr->power;
temp->coeff=curr->coeff;
temp->next=NULL;
curr=curr->next;
}
else if(curr1->power<curr->power)
{
temp=new node;
temp->power=curr1->power;
temp->coeff=curr1->coeff;
temp->next=NULL;
curr1=curr1->next;
}
if(first==NULL)
{
first=temp;
curr2=first;
}
else
{
curr2->next=temp;
curr2=temp;
}
}
if(curr!=NULL)
{
while(curr!=NULL)
{
temp=new node;
temp->power=curr->power;
temp->coeff=curr->coeff;
temp->next=NULL;
curr=curr->next;
curr2->next=temp;
curr2=temp;
}
}
else if(curr1!=NULL)
{
while(curr1!=NULL)
{
temp=new node;
temp->power=curr1->power;
temp->coeff=curr1->coeff;
temp->next=NULL;
curr1=curr1->next;
curr2->next=temp;
curr2=temp;
}
}
if(curr2!=NULL)
cout<<"Warning curr2 not null:\n";
}
void display()
{
node *current;
current=this->first;
while(current!=NULL)
{
cout<<current->power<<" -> "<<current->coeff<<"\n";
current=current->next;
}
cout<<endl;
}
};
int main()
{
cout << "\n\t ~~~~~~~~~~~~~~~~~~~~~~~Practical 10~~~~~~~~~~~~~~~~~~~~\n\t\t\t\t \n";
poly p1,p2,p3;
cout << "Enter data of first polynomial" << endl;
p1.create();
cout << endl << "Enter data for second polynomial" << endl;
p2.create();
p1.display();
p2.display();
p3.add(p1,p2);
cout<< endl << "Here Comes Resulting Polynomial: " << endl << endl;
p3.display();
cout << endl << endl;
}