【问题标题】:Linked List Delete and insertAfter Method链表删除和insertAfter方法
【发布时间】:2020-10-26 19:10:09
【问题描述】:

我在使用 delete 和 insertAfter 方法时遇到问题,这些方法是执行操作并在操作成功(真)或不成功(假)时返回布尔值。 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 方法只是删除实际上应该看起来像这样的四个:三、四、二 但我得到:三,二,一 主要:

public static void main(String[] args) {
        
        StringList s = new StringList();
        
        s.prepend("one");
        s.prepend("two");
        s.prepend("three");
        System.out.println(s);
        
        s.insertAfter("four", "three");
        System.out.println(s);
        
        System.out.println(s.delete("one"));
        System.out.println(s);
        
        
    }

【问题讨论】:

  • 我以为我昨天看到了这个问题。你又发帖了?我清楚地记得 prepend() 方法,我觉得它是一个奇怪的方法名称。我会使用 addFirst()。无论如何,您应该包含 prepend() 方法的代码,因为这是您进行设置的方式。
  • 是的,我的错,我想我会让事情更清楚,只是为了看看我正在尝试的方法,谢谢!

标签: java methods linked-list


【解决方案1】:

您询问了两种方法:

insertAfter

一些问题:

  • 您不使用参数key
  • 如果head 为空,则永远无法满足将节点插入具有键的节点的条件,因此在这种情况下,您应该返回false。
  • current 被初始化为 head,因此您将新节点分配给 head.next 而不检查 key 匹配...
  • 你总是返回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
}

delete

  • 您没有使用参数key
  • 如果head(即current)为null,则永远无法满足要删除的节点应具有给定键的条件,因此在这种情况下应返回false。
  • 在另一种情况下,您总是删除第二个节点而不检查 key 匹配...

更正:

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
}

关于prepend的备注:

您不需要if 语句。当head 为空时,else 块中的代码可以正常工作,因此您的代码可以是:

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多