なぜ私のadd()
とprint()
の方法がうまくいかないのか理解しようとしていました。私は事実上すべてを試しましたが、私はこれを行うことができません。私のコードが間違っていることは分かっています(新しいものを試すために私のコードが削除されたので、私のコードが正しいかどうかは分かりません)。最初にノードを追加しようとすると、リストが空に見えるのはなぜですか?
読んでいただきありがとうございます。
NodeFNクラス:あなたはリンクリストを作成している
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();
}
}
出力コードとは何ですか? – dahui
出力は何もありません。コンソールに何も表示されません。 – g24
@ g24最初に 'add()'に渡されたすべてのノードを追加しますか? – progyammer