簡體   English   中英

鏈表Delete和insertAfter方法

[英]Linked List Delete and insertAfter Method

我在使用 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);
        
        
    }

你問了兩種方法:

insertAfter

一些問題:

  • 您不使用參數key
  • 如果head為 null,則永遠無法滿足應將節點插入到具有鍵的節點之后的條件,因此在這種情況下,您應該返回 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 (is current ) 為空,則永遠無法滿足要刪除的節點應具有給定鍵的條件,因此在這種情況下應返回 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM