我想實現一個刪除(節點x)方法和一個搜索(E鍵)方法,但我不明白我如何可以使循環遍歷列表?我試圖寫入刪除方法。以下是我的代碼:如何在一個圓形單鏈表中實現刪除和搜索?
public class CircularSinglyLinkedList<E> {
private Node<E> head;
private Node<E> tail;
private class Node<E> {
public final E key;
public Node<E> next;
private Node(E key) {
this.key = key;
}
}
public void insert(Node<E> x) {
x.next = head;
tail.next = x;
head = x;
}
public void delete(Node<E> x) {
Node<E> y = head;
while(y != x && y.next != head) {
y = y.next;
}
if(y.next == x) {
y.next = x.next;
x.next = null;
}
}
public E search(E key) {
}
}
從頭部或指定節點搜索到結尾,1乘1. –
但是我會在循環中使用什麼條件? –
@如果你試圖找到一個元素,那麼做比較。 –