[英]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.