Java 如何在循环单链接列表中实现删除和搜索?

Java 如何在循环单链接列表中实现删除和搜索?,java,algorithm,linked-list,infinite-loop,circular-list,Java,Algorithm,Linked List,Infinite Loop,Circular List,我试图实现一个delete(Node x)方法和一个search(E key)方法,但我不明白如何创建一个遍历列表的循环?我试着编写delete方法。以下是我目前的代码: public class CircularSinglyLinkedList<E> { private Node<E> head; private Node<E> tail; private class Node<E> { public

我试图实现一个delete(Node x)方法和一个search(E key)方法,但我不明白如何创建一个遍历列表的循环?我试着编写delete方法。以下是我目前的代码:

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) {

    }

}
公共类循环链接列表{
专用节点头;
私有节点尾部;
私有类节点{
公钥;
公共节点下一步;
专用节点(E密钥){
this.key=key;
}
}
公共空白插入(节点x){
x、 下一个=头部;
tail.next=x;
水头=x;
}
公共无效删除(节点x){
节点y=头部;
while(y!=x&&y.next!=head){
y=y.next;
}
如果(y.next==x){
y、 next=x.next;
x、 next=null;
}
}
公共电子搜索(E键){
}
}

您需要遍历循环列表以删除和搜索节点。我希望以下代码将有所帮助:

private Node delete(Node x) {
    Node node = head;
    do {
        if (node.next.element == x.element) {
            Node n = node.next;
            node.next = n.next;
            if (n == head) { // removal of head
                head = node;
            }
            return n;
        }
        node = node.next();
    } while(node != head);
    return null;
}
它将搜索节点x并将其删除。虽然你还没有发布你的
节点
类的结构,但我仍然希望你能做出相应的修改


函数将拒绝删除最后一个元素(当列表仅包含一个元素时),就像在循环链表中一样。我假设最后一个元素有指向下一个元素的头。

您需要遍历循环列表以删除和搜索节点。我希望以下代码将有所帮助:

private Node delete(Node x) {
    Node node = head;
    do {
        if (node.next.element == x.element) {
            Node n = node.next;
            node.next = n.next;
            if (n == head) { // removal of head
                head = node;
            }
            return n;
        }
        node = node.next();
    } while(node != head);
    return null;
}
它将搜索节点x并将其删除。虽然你还没有发布你的
节点
类的结构,但我仍然希望你能做出相应的修改


函数将拒绝删除最后一个元素(当列表仅包含一个元素时),就像在循环链表中一样。我假设最后一个元素有指向下一个元素的头。

您需要遍历循环列表以删除和搜索节点。我希望以下代码将有所帮助:

private Node delete(Node x) {
    Node node = head;
    do {
        if (node.next.element == x.element) {
            Node n = node.next;
            node.next = n.next;
            if (n == head) { // removal of head
                head = node;
            }
            return n;
        }
        node = node.next();
    } while(node != head);
    return null;
}
它将搜索节点x并将其删除。虽然你还没有发布你的
节点
类的结构,但我仍然希望你能做出相应的修改


函数将拒绝删除最后一个元素(当列表仅包含一个元素时),就像在循环链表中一样。我假设最后一个元素有指向下一个元素的头。

您需要遍历循环列表以删除和搜索节点。我希望以下代码将有所帮助:

private Node delete(Node x) {
    Node node = head;
    do {
        if (node.next.element == x.element) {
            Node n = node.next;
            node.next = n.next;
            if (n == head) { // removal of head
                head = node;
            }
            return n;
        }
        node = node.next();
    } while(node != head);
    return null;
}
它将搜索节点x并将其删除。虽然你还没有发布你的
节点
类的结构,但我仍然希望你能做出相应的修改


该函数将拒绝删除最后一个元素(当列表仅包含一个元素时),就像在循环链表中一样。我假设最后一个元素有头指向下一个元素。

您需要在
节点
类中实现
equals
hashCode
方法

while(!y.equals(x) && !y.next.equals(head)) {
    y = y.next;
}

您需要在
节点
类中实现
equals
hashCode
方法

while(!y.equals(x) && !y.next.equals(head)) {
    y = y.next;
}

您需要在
节点
类中实现
equals
hashCode
方法

while(!y.equals(x) && !y.next.equals(head)) {
    y = y.next;
}

您需要在
节点
类中实现
equals
hashCode
方法

while(!y.equals(x) && !y.next.equals(head)) {
    y = y.next;
}

我想,您只需将循环转换为while post condition循环,如下所示:

Node<E> y = head;
do {
    if (y.next == x) {
        y.next = x.next;
        x.next = null;
        return;
    }
    y = y.next;
} while (y != head);
节点y=头部;
做{
如果(y.next==x){
y、 next=x.next;
x、 next=null;
返回;
}
y=y.next;
}而(y!=头部);
最好在
search
方法中实现节点搜索(使其返回
node


顺便说一句,
delete
方法将
Node
作为参数,但是从外部调用它会很困难,因为调用者无法从任何地方获得对
Node
的引用:
head
tail
是私有的,
search
返回
E
,而不是
Node
,我想,您只需将循环转换为while post condition循环,如下所示:

Node<E> y = head;
do {
    if (y.next == x) {
        y.next = x.next;
        x.next = null;
        return;
    }
    y = y.next;
} while (y != head);
节点y=头部;
做{
如果(y.next==x){
y、 next=x.next;
x、 next=null;
返回;
}
y=y.next;
}而(y!=头部);
最好在
search
方法中实现节点搜索(使其返回
node


顺便说一句,
delete
方法将
Node
作为参数,但是从外部调用它会很困难,因为调用者无法从任何地方获得对
Node
的引用:
head
tail
是私有的,
search
返回
E
,而不是
Node
,我想,您只需将循环转换为while post condition循环,如下所示:

Node<E> y = head;
do {
    if (y.next == x) {
        y.next = x.next;
        x.next = null;
        return;
    }
    y = y.next;
} while (y != head);
节点y=头部;
做{
如果(y.next==x){
y、 next=x.next;
x、 next=null;
返回;
}
y=y.next;
}而(y!=头部);
最好在
search
方法中实现节点搜索(使其返回
node


顺便说一句,
delete
方法将
Node
作为参数,但是从外部调用它会很困难,因为调用者无法从任何地方获得对
Node
的引用:
head
tail
是私有的,
search
返回
E
,而不是
Node
,我想,您只需将循环转换为while post condition循环,如下所示:

Node<E> y = head;
do {
    if (y.next == x) {
        y.next = x.next;
        x.next = null;
        return;
    }
    y = y.next;
} while (y != head);
节点y=头部;
做{
如果(y.next==x){
y、 next=x.next;
x、 next=null;
返回;
}
y=y.next;
}而(y!=头部);
最好在
search
方法中实现节点搜索(使其返回
node

顺便说一句,
delete
方法将
Node
作为参数,但从外部调用它会很困难,因为调用者无法从外部获取对
Node
的引用