Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 链表删除和insertAfter方法_Java_Methods_Linked List - Fatal编程技术网

Java 链表删除和insertAfter方法

Java 链表删除和insertAfter方法,java,methods,linked-list,Java,Methods,Linked List,我在使用delete和insertAfter方法时遇到了问题,这些方法用于执行操作并返回一个布尔值,如果操作成功(true)或失败(false)。InsertAfter方法将插入一个字符串,但始终在同一位置,Delete方法将始终删除同一节点 private class StrNode { String data; StrNode next; } private StrNode head; // the head of the singly

我在使用delete和insertAfter方法时遇到了问题,这些方法用于执行操作并返回一个布尔值,如果操作成功(true)或失败(false)。InsertAfter方法将插入一个字符串,但始终在同一位置,Delete方法将始终删除同一节点

private class StrNode {

        String data;
        StrNode next;
    }

    private StrNode head;   // the head of the singly-linked list.

    public StringList() {
        head = null;
    }

public void prepend(String s) {                                                         
        var newNode = new StrNode();
        // TODO: Adds an item to the start of the list.     
        newNode.data = s;
        if(head == null) {
            head = newNode;
        }
        else {
        newNode.next = head;
        head = newNode;
        }
        
    }

/**
     * Inserts an item after the first instance of a key if the key exists.
     *
     * @param s the item to insert
     * @param key the item in the list to insert after
     * @return whether the insertion was successful
     */
    public boolean insertAfter(String s, String key) {                                                      
        // TODO:    Inserts an item after the first instance of a key if the key exists.
        var newNode = new StrNode();
        StrNode current = head;
        newNode.data = s;
        
        if(head == null) {
            head = newNode;
        }
        else if(current == newNode.next){
            current.next = newNode;
            current = newNode;

        }
        else {
            newNode.next = current.next;
            current.next = newNode;
        }
        
        
        return false;
    }
    
    /**
     * Deletes the first instance of an item from the list.
     *
     * @param key the value of the item to delete from the list.
     * @return whether the deletion was successful.
     */
    public boolean delete(String key) {                                                     
        // TODO:    Deletes the first instance of an item from the list.
        StrNode current = head;
        StrNode sucNode = current;
        
        if(current == null) {
            sucNode = head.next;
            head = sucNode;
            return true;
        }
        else if(current.next != null) {
            sucNode = current.next.next;
            current.next = sucNode;
            return true;
        }

        return false;
    }
我想用的主要方法是在三之后插入四,应该是:三,二,四,一。 但我得到:三,四,二,一

delete方法只是删除四个,实际上应该是:三,四,二 但我得到:三,二,一 主要内容:


您询问了两种方法:

insertAfter
一些问题:

  • 您不使用参数
  • 如果
    head
    为null,则永远不能满足以下条件:节点应插入到具有密钥的节点之后,因此在这种情况下,应返回false
  • current
    被初始化为
    head
    ,因此您正在将新节点分配给
    head。下一步
    不检查
    是否匹配
  • 您总是返回
    false
    ,从不返回
    true
您应该通过迭代列表来查找给定的键:

public boolean insertAfter(String s, String key) {
    // Inserts an item after the first instance of a key if the key exists.
    StrNode current = head;
    
    while (current != null) {
        if (current.data == key) { // found the insertion spot
            var newNode = new StrNode();
            newNode.data = s;
            newNode.next = current.next;
            current.next = newNode;
            return true;
        }
        current = current.next; // need to walk along the list
    }
    return false; // didn't find the key
}
删除
  • 您不使用参数
  • 如果
    head
    (is
    current
    )为null,则永远不能满足要删除的节点应具有给定密钥的条件,因此在这种情况下,应返回false
  • 在另一种情况下,您总是在不检查
    键的情况下删除第二个节点
更正:

public boolean delete(String key) {                                                     
    // Deletes the first instance of an item from the list.
    StrNode current = head;
    if (head == null) return false;
    if (head.data == key) {
         head = head.next;
         return true;
    }

    while (current.next != null) {
        if (current.next.data == key) { // found it
            current.next = current.next.next; // delete it
            return true;
        }
        current = current.next; // need to walk along the list
    }
    return false; // not found
}
关于
前置的备注
: 您不需要
if
语句。
else
块中的代码在
head
为空时工作正常,因此您的代码可以是:

public void prepend(String s) { 
    // Adds an item to the start of the list. 
    StrNode node = new StrNode();
    node.data = s;
    node.next = head;
    head = node;
}

我想我昨天看到了这个问题。你又发了吗?我清楚地记得prepend()方法,它给我的印象是一个奇怪的方法名。我会使用addFirst()。无论如何,你应该包括prepend()方法的代码,因为这就是你设置的方式。是的,我的错,我想我会让事情变得更清楚,只是为了看看我正在尝试的方法谢谢!
public void prepend(String s) { 
    // Adds an item to the start of the list. 
    StrNode node = new StrNode();
    node.data = s;
    node.next = head;
    head = node;
}