test: CircleLinkedListTest (#5422)

* test: CircleLinkedListTest

* checkstyle: fix formatting

---------

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko
2024-08-28 18:31:39 +02:00
committed by GitHub
parent a9bc7c269d
commit 45563ccbde
2 changed files with 92 additions and 25 deletions

View File

@ -51,22 +51,25 @@ public class CircleLinkedList<E> {
size++;
}
// utility function for traversing the list
public String toString() {
Node<E> 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<E> 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<E> {
size--;
return saved;
}
public static void main(String[] args) {
CircleLinkedList<Integer> 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);
}
}