簡體   English   中英

Java鏈接列表 - 添加方法

[英]Java Linked List - add method

數據結構類,實現具有頭,尾和當前節點的單鏈表。 如果遇到方法問題,可以在正確的方向上使用微調。

從賦值,編寫方法:

add(item):在列表中的當前節點之后添加item(String),並設置當前指針以引用新節點。

我的嘗試:

public void add(String item)
{
    if(curr != null)
    {
        Node newNode = new Node(item, curr.next);
        curr.next = newNode;
        curr = newNode;
    }
    else
    {
        head = tail = new Node(item, null);
        curr = head;
    }
}

我的添加方法似乎只在我將項目添加到列表中間時才起作用,而不是在任何一端。 如果我使用它來添加一些項目然后打印列表,只有我添加的第一個項目將在列表中,而我的prepend和append方法測試得很好。

我的代碼有什么明顯的問題嗎? 我覺得我錯過了一些明顯的東西。

所有:

public class LinkedList {
    Node head = null; /* Head of the list */
    Node tail = null; /* Tail of the list */
    Node curr = null; /* Current node in the list */

    public void prepend(String item) {
        if (head == null) {
            head = tail = new Node(item, null);
            curr = head;
        } else {
            head = new Node(item, head);
            curr = head;
        }
    }

    public void append(String item) {
        if (head == null) {
            head = tail = new Node(item, null);
            curr = tail;
        } else {
            tail.next = new Node(item, null);
            tail = tail.next;
            curr = tail;
        }
    }

    public void add(String item) {
        if (curr != null) {
            Node newNode = new Node(item, curr.next);
            curr.next = newNode;
            curr = newNode;
        } else {
            head = tail = new Node(item, null);
            curr = head;
        }
    }

    public void delete() {
        if (curr.next == null) {
            Node temp = head;
            while (temp.next != curr) {
                System.out.println(temp.item);
                temp = temp.next;
            }
            temp.next = null;
            curr = head;
        }
    }

    public void find(String item) {
        Node temp = new Node(curr.item, curr.next);
        if (item.equals(temp.item))
            curr = temp;
        else {
            temp = temp.next;
            while (temp.next != null && temp != curr) {
                if (item.equals(temp.item))
                    curr = temp;
            }
        }
    }

    public String get() {
        if (curr != null)
            return curr.item;
        else
            return "";
    }

    public boolean next() {
        if (curr != tail) {
            curr = curr.next;
            return true;
        } else
            return false;
    }

    public void start() {
        curr = head;
    }

    public void end() {
        curr = tail;
    }

    public boolean empty() {
        if (head == null)
            return true;
        else
            return false;
    }
}

Node類:

class Node {
    Node next;
    String item;

    Node(String item, Node next) {
        this.next = next;
        this.item = item;
    }
}

有確實是一個問題add :它不更新tail當節點已經存在。 考慮這一系列動作:

LinkedList list = new LinkedList(); 
list.add("one");
list.add("two");
list.append("three");

如果你要使用這個打印它:

public void print() {
    Node curr = this.head;
    while(curr != null) {
        System.out.println(curr.item);
        curr = curr.next;
    }
}

像這樣:

list.print();

你會得到以下輸出:

one
three

發生這種情況是因為在執行第二個add操作后, tailappend依賴)繼續指向第一個Node

我在這里沒有看到任何問題,所以我猜這個問題在其他地方。

好的,我看到的唯一問題是刪除:

public void delete()
{
    Node temp = head;

    while(temp != null && temp.next != curr) {
         System.out.println(temp.item);
         temp=temp.next;

    }

    if (temp != null && temp.next != null) {
        temp.next = temp.next.next;
    }
    curr = head;

}

我想我找到了你的問題。 如果你使用append(),你可以在尾部之后直接添加它。 但是當您在尾部之后添加了先前的節點時,您不會將尾部設置為新節點。 這意味着一旦調用append()兩次,就會松開在第一個append()之后添加的所有節點。

簡要示例:

public static void main(String[] args) {
    LinkedList list = new LinkedList();
    list.add("First add");
    list.append("First Append");
    list.add("Second add");
    list.prepend("First prepend");
    list.add("Third add");
    list.prepend("Second prepend");
    list.add("fourth add");
    list.append("Second Append");
    list.add("Fifth add");
    list.add("Sixth add");

    list.start();
    do {
        System.out.println(list.get().toString());

    } while (list.next());
}

輸出:

Second prepend
fourth add
First prepend
Third add
First add
First Append
Second Append

結論:“第二次添加”丟失,以及“第五次添加”和“第六次添加”,因為你的next()方法一到達尾部就會停止。 如果最后添加新節點,則需要始終更新尾部。

希望這可以幫助。 干杯,Chnoch

我認為問題是

if (curr != null) {
    Node newNode = new Node(item, curr.next); //<-- here (curr.next)

//and

Node(String item, Node next) {
    this.next = next; //<-- here

嘗試( 編輯 ):

Node newNode = new Node(item, curr); // pass curr to the constructor of Node
curr = newNode;

暫無
暫無

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

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