简体   繁体   English

为什么在尝试在开头添加节点时列表显示为空?

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

I've literally lost my mind trying to figure out why my add() and print() methods won't work. 我真的失去了理智,想弄清楚为什么我的add()print()方法不起作用。 I've tried virtually everything but I just can't do this. 我几乎尝试了所有东西,但我不能这样做。 I know my code is dead wrong (I can't even tell if my code was right at one point or another because I deleted it to try new things) so what could be wrong with it? 我知道我的代码是错误的(我甚至无法判断我的代码是否正确,因为我删除它以尝试新的东西)所以它可能有什么问题?

I appreciate you taking the time to read. 感谢您抽出宝贵时间阅读。

NodeFN class: 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;}
} 

Queue class: 队列类:

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

You're creating a linked list. 您正在创建链接列表。 In order to print that, you need to loop through the list until you reach a null in getNext() . 为了打印它,您需要循环遍历列表,直到在getNext()达到null。 Essentially, you should end up with something like this: 基本上,你应该得到这样的东西:

public void print() {
   NodeFN current = head;

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

As for your add() method, the idea is to basically put any new nodes as the next reference of the last node in your list. 至于你的add()方法,我们的想法是基本上把任何新节点作为列表中最后一个节点的next引用。 Simply keep a reference to the last node in your Queue class. 只需保留对Queue类中last节点的引用。 When adding, set next of the last node to your newly created node, and set the new node as the last . 添加时,将last节点的next设置为新创建的节点,并将新节点设置为last节点。

I can't speak for your add method, but what is n here ? 我不能为你的add方法讲,但什么是n在这里?

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

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

The queue class should not care about public String n at all. 队列类根本不应该关心public String n You only need the head Node. 您只需要head节点。

Then, nn.getNext().getData(); 然后, nn.getNext().getData(); is returning something, yes? 回来了,是吗? But, you are not printing it nor are you "moving forward" in the list. 但是,你不打印它,也不是你在列表中“前进”。 (You don't assign nn to the next node). (您不将nn分配给下一个节点)。

Try something like this 尝试这样的事情

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

    NodeFN tmp = head;

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

If you want the nodes to be add at the start of the list, then this should work. 如果您希望在列表的开头添加节点,那么这应该可行。

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

For the add method, you had the right idea to start. 对于add方法,您有正确的想法开始。 Make nn the head if there are no element in the list yet. 如果列表中没有元素,请将头部设为nn Otherwise, traverse the list to the last element and add it to the end (since it's a queue). 否则,遍历列表到最后一个元素并将其添加到结尾(因为它是一个队列)。

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

If you want to add the new node to the beginning (for whatever strange reason), it would be even easier. 如果你想将新节点添加到开头(出于任何奇怪的原因),它会更容易。

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

For the print method. 对于打印方法。 You're not setting nn (the cursor) to reference any node in the actual queue. 您没有设置nn (光标)来引用实际队列中的任何节点。 You should be setting nn to the head of the queue and then iterating through the queue. 您应该将nn设置为队列的头部,然后迭代队列。 NodeFN nn = this.head . NodeFN nn = this.head And then in the while loop's body, print the data nn.getData() , then move to the next node nn = nn.getNext() . 然后在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.

相关问题 为什么'replaceAll'方法不在字符串的开头添加空格? - Why does the 'replaceAll' method not add an empty space at the beginning of the String? 尝试将节点添加到链接列表 - Trying to add nodes to linked list 为什么我的JFileChooser出现但为空? - Why does my JFileChooser appear, but is empty? 为什么列表中的最后一个元素显示为 null - why does the last element in list appear as null 为什么在尝试使用参数化的泛型获取查询时出现空列表 - Why I am getting the empty list while trying to fetch the query with the paramaterized generic 清空清单 <String> 在for循环的开始 - Empty the List<String> at the beginning of for loop 为什么 DataOutputStream.writeUTF() 在开头添加额外的 2 个字节? - Why does DataOutputStream.writeUTF() add additional 2 bytes at the beginning? 如何在两个虚拟节点之间的双向链表的开头添加节点? - How can I add a node at the beginning of a doubly linked list in-between two dummy nodes? 为什么会出现空白画布而不是我的图像? - Why does an empty canvas appear instead of my image? 为什么List.Add(E)返回布尔值而List.Add(int,E)返回void? - Why does List.add(E) return boolean while List.Add(int, E) returns void?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM