Spaces:
Running
Running
File size: 1,478 Bytes
f11ab78 | 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 | 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;
}
}
}
|