Repository navigation
Expand file tree
/
Copy pathLinkedListCodeRepository.java
More file actions
3347 lines (2809 loc) · 135 KB
/
Copy pathLinkedListCodeRepository.java
File metadata and controls
3347 lines (2809 loc) · 135 KB
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.simulator;
import java.util.HashMap;
import java.util.Map;
public class LinkedListCodeRepository {
private final Map<String, CodeExample> codeExamples;
public LinkedListCodeRepository() {
codeExamples = new HashMap<>();
initializeCodeExamples();
}
private void initializeCodeExamples() {
// =============== BASIC OPERATIONS ===============
codeExamples.put("Create LinkedList", new CodeExample(
"Create LinkedList",
"Basic LinkedList class structure with Node definition",
"O(1)",
"""
// LinkedList class with Node structure
public class LinkedList {
private Node head;
private int size;
// Inner Node class
private static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
// Constructor
public LinkedList() {
this.head = null;
this.size = 0;
}
// Check if list is empty
public boolean isEmpty() {
return head == null;
}
// Get size of list
public int size() {
return size;
}
}
"""
));
codeExamples.put("Insert at Beginning", new CodeExample(
"Insert at Beginning",
"Insert a new node at the beginning of the LinkedList",
"O(1)",
"""
// Insert element at the beginning
public void insertAtBeginning(int data) {
// Create new node
Node newNode = new Node(data);
// Point new node to current head
newNode.next = head;
// Update head to new node
head = newNode;
// Increment size
size++;
System.out.println("Inserted " + data + " at beginning");
}
// Usage Example:
LinkedList list = new LinkedList();
list.insertAtBeginning(10);
list.insertAtBeginning(20);
list.insertAtBeginning(30);
// Result: 30 -> 20 -> 10 -> null
"""
));
codeExamples.put("Insert at End", new CodeExample(
"Insert at End",
"Insert a new node at the end of the LinkedList",
"O(n)",
"""
// Insert element at the end
public void insertAtEnd(int data) {
Node newNode = new Node(data);
// If list is empty, make new node the head
if (head == null) {
head = newNode;
} else {
// Traverse to the last node
Node current = head;
while (current.next != null) {
current = current.next;
}
// Link last node to new node
current.next = newNode;
}
size++;
System.out.println("Inserted " + data + " at end");
}
// Usage Example:
LinkedList list = new LinkedList();
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
// Result: 10 -> 20 -> 30 -> null
"""
));
codeExamples.put("Insert at Position", new CodeExample(
"Insert at Position",
"Insert a new node at a specific position in the LinkedList",
"O(n)",
"""
// Insert element at specific position
public void insertAtPosition(int position, int data) {
// Validate position
if (position < 0 || position > size) {
throw new IndexOutOfBoundsException("Invalid position: " + position);
}
// If inserting at beginning
if (position == 0) {
insertAtBeginning(data);
return;
}
Node newNode = new Node(data);
Node current = head;
// Traverse to position-1
for (int i = 0; i < position - 1; i++) {
current = current.next;
}
// Insert new node
newNode.next = current.next;
current.next = newNode;
size++;
System.out.println("Inserted " + data + " at position " + position);
}
// Usage Example:
LinkedList list = new LinkedList();
list.insertAtEnd(10);
list.insertAtEnd(30);
list.insertAtPosition(1, 20);
// Result: 10 -> 20 -> 30 -> null
"""
));
codeExamples.put("Search Element", new CodeExample(
"Search Element",
"Search for an element in the LinkedList and return its position",
"O(n)",
"""
// Search for element and return position
public int search(int data) {
Node current = head;
int position = 0;
// Traverse the list
while (current != null) {
if (current.data == data) {
System.out.println("Found " + data + " at position " + position);
return position;
}
current = current.next;
position++;
}
System.out.println(data + " not found in the list");
return -1; // Element not found
}
// Search with boolean return
public boolean contains(int data) {
return search(data) != -1;
}
// Usage Example:
LinkedList list = new LinkedList();
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
int position = list.search(20); // Returns 1
boolean exists = list.contains(25); // Returns false
"""
));
codeExamples.put("Delete Element", new CodeExample(
"Delete Element",
"Delete the first occurrence of an element from the LinkedList",
"O(n)",
"""
// Delete first occurrence of element
public boolean delete(int data) {
// Empty list
if (head == null) {
return false;
}
// If head node contains the data
if (head.data == data) {
head = head.next;
size--;
System.out.println("Deleted " + data + " from beginning");
return true;
}
Node current = head;
// Find the node to delete
while (current.next != null && current.next.data != data) {
current = current.next;
}
// If element found
if (current.next != null) {
current.next = current.next.next;
size--;
System.out.println("Deleted " + data);
return true;
}
System.out.println(data + " not found for deletion");
return false; // Element not found
}
// Usage Example:
LinkedList list = new LinkedList();
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
boolean deleted = list.delete(20); // Returns true
// Result: 10 -> 30 -> null
"""
));
codeExamples.put("Delete at Position", new CodeExample(
"Delete at Position",
"Delete element at a specific position from the LinkedList",
"O(n)",
"""
// Delete element at specific position
public boolean deleteAtPosition(int position) {
// Validate position
if (position < 0 || position >= size || head == null) {
return false;
}
// If deleting head
if (position == 0) {
int data = head.data;
head = head.next;
size--;
System.out.println("Deleted " + data + " at position " + position);
return true;
}
Node current = head;
// Traverse to position-1
for (int i = 0; i < position - 1; i++) {
current = current.next;
}
int data = current.next.data;
current.next = current.next.next;
size--;
System.out.println("Deleted " + data + " at position " + position);
return true;
}
// Usage Example:
LinkedList list = new LinkedList();
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
boolean deleted = list.deleteAtPosition(1); // Deletes 20
// Result: 10 -> 30 -> null
"""
));
codeExamples.put("Display List", new CodeExample(
"Display List",
"Display all elements in the LinkedList",
"O(n)",
"""
// Display all elements
public void display() {
if (head == null) {
System.out.println("List is empty");
return;
}
Node current = head;
System.out.print("LinkedList: ");
while (current != null) {
System.out.print(current.data);
if (current.next != null) {
System.out.print(" -> ");
}
current = current.next;
}
System.out.println(" -> null");
}
// Get as array
public int[] toArray() {
int[] array = new int[size];
Node current = head;
int index = 0;
while (current != null) {
array[index++] = current.data;
current = current.next;
}
return array;
}
// toString method
@Override
public String toString() {
if (head == null) {
return "[]";
}
StringBuilder sb = new StringBuilder("[");
Node current = head;
while (current != null) {
sb.append(current.data);
if (current.next != null) {
sb.append(", ");
}
current = current.next;
}
sb.append("]");
return sb.toString();
}
"""
));
// =============== SINGLY LINKEDLIST ===============
codeExamples.put("Node Class", new CodeExample(
"Singly LinkedList Node",
"Node class implementation for Singly LinkedList",
"O(1)",
"""
// Node class for Singly LinkedList
public class SinglyNode {
int data; // Data stored in the node
SinglyNode next; // Reference to next node
// Constructor
public SinglyNode(int data) {
this.data = data;
this.next = null;
}
// Constructor with next node
public SinglyNode(int data, SinglyNode next) {
this.data = data;
this.next = next;
}
// Getter methods
public int getData() {
return data;
}
public SinglyNode getNext() {
return next;
}
// Setter methods
public void setData(int data) {
this.data = data;
}
public void setNext(SinglyNode next) {
this.next = next;
}
@Override
public String toString() {
return "Node{" + data + "}";
}
}
"""
));
codeExamples.put("Complete Implementation", new CodeExample(
"Complete Singly LinkedList",
"Full implementation of Singly LinkedList with all operations",
"O(1) to O(n)",
"""
// Complete Singly LinkedList Implementation
public class SinglyLinkedList {
private SinglyNode head;
private SinglyNode tail;
private int size;
// Constructor
public SinglyLinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
// Add element at beginning
public void addFirst(int data) {
SinglyNode newNode = new SinglyNode(data);
if (isEmpty()) {
head = tail = newNode;
} else {
newNode.next = head;
head = newNode;
}
size++;
}
// Add element at end
public void addLast(int data) {
SinglyNode newNode = new SinglyNode(data);
if (isEmpty()) {
head = tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
size++;
}
// Remove first element
public int removeFirst() {
if (isEmpty()) {
throw new RuntimeException("List is empty");
}
int data = head.data;
head = head.next;
if (head == null) {
tail = null;
}
size--;
return data;
}
// Remove last element
public int removeLast() {
if (isEmpty()) {
throw new RuntimeException("List is empty");
}
if (size == 1) {
return removeFirst();
}
// Find second last node
SinglyNode current = head;
while (current.next != tail) {
current = current.next;
}
int data = tail.data;
tail = current;
tail.next = null;
size--;
return data;
}
// Utility methods
public boolean isEmpty() {
return head == null;
}
public int size() {
return size;
}
public int getFirst() {
if (isEmpty()) {
throw new RuntimeException("List is empty");
}
return head.data;
}
public int getLast() {
if (isEmpty()) {
throw new RuntimeException("List is empty");
}
return tail.data;
}
}
"""
));
// ✅ NEW: Traversal Methods
codeExamples.put("Traversal Methods", new CodeExample(
"Singly LinkedList Traversal",
"Various traversal methods for Singly LinkedList including iterative and recursive approaches",
"O(n)",
"""
// Forward traversal methods for Singly LinkedList
// Iterative traversal
public void traverse() {
if (head == null) {
System.out.println("List is empty");
return;
}
SinglyNode current = head;
System.out.print("List: ");
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
// Recursive traversal
public void traverseRecursive() {
System.out.print("List (Recursive): ");
traverseRecursiveHelper(head);
System.out.println();
}
private void traverseRecursiveHelper(SinglyNode node) {
if (node != null) {
System.out.print(node.data + " ");
traverseRecursiveHelper(node.next);
}
}
// Reverse traversal using recursion
public void traverseReverse() {
System.out.print("List (Reverse): ");
traverseReverseHelper(head);
System.out.println();
}
private void traverseReverseHelper(SinglyNode node) {
if (node != null) {
traverseReverseHelper(node.next);
System.out.print(node.data + " ");
}
}
// Get element at specific position
public int get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
SinglyNode current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current.data;
}
// Usage Example:
SinglyLinkedList list = new SinglyLinkedList();
list.addLast(10);
list.addLast(20);
list.addLast(30);
list.traverse(); // Output: List: 10 20 30
list.traverseRecursive(); // Output: List (Recursive): 10 20 30
list.traverseReverse(); // Output: List (Reverse): 30 20 10
"""
));
// ✅ NEW: Size & Empty Check
codeExamples.put("Size & Empty Check", new CodeExample(
"Size and Empty Check Operations",
"Various utility methods to check size, emptiness, and validate LinkedList state",
"O(1) to O(n)",
"""
// Size and empty check operations for LinkedList
// Check if list is empty - O(1)
public boolean isEmpty() {
return head == null;
}
// Get current size (if maintained) - O(1)
public int size() {
return size;
}
// Calculate size by counting - O(n)
public int calculateSize() {
int count = 0;
SinglyNode current = head;
while (current != null) {
count++;
current = current.next;
}
return count;
}
// Check if list has only one element
public boolean hasSingleElement() {
return head != null && head.next == null;
}
// Check if list has at least n elements
public boolean hasAtLeastNElements(int n) {
if (n <= 0) return true;
int count = 0;
SinglyNode current = head;
while (current != null && count < n) {
count++;
current = current.next;
}
return count >= n;
}
// Get length without using size variable
public int length() {
int length = 0;
SinglyNode current = head;
while (current != null) {
length++;
current = current.next;
}
return length;
}
// Validate list integrity
public boolean isValid() {
if (head == null) {
return size == 0;
}
int actualSize = calculateSize();
return actualSize == size;
}
// Compare sizes of two lists
public static int compareSizes(SinglyLinkedList list1, SinglyLinkedList list2) {
int size1 = list1.size();
int size2 = list2.size();
if (size1 < size2) return -1;
if (size1 > size2) return 1;
return 0;
}
// Usage Example:
SinglyLinkedList list = new SinglyLinkedList();
System.out.println("Is empty: " + list.isEmpty()); // true
System.out.println("Size: " + list.size()); // 0
list.addLast(10);
list.addLast(20);
System.out.println("Is empty: " + list.isEmpty()); // false
System.out.println("Size: " + list.size()); // 2
System.out.println("Has single element: " + list.hasSingleElement()); // false
System.out.println("Has at least 3 elements: " + list.hasAtLeastNElements(3)); // false
"""
));
// =============== DOUBLY LINKEDLIST ===============
codeExamples.put("Node Class", new CodeExample(
"Doubly LinkedList Node",
"Node class implementation for Doubly LinkedList with previous pointer",
"O(1)",
"""
// Node class for Doubly LinkedList
public class DoublyNode {
int data; // Data stored in the node
DoublyNode next; // Reference to next node
DoublyNode prev; // Reference to previous node
// Constructor
public DoublyNode(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
// Constructor with next and previous nodes
public DoublyNode(int data, DoublyNode prev, DoublyNode next) {
this.data = data;
this.prev = prev;
this.next = next;
}
// Getter methods
public int getData() {
return data;
}
public DoublyNode getNext() {
return next;
}
public DoublyNode getPrev() {
return prev;
}
// Setter methods
public void setData(int data) {
this.data = data;
}
public void setNext(DoublyNode next) {
this.next = next;
}
public void setPrev(DoublyNode prev) {
this.prev = prev;
}
@Override
public String toString() {
return "DoublyNode{" + data + "}";
}
}
"""
));
// ✅ NEW: Doubly LinkedList Complete Implementation
codeExamples.put("Complete Implementation", new CodeExample(
"Complete Doubly LinkedList",
"Full implementation of Doubly LinkedList with all operations and bidirectional pointers",
"O(1) to O(n)",
"""
// Complete Doubly LinkedList Implementation
public class DoublyLinkedList {
private DoublyNode head;
private DoublyNode tail;
private int size;
// Constructor
public DoublyLinkedList() {
this.head = null;
this.tail = null;
this.size = 0;
}
// Check if list is empty
public boolean isEmpty() {
return head == null;
}
// Get size of list
public int size() {
return size;
}
// Add element at beginning - O(1)
public void addFirst(int data) {
DoublyNode newNode = new DoublyNode(data);
if (isEmpty()) {
head = tail = newNode;
} else {
newNode.next = head;
head.prev = newNode;
head = newNode;
}
size++;
}
// Add element at end - O(1)
public void addLast(int data) {
DoublyNode newNode = new DoublyNode(data);
if (isEmpty()) {
head = tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
size++;
}
// Remove first element - O(1)
public int removeFirst() {
if (isEmpty()) {
throw new RuntimeException("List is empty");
}
int data = head.data;
head = head.next;
if (head != null) {
head.prev = null;
} else {
tail = null; // List becomes empty
}
size--;
return data;
}
// Remove last element - O(1)
public int removeLast() {
if (isEmpty()) {
throw new RuntimeException("List is empty");
}
int data = tail.data;
tail = tail.prev;
if (tail != null) {
tail.next = null;
} else {
head = null; // List becomes empty
}
size--;
return data;
}
// Insert at specific position - O(n)
public void insertAtPosition(int position, int data) {
if (position < 0 || position > size) {
throw new IndexOutOfBoundsException("Invalid position: " + position);
}
if (position == 0) {
addFirst(data);
return;
}
if (position == size) {
addLast(data);
return;
}
DoublyNode newNode = new DoublyNode(data);
DoublyNode current = head;
// Traverse to position
for (int i = 0; i < position; i++) {
current = current.next;
}
// Insert new node before current
newNode.next = current;
newNode.prev = current.prev;
current.prev.next = newNode;
current.prev = newNode;
size++;
}
// Delete at specific position - O(n)
public boolean deleteAtPosition(int position) {
if (position < 0 || position >= size || isEmpty()) {
return false;
}
if (position == 0) {
removeFirst();
return true;
}
if (position == size - 1) {
removeLast();
return true;
}
DoublyNode current = head;
// Traverse to position
for (int i = 0; i < position; i++) {
current = current.next;
}
// Remove current node
current.prev.next = current.next;
current.next.prev = current.prev;
size--;
return true;
}
// Search for element - O(n)
public int search(int data) {
DoublyNode current = head;
int position = 0;
while (current != null) {
if (current.data == data) {
return position;
}
current = current.next;
position++;
}
return -1; // Not found
}
// Get first element
public int getFirst() {
if (isEmpty()) {
throw new RuntimeException("List is empty");
}
return head.data;
}
// Get last element
public int getLast() {
if (isEmpty()) {
throw new RuntimeException("List is empty");
}
return tail.data;
}
// Usage Example:
DoublyLinkedList dll = new DoublyLinkedList();
dll.addLast(10);
dll.addLast(20);
dll.addFirst(5);
dll.insertAtPosition(2, 15);
// Result: 5 <-> 10 <-> 15 <-> 20
}
"""
));
// ✅ NEW: Bidirectional Traversal
codeExamples.put("Bidirectional Traversal", new CodeExample(
"Doubly LinkedList Bidirectional Traversal",
"Forward and backward traversal methods for Doubly LinkedList",
"O(n)",
"""
// Bidirectional traversal methods for Doubly LinkedList
// Forward traversal (head to tail)
public void traverseForward() {
if (isEmpty()) {
System.out.println("List is empty");
return;
}
System.out.print("Forward: ");
DoublyNode current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
// Backward traversal (tail to head)
public void traverseBackward() {
if (isEmpty()) {
System.out.println("List is empty");
return;
}
System.out.print("Backward: ");
DoublyNode current = tail;
while (current != null) {
System.out.print(current.data + " ");
current = current.prev;
}
System.out.println();
}
// Recursive forward traversal
public void traverseForwardRecursive() {
System.out.print("Forward (Recursive): ");
traverseForwardHelper(head);
System.out.println();
}
private void traverseForwardHelper(DoublyNode node) {
if (node != null) {
System.out.print(node.data + " ");
traverseForwardHelper(node.next);
}
}
// Recursive backward traversal
public void traverseBackwardRecursive() {
System.out.print("Backward (Recursive): ");
traverseBackwardHelper(tail);
System.out.println();
}
private void traverseBackwardHelper(DoublyNode node) {
if (node != null) {
System.out.print(node.data + " ");
traverseBackwardHelper(node.prev);
}
}
// Traverse from specific position in both directions
public void traverseFromPosition(int position) {
if (position < 0 || position >= size) {
System.out.println("Invalid position");
return;
}
// Find the node at position
DoublyNode current = head;
for (int i = 0; i < position; i++) {
current = current.next;
}
System.out.println("Traversing from position " + position + " (value: " + current.data + ")");