-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle.java
522 lines (467 loc) · 14.3 KB
/
single.java
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import java.util.*;
class single {
node head = null;
node tail = null;
node first = null;
int size = 0;
class node {
int data;
node next;
node(int data) {
this.data = data;
this.next = null;
size++;
}
}
public void creation() {
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter how many data you want to insert in the linkedlist");
n = sc.nextInt();
System.out.println("Enter the data");
for (int i = 0; i < n; i++) {
int data = sc.nextInt();
node newnode = new node(data);
if (head == null) {
head = tail = newnode;
} else {
tail.next = newnode;
tail = newnode;
}
}
}
public void display() {
if (head == null) {
System.out.println("No linklist exist");
} else {
node curr = head;
while (curr != null) {
System.out.println(curr.data);
curr = curr.next;
}
}
}
public void addfirst(int data) {
node newnode = new node(data);
if (head == null) {
head = tail = newnode;
} else {
newnode.next = head;
head = newnode;
}
}
public void addbefore(int index, int data) {
node newnode = new node(data);
if (head == null) {
head = tail = newnode;
} else if (index <= 2) {
addfirst(data);
} else {
node curr = head;
for (int i = 1; i < index - 2; i++) {
curr = curr.next;
}
node nex = curr.next;
curr.next = newnode;
newnode.next = nex;
}
}
public void addafter(int index, int data) {
node newnode = new node(data);
if (head == null) {
head = tail = newnode;
} else if (index == 1) {
node nex = head.next;
head.next = newnode;
newnode.next = nex;
} else {
node curr = head;
for (int i = 1; i < index; i++) {
curr = curr.next;
}
node nex = curr.next;
curr.next = newnode;
newnode.next = nex;
}
}
public void addlast(int data) {
node newnode = new node(data);
if (head == null) {
head = tail = newnode;
} else {
node curr = head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = newnode;
}
}
public void addatindex(int index, int data) {
node newnode = new node(data);
if (head == null) {
head = tail = newnode;
} else if (index == 1) {
addfirst(data);
} else {
node curr = head;
for (int i = 1; i < index - 1; i++) {
curr = curr.next;
}
node nex = curr.next;
curr.next = newnode;
newnode.next = nex;
}
}
public void deleteatindex(int index) {
if (head == null) {
System.out.println("No link list exist");
} else if (index <= 0 || index >= size) {
System.out.println("Invalid index");
} else if (index == 1) {
deletefirst();
} else {
node curr = head;
for (int i = 1; i < index - 1; i++) {
curr = curr.next;
}
node nex = curr.next;
System.out.println(curr.next.data + "is deleted");
curr.next = nex.next;
size--;
}
}
public void deletefirst() {
if (head == null) {
System.out.println("No linklist exist");
} else if (head.next == null) {
System.out.println(head.data + " is deleted");
head = tail = null;
size--;
} else {
System.out.println(head.data + " is deleted");
head = head.next;
size--;
}
}
public void deletelast() {
if (head == null) {
System.out.println("No linklist exist");
} else if (head.next == null) {
System.out.println(head.data + " is deleted");
head = tail = null;
size--;
} else {
node curr = head;
while (curr.next.next != null) {
curr = curr.next;
}
System.out.println(curr.next.data + " is deleted");
curr.next = null;
size--;
}
}
public void updateatindex(int index, int data) {
if (head == null) {
System.out.println("No linkedlist exist");
} else if (index <= 0) {
System.out.println("invalid index");
} else {
node curr = head;
for (int i = 1; i < index; i++) {
curr = curr.next;
}
curr.data = data;
}
}
public void getsize() {
System.out.println("The size is " + size);
}
public void bubblesortascending() {
if (head == null) {
return;
}
node curr = head;
while (curr != null) {
node index = curr.next;
while (index != null) {
if (curr.data > index.data) {
int temp = curr.data;
curr.data = index.data;
index.data = temp;
}
index = index.next;
}
curr = curr.next;
}
}
public void bubblesortdecending() {
if (head == null) {
System.out.println("The linklist doesnot exist");
return;
}
node curr = head;
while (curr != null) {
node boy = curr.next;
while (boy != null) {
if (curr.data < boy.data) {
int temp = curr.data;
curr.data = boy.data;
boy.data = temp;
}
boy = boy.next;
}
curr = curr.next;
}
}
public void insertionsort() {
// if (head == null) {
// return;
// }
// node curr = head;
// while (curr != null) {
// node index = curr.next;
// while (index != null) {
// if (curr.data > index.data) {
// int temp = curr.data;
// index.data = curr.data;
// curr.data = temp;
// }
// index = index.next;
// }
// curr = curr.next;
// }
}
public void reverse() {
node prev = null;
node curr = head;
node nex = head;
while (nex != null) {
nex = nex.next;
curr.next = prev;
prev = curr;
curr = nex;
}
head = prev;
}
public void concatenate(single s, single t) {
node curr = s.head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = t.head;
}
public static void printalternatenode(node s) {
if (s == null) {
return;
}
node curr = s;
int i = 0;
while (curr != null) {
if (i % 2 == 0) {
System.out.println(curr.data);
}
i++;
curr = curr.next;
}
}
// public static void storealternatenode(node s, node t) {
// if (s == null) {
// return;
// }
// node curr = s;
// int i = 0;
// while (curr != null) {
// if (i % 2 == 0) {
// // System.out.println(curr.data);
// if (t == null) {
// t = curr;
// } else {
// node test = t;
// while (test.next != null) {
// test = test.next;
// }
// test.next = curr;
// }
// }
// i++;
// curr = curr.next;
// }
// }
public single skipalternatestorereaming() {
single result = new single();
if (head == null && head.next == null) {
return result;
}
node curr = head;
while (curr != null && curr.next != null) {
result.addlast(curr.data);
curr = curr.next.next;
}
if (curr != null) {
result.addlast(curr.data);
}
return result;
}
public static void print(node head) {
if (head == null) {
System.out.println("list is empty");
return;
}
node temp = head;
while (temp != null) {
System.out.print(temp.data + "->");
temp = temp.next;
}
System.out.println("null");
}
public single[] split(int count) {
single firstlist = new single();
single secondlist = new single();
if (head == null) {
return new single[] { firstlist, secondlist };
}
int cou = 0;
node curr = head;
while (curr != null && cou < count) {
firstlist.addlast(curr.data);
curr = curr.next;
cou++;
}
while (curr != null) {
secondlist.addlast(curr.data);
curr = curr.next;
}
return new single[] { firstlist, secondlist };
}
public single[] splitwithoutcount() {
single firstlist = new single();
single secondlist = new single();
if (head == null) {
return new single[] { firstlist, secondlist };
}
node prev = null;
node slow = head;
node fast = head;
while (fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
if (fast != null) {
slow = slow.next;
}
if (prev != null) {
prev.next = null;
} else {
firstlist.head = head;
}
firstlist.head = head;
secondlist.head = slow;
return new single[] { firstlist, secondlist };
}
public void removeduplicate() {
if (head == null) {
return;
}
node curr = head;
while (curr != null) {
node check = curr;
while (check.next != null) {
if (curr.data == check.next.data) {
check.next = check.next.next;
} else {
check = check.next;
}
}
curr = curr.next;
}
}
public void largestelement() {
if (head == null) {
System.out.println("The linklist doesnot exist");
return;
}
node curr = head.next;
int max = head.data;
while (curr != null) {
if (curr.data > max) {
max = curr.data;
}
curr = curr.next;
}
System.out.println("The largest element in the linkedlist is " + max);
}
public static void main(String args[]) {
// System.out.println("Hi this is subodh narayan sah");
single s = new single();
s.creation();
// System.out.println("The element in the linkedlist is");
// s.display();
single p = new single();
// t.creation();
// System.out.println("The element in the linkedlist is");
// t.display();
// s.addfirst(6);
// s.addfirst(5);
// s.addfirst(4);
// s.addfirst(2);
// s.addfirst(1);
// System.out.println("The element in the linkedlist is");
// s.display();
// s.addbefore(2, 3);
// s.addlast(7);
// s.addafter(1, 9);
// System.out.println("The element in the linkedlist is");
// s.display();
// System.out.println("The insetion of element at index in the linkedlist is");
// s.addatindex(3, 27);
// s.display();
// System.out.println("The deletion of element at index in the linkedlist is");
// s.deleteatindex(1);
// s.display();
// System.out.println("The deletion of element at first and last in the
// linkedlist is");
// s.deletefirst();
// s.deletelast();
// s.display();
// System.out.println("The upadation of element at index in the linkedlist is");
// s.updateatindex(3, 0);
// s.display();
// s.getsize();
// System.out.println("The sorting of element by bubble sort in the linkedlist
// is");
// s.bubblesortdecending();
// s.display();
// System.out.println("The linkedlist after reverse");
// s.reverse();
// s.display();
// single con = new single();
// s.concatenate(s, t);
// System.out.println("Print alternate");
// printalternatenode(s.head);
// System.out.println("skip alternate and store remaining in new liked list");
// single result = s.skipalternatestorereaming();
// result.display();
// single[] splitlist = s.split(3);
// single firstlist = splitlist[0];
// single secondlist = splitlist[1];
// System.out.println("First list: count =3");
// firstlist.display();
// System.out.println("Secong list");
// secondlist.display();
// System.out.println("split linkedlist wihtout count");
// single[] result2 = s.splitwithoutcount();
// single firstlist1 = result2[0];
// single secondlist1 = result2[1];
// System.out.println("First list");
// firstlist1.display();
// System.out.println("Second list");
// secondlist1.display();
System.out.println("Entered data");
s.display();
s.removeduplicate();
System.out.println("The linklist element without duplicates");
s.display();
s.largestelement();
}
}