Spaces:
Running
Running
| public class CircularLinkedList<T> { | |
| private Node<T> head; | |
| private int size = 0; | |
| public void add(T data) { | |
| Node<T> newNode = new Node<>(data); | |
| if (head == null) { | |
| head = newNode; | |
| head.next = head; | |
| head.prev = head; | |
| } else { | |
| Node<T> last = head.prev; | |
| last.next = newNode; | |
| newNode.prev = last; | |
| newNode.next = head; | |
| head.prev = newNode; | |
| } | |
| size++; | |
| } | |
| public void rotateOneStep(int steps) { | |
| if (head == null || steps <= 0) | |
| { | |
| return; | |
| } | |
| for (int i = 0; i < steps; i++) { | |
| head = head.prev; | |
| } | |
| } | |
| public T get(int index) { | |
| if (index < 0 || index >= size); | |
| Node<T> current = head; | |
| for (int i = 0; i < index; i++) { | |
| current = current.next; | |
| } | |
| return current.data; | |
| } | |
| public void toArray(T[] array) { | |
| if (array.length < size) ; | |
| Node<T> curr = head; | |
| for (int i = 0; i < size; i++) { | |
| array[i] = curr.data; | |
| curr = curr.next; | |
| } | |
| } | |
| public int size() { | |
| return size; | |
| } | |
| public static class Node<T> { | |
| T data; | |
| Node<T> next; | |
| Node<T> prev; // ← burası eklendi | |
| public Node(T data) { | |
| this.data = data; | |
| } | |
| } | |
| } | |