-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMERGING_LIST.CPP
246 lines (223 loc) · 3.85 KB
/
MERGING_LIST.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include<iostream>
using namespace std;
namespace d
{
class node
{
int info;
node *next;
public:
node(int m = 0 )
{
info = m;
next = NULL;
}
void setdata(int m)
{
info = m;
}
node* getnext()
{
return next;
}
void setnext(node* p)
{
next = p;
}
int getdata()
{
return info;
}
};
class linklist
{
node* start;
public:
linklist()
{
start = NULL;
}
node* getstart()
{
return start;
}
void setstart(node * m)
{
start = m;
}
void add_node(int s)
{
node *temp = new node(s);
if(start == NULL){start = temp;return;}
node* search=start;
if(temp->getdata() < search->getdata())
{
temp->setnext(search);
start=temp;
return;
}
while(search->getnext() != NULL && search->getnext()->getdata() < temp->getdata())
{
search = search->getnext();
}
temp->setnext(search->getnext());
search->setnext(temp);
}
void deletion(int m )
{
if(start==NULL)
{
cout<<"OOPS! THERE IS NO NODE IN THIS LIST."<<endl;
cout<<endl;
return;
}
node* search=start;
if(start->getdata()==m)
{
cout<<"\t"<<m<<" DELETED"<<endl;
start=start->getnext();
delete search;
cout<<endl;
return;
}
while(search->getnext()!=NULL)
{
if(search->getnext()->getdata()==m)
{
node* del=search->getnext();
search->setnext(search->getnext()->getnext());
cout<<"\t"<<m<<" DELETED"<<endl;
delete del;
cout<<endl;
return;
}
search = search->getnext();
}
cout<<"NODE NOT FOUND"<<endl;
cout<<endl;
}
void display()
{
if(start==NULL){cout<<"LIST EMPTY"<<endl;return;}
node *dis=start;
while(dis!= NULL)
{
cout<<dis->getdata()<<endl;
dis=dis->getnext();
}
cout<<endl;
}
void merge(node* one,node* two)
{
if(one != NULL && two != NULL)
{
node* some;
if(one->getdata() < two->getdata())
{
some = new node(one->getdata());
one = one->getnext();
}
else
{
some = new node(two->getdata());
two = two->getnext();
}
start = some;
while(one != NULL && two != NULL)
{
if(one->getdata() < two->getdata())
{
some->setnext(new node(one->getdata()));
one = one->getnext();
}
else
{
some->setnext(new node(two->getdata()));
two = two->getnext();
}
some = some->getnext();
}
if(one == NULL)
{
while(two != NULL)
{
some->setnext(new node(two->getdata()));
some = some->getnext();
two = two->getnext();
}
}
else
{
while(one != NULL)
{
some->setnext(new node(one->getdata()));
some = some->getnext();
one = one->getnext();
}
}
}
}
};
};
using namespace d;
int main()
{
linklist list;
linklist list7;
linklist list15;
int option,data;
do
{
cout<<"1.ADD RECORD.\n2.DELETE RECORD\n3.DISPLAY DATABASE.\n\n4.ADD IN SECOND LIST\n5.DELETE FROM SECOND.\n6.DISPLAY SECOND DATABASE\n\n7.MERGE BOTH DATABASES.\n8.DISPLAY MERGED LIST.\n9.EXIT\n"<<endl;
cout<<"ENTER THE CHOICE FROM THE ABOVE OPTION."<<endl;
cin>>option;
switch(option)
{
case 1:
cout<<"ENTER A DATA VALUE"<<endl;
cin>>data;
list.add_node(data);
cout<<endl;
break;
case 2:
cout<<"ENTER A DATA VALUE"<<endl;
cin>>data;
list.deletion(data);
break;
case 3:
list.display();
cout<<endl;
break;
case 4:
cout<<"ENTER A DATA VALUE"<<endl;
cin>>data;
list7.add_node(data);
cout<<endl;
break;
case 5:
cout<<"ENTER A DATA VALUE"<<endl;
cin>>data;
list7.deletion(data);
break;
case 6:
list7.display();
break;
case 7:
list15.merge(list.getstart(),list7.getstart());
break;
case 8:
cout<<"MERGED LIST IS LIKE THIS:"<<endl;
list15.display();
cout<<endl;
break;
case 9:
break;
default:
cout<<"BAD CHOICE__TRY AGAIN."<<endl;
}
}while(option != 9);
cout<<endl;
char a;cin>>a;
return 0;
}
//};