diff --git a/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java index 6eb9d75fe..2b50f7310 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java @@ -51,22 +51,25 @@ public class CircleLinkedList { size++; } - // utility function for traversing the list public String toString() { - Node p = head.next; - String s = "[ "; - while (p != head) { - s += p.value; - if (p != tail) { - s += " , "; - } - p = p.next; + if (size == 0) { + return "[]"; } - return s + " ]"; + StringBuilder sb = new StringBuilder("[ "); + Node current = head.next; + while (current != head) { + sb.append(current.value); + if (current.next != head) { + sb.append(", "); + } + current = current.next; + } + sb.append(" ]"); + return sb.toString(); } public E remove(int pos) { - if (pos > size || pos < 0) { + if (pos >= size || pos < 0) { // catching errors throw new IndexOutOfBoundsException("position cannot be greater than size or negative"); } @@ -89,18 +92,4 @@ public class CircleLinkedList { size--; return saved; } - - public static void main(String[] args) { - CircleLinkedList cl = new CircleLinkedList<>(); - cl.append(12); - System.out.println(cl); - cl.append(23); - System.out.println(cl); - cl.append(34); - System.out.println(cl); - cl.append(56); - System.out.println(cl); - cl.remove(3); - System.out.println(cl); - } } diff --git a/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java new file mode 100644 index 000000000..b7a05ef29 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java @@ -0,0 +1,78 @@ +package com.thealgorithms.datastructures.lists; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +public class CircleLinkedListTest { + + @Test + public void testAppendAndSize() { + CircleLinkedList list = new CircleLinkedList<>(); + list.append(1); + list.append(2); + list.append(3); + + assertEquals(3, list.getSize()); + assertEquals("[ 1, 2, 3 ]", list.toString()); + } + + @Test + public void testRemove() { + CircleLinkedList list = new CircleLinkedList<>(); + list.append(1); + list.append(2); + list.append(3); + list.append(4); + + assertEquals(2, list.remove(1)); + assertEquals(3, list.remove(1)); + assertEquals("[ 1, 4 ]", list.toString()); + assertEquals(2, list.getSize()); + } + + @Test + public void testRemoveInvalidIndex() { + CircleLinkedList list = new CircleLinkedList<>(); + list.append(1); + list.append(2); + + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(2)); + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1)); + } + + @Test + public void testToStringEmpty() { + CircleLinkedList list = new CircleLinkedList<>(); + assertEquals("[]", list.toString()); + } + + @Test + public void testToStringAfterRemoval() { + CircleLinkedList list = new CircleLinkedList<>(); + list.append(1); + list.append(2); + list.append(3); + list.remove(1); + + assertEquals("[ 1, 3 ]", list.toString()); + } + + @Test + public void testSingleElement() { + CircleLinkedList list = new CircleLinkedList<>(); + list.append(1); + + assertEquals(1, list.getSize()); + assertEquals("[ 1 ]", list.toString()); + assertEquals(1, list.remove(0)); + assertEquals("[]", list.toString()); + } + + @Test + public void testNullElement() { + CircleLinkedList list = new CircleLinkedList<>(); + assertThrows(NullPointerException.class, () -> list.append(null)); + } +}