11package com .thealgorithms .datastructures .lists ;
22
3+ /**
4+ * This class is a circular singly linked list implementation. In a circular linked list,
5+ * the last node points back to the first node, creating a circular chain.
6+ *
7+ * <p>This implementation includes basic operations such as appending elements
8+ * to the end, removing elements from a specified position, and converting
9+ * the list to a string representation.
10+ *
11+ * @param <E> the type of elements held in this list
12+ */
313public class CircleLinkedList <E > {
414
5- private static final class Node <E > {
15+ /**
16+ * A static nested class representing a node in the circular linked list.
17+ *
18+ * @param <E> the type of element stored in the node
19+ */
20+ static final class Node <E > {
621
722 Node <E > next ;
823 E value ;
@@ -13,44 +28,56 @@ private Node(E value, Node<E> next) {
1328 }
1429 }
1530
16- // For better O.O design this should be private allows for better black box design
1731 private int size ;
18- // this will point to dummy node;
19- private Node <E > head = null ;
20- private Node <E > tail = null ; // keeping a tail pointer to keep track of the end of list
32+ Node <E > head = null ;
33+ private Node <E > tail ;
2134
22- // constructor for class.. here we will make a dummy node for circly linked list implementation
23- // with reduced error catching as our list will never be empty;
35+ /**
36+ * Initializes a new circular linked list. A dummy head node is used for simplicity,
37+ * pointing initially to itself to ensure the list is never empty.
38+ */
2439 public CircleLinkedList () {
25- // creation of the dummy node
26- head = new Node <E >(null , head );
40+ head = new Node <>(null , head );
2741 tail = head ;
2842 size = 0 ;
2943 }
3044
31- // getter for the size... needed because size is private.
45+ /**
46+ * Returns the current size of the list.
47+ *
48+ * @return the number of elements in the list
49+ */
3250 public int getSize () {
3351 return size ;
3452 }
3553
36- // for the sake of simplistiy this class will only contain the append function or addLast other
37- // add functions can be implemented however this is the basses of them all really.
54+ /**
55+ * Appends a new element to the end of the list. Throws a NullPointerException if
56+ * a null value is provided.
57+ *
58+ * @param value the value to append to the list
59+ * @throws NullPointerException if the value is null
60+ */
3861 public void append (E value ) {
3962 if (value == null ) {
40- // we do not want to add null elements to the list.
4163 throw new NullPointerException ("Cannot add null element to the list" );
4264 }
43- // head.next points to the last element;
4465 if (tail == null ) {
45- tail = new Node <E >(value , head );
66+ tail = new Node <>(value , head );
4667 head .next = tail ;
4768 } else {
48- tail .next = new Node <E >(value , head );
69+ tail .next = new Node <>(value , head );
4970 tail = tail .next ;
5071 }
5172 size ++;
5273 }
5374
75+ /**
76+ * Returns a string representation of the list in the format "[ element1, element2, ... ]".
77+ * An empty list is represented as "[]".
78+ *
79+ * @return the string representation of the list
80+ */
5481 public String toString () {
5582 if (size == 0 ) {
5683 return "[]" ;
@@ -68,23 +95,27 @@ public String toString() {
6895 return sb .toString ();
6996 }
7097
98+ /**
99+ * Removes and returns the element at the specified position in the list.
100+ * Throws an IndexOutOfBoundsException if the position is invalid.
101+ *
102+ * @param pos the position of the element to remove
103+ * @return the value of the removed element
104+ * @throws IndexOutOfBoundsException if the position is out of range
105+ */
71106 public E remove (int pos ) {
72107 if (pos >= size || pos < 0 ) {
73- // catching errors
74- throw new IndexOutOfBoundsException ("position cannot be greater than size or negative" );
108+ throw new IndexOutOfBoundsException ("Position out of bounds" );
75109 }
76- // we need to keep track of the element before the element we want to remove we can see why
77- // bellow.
110+
78111 Node <E > before = head ;
79112 for (int i = 1 ; i <= pos ; i ++) {
80113 before = before .next ;
81114 }
82115 Node <E > destroy = before .next ;
83116 E saved = destroy .value ;
84- // assigning the next reference to the element following the element we want to remove...
85- // the last element will be assigned to the head.
86- before .next = before .next .next ;
87- // scrubbing
117+ before .next = destroy .next ;
118+
88119 if (destroy == tail ) {
89120 tail = before ;
90121 }
0 commit comments