簡體   English   中英

為什么在嘗試在開頭添加節點時列表顯示為空?

[英]Why does the list appear empty while trying to add nodes in the beginning?

我真的失去了理智,想弄清楚為什么我的add()print()方法不起作用。 我幾乎嘗試了所有東西,但我不能這樣做。 我知道我的代碼是錯誤的(我甚至無法判斷我的代碼是否正確,因為我刪除它以嘗試新的東西)所以它可能有什么問題?

感謝您抽出寶貴時間閱讀。

NodeFN類:

public class NodeFN {
     private String data; // Data for node.
     private NodeFN next; // Next node.

public NodeFN(String data) {
    this.data = data; // Take the data value passed in & store it in the data field.
    this.next = null; // Take the next node & store it in the next field.
}

   // Mutator functions.
public String getData() {return data;}
public NodeFN getNext() {return next;}
public void setData(String d) {data = d;}
public void setNext(NodeFN n) {next = n;}
} 

隊列類:

public class Queue {
   NodeFN head; // Head of node.
   public String n;

public Queue(String n) { 
    head = new NodeFN(n); // head is now an object of NodeFN which holds a string.
}

public void add(String n) {
    NodeFN nn = new NodeFN(n); // nn is now an object of NodeFN which holds a string, it should return something.
        if(head == null) {
            head = nn;
        }
        while(nn.getData().compareTo(head.getData()) < 0) {
                nn.setNext(head); // Put node in beginning of the list.
                nn.setData(n);      
        }
    }

public void print() {
    NodeFN nn = new NodeFN(n);

    while(nn != null) {
        nn.getNext().getData();
        System.out.println(nn.getData() + " ");
    }
}

public static void main(String[] args) {
    Queue q = new Queue("string to test");
    q.add("another string to test if add method works.");
    q.print();
}
}

您正在創建鏈接列表。 為了打印它,您需要循環遍歷列表,直到在getNext()達到null。 基本上,你應該得到這樣的東西:

public void print() {
   NodeFN current = head;

   while(current != null) {
       System.out.println(current.getData());
       current = current.getNext();
   }
}

至於你的add()方法,我們的想法是基本上把任何新節點作為列表中最后一個節點的next引用。 只需保留對Queue類中last節點的引用。 添加時,將last節點的next設置為新創建的節點,並將新節點設置為last節點。

我不能為你的add方法講,但什么是n在這里?

public void print() {
    NodeFN nn = new NodeFN(n);

    while(nn != null) {
        nn.getNext().getData();
        System.out.println(nn.getData() + " ");
    }
}

隊列類根本不應該關心public String n 您只需要head節點。

然后, nn.getNext().getData(); 回來了,是嗎? 但是,你不打印它,也不是你在列表中“前進”。 (您不將nn分配給下一個節點)。

嘗試這樣的事情

public void print() {
    if (head == null) System.out.println("()");

    NodeFN tmp = head;

    while(tmp != null) {
        System.out.println(tmp.getData() + " ");
        tmp = tmp.getNext();
    }
}

如果您希望在列表的開頭添加節點,那么這應該可行。

public void add(String n) {
    NodeFN nn = new NodeFN(n);
    if(head == null) {
        head = nn;
    }

    // Don't use a while loop, there is nothing to repeat
    if (n.compareTo(head.getData()) < 0) {
        // Both these operations put 'nn' in beginning of the list.
        nn.setNext(head); 
        head = nn;
    }
}

對於add方法,您有正確的想法開始。 如果列表中沒有元素,請將頭部設為nn 否則,遍歷列表到最后一個元素並將其添加到結尾(因為它是一個隊列)。

public void add(String n) {
    NodeFN nn = new NodeFN(n);
    if(head == null) {
        head = nn;
    }
    else {
        NodeFN cursor = this.head;
        while(cursor.getNext() != null) {
            cursor = cursor.getNext();
        }
        cursor.setNext(nn);
    }
}

如果你想將新節點添加到開頭(出於任何奇怪的原因),它會更容易。

public void add(String n) {
    NodeFN nn = new NodeFN(n);
    nn.setNext(this.head);
    this.head = nn;
}

對於打印方法。 您沒有設置nn (光標)來引用實際隊列中的任何節點。 您應該將nn設置為隊列的頭部,然后迭代隊列。 NodeFN nn = this.head 然后在while循環的主體中,打印數據nn.getData() ,然后移動到下一個節點nn = nn.getNext()

public void print() {
    NodeFN cursor= this.head;

    while(cursor != null) {
        System.out.println(cursor.getData() + " ");
        cursor= cursor.getNext();           
    }
}

暫無
暫無

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

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