-
Notifications
You must be signed in to change notification settings - Fork 0
/
PROGRAM_SINGLY_LINKLIST.cpp
321 lines (294 loc) · 5.76 KB
/
PROGRAM_SINGLY_LINKLIST.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//18 singly linklist
#include<iostream>
using namespace std;
class node
{
private:
int no;
node*next;
public:
friend class list;
};
class list
{
node*root;
node*curr;
node*prev;
node*temp;
node*proot;
node*nroot;
node*sroot;
node*r;
public:
//-------------------------------------------------------
list()
{
root=NULL; //node*root!=NULL as address of ptr can't be NULL therefore root=NULL similarly for all.
curr=NULL;
prev=NULL;
temp=NULL;
proot=NULL;
nroot=NULL;
sroot=NULL;
r=NULL;
}
//---------------------------------------------------------
void createlist() //1st func
{
while(1)
{
curr=new node; //create blank node
cout<<"enter no : ";
cin>>curr->no;
if(curr->no==0)
break;
curr->next=NULL;
if(root==NULL)
{
root=curr;
prev=curr;
}
else
{
prev->next=curr;
prev=curr;
}
}
}
//-------------------------------------------------------
void display(node *r) //common display function
{
cout<<"\n";
while(r!=NULL)
{
cout<<r->no<<" ";
r=r->next; //100=100->120 ie r=120
}
cout<<"NULL";
}
//-------------------------------------------------------
node* getroot()
{
return root; /*we can't pass directly root in switch case as root is not declared in main
function so written root with help of another function */
}
//------------------------------------------------------
void delneg() //delete negative node function
{
curr=root; //set each ptr to root
prev=root;
temp=root;
cout<<"\n";
while(curr!=NULL)
{
if(curr->no<0) //if -ve no. then will enter
{
if(curr==root) //if first no. -ve increment root tonext node
{
root=curr->next;
prev=root;//don't just forward using next change
curr=root;
}
else
{
prev->next=curr->next; //else connect the node by skipping -ve node
temp=curr->next; //store next node address temp
delete curr; //delete curr
curr=temp; //will assign the next node address
}
}
else
{
prev=curr;
curr=curr->next; //only for poitive no.s for negative 1st only curr increments
}
}
display(root); //it will go to display function and print eachnode except -ve ones
}
//-------------------------------------------------------
void splitlist(int t)
{
// cout<<"nakul "<<t<<endl;
r=root;//don't disturb root pointer
// cout<<r;
if(t==0)//if not merging problem
nroot=NULL;
if(t==1)
proot=NULL;
while(r!=NULL)
{
if(t==0) //negative link list creation
{
if(r->no < 0)//if not less then directly increment r=r->next;
{
curr=new node;
curr->no=r->no;
// cout<<endl<<curr->no;
curr->next=NULL;
if(nroot==NULL)
{
nroot=curr;
prev=curr;
}
else
{
// cout<<"HEllo else ";
prev->next=curr;
prev=curr;
}
}
}
if(t==1) //positive linklist creation
{
if(r->no > 0)
{
curr=new node;
curr->no=r->no;
curr->next=NULL;
if(proot==NULL)
{
proot=curr;
prev=curr;
}
else
{
prev->next=curr;
prev=curr;
}
}
}
r=r->next; //very very imp
}
// cout<<"End"<<endl;
if(t==0)
{
display(nroot); //it will pass nroot to display and -ve linkl list will be displayed
}
if(t==1)
{
display(proot); //it will pass proot to display and +ve linkl list will be displayed
}
}
//---------------------------------------------------
int countnode(node *s)
{
int count=0;
while(s!=NULL)
{
count++;
s=s->next;
}
return count;
}
//---------------------------------------------------
void sort(node*r,int cnt) //Bubble sort algorithm cch
{
int i,j,tmp;
node*t;
t=r;
for(i=1;i<cnt;i++)
{
r=t; //most IMP
for(j=1;j<cnt;j++)
{
if(r->no > (r->next)->no) //-5>-2
{
tmp=r->no;
r->no=(r->next)->no;
(r->next)->no=tmp;
}
r=r->next;
}
}
}
//---------------------------------------------------
void merge()
{
int ncount;
int pcount;
node*r1; //negative nos
r1=nroot;
node*r2; //positive nos
r2=proot;
ncount=countnode(nroot);
// cout<<"\nNEGATIVE COUNT : "<<ncount;
pcount=countnode(proot);
// cout<<"\nPOSITIVE COUNT : "<<pcount;
sort(r1,ncount);
sort(r2,pcount);
while(r1!=NULL)
{
curr=new node;
curr->no=r1->no;
curr->next=NULL;
if(sroot==NULL)
{
sroot=curr;
prev=curr;
}
else
{
prev->next=curr;
prev=curr;
}
r1=r1->next;
}
while(r2!=NULL)
{
curr=new node;
curr->no=r2->no ;
curr->next=NULL;
if(sroot==NULL)
{
sroot=curr;
prev=curr;
}
else
{
prev->next=curr;
prev=curr;
}
r2=r2->next;
}
cout<<"\nMerge list is : ";
display(sroot);
}
};
main()
{
list l1;
int op=-1;
while(op!=0)
{
cout<<"\n1.create list ";
cout<<"\n2.displsy list ";
cout<<"\n3.delete -ve no.s";
cout<<"\n4.+ve & -ve linklist";
cout<<"\n5.merge";
cout<<"\n6.exit";
cout<<"\n\nenter choice ";
cin>>op;
switch(op)
{
case 1:
l1.createlist();
break;
case 2:
l1.display(l1.getroot());
break;
case 3:
l1.delneg();
break;
case 4:
l1.splitlist(0); //negative linklist split
l1.splitlist(1); //positive linklist split
break;
case 5:
l1.merge();
break;
case 6:
op=0;
break;
}
}
}