2016-11-04 9 views
1

なぜ私の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(); 
} 
} 
+1

出力コードとは何ですか? – dahui

+0

出力は何もありません。コンソールに何も表示されません。 – g24

+0

@ g24最初に 'add()'に渡されたすべてのノードを追加しますか? – progyammer

答えて

0

私はあなたの追加メソッドについて話すことはできませんが、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; 
    } 
} 
+0

うわー....私はそれが間違っていると思ったので、私はあなたが今朝それを削除したとき、昨夜私がこの同じ 'add()'メソッドコードを持っていなかったことをあなたに教えています。もちろん、私の 'print()'メソッドは途切れていたので、まずコンソールで何かを見る方法がありませんでした。ありがとう、cricket_007。 – g24

+0

ようこそ。通常は、void 'print'メソッドの代わりに' toString'メソッドを作成しますが、何をしたいのですか? –

+0

私はそれを検討するつもりですが、なぜあなたはそれを言っているのか分かります。もう一度、ありがとう。 – g24

0

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

キュークラス。これを印刷するには、ヌルに達するまでリストをループさせてgetNext()にする必要があります。基本的に、あなたはこのようなもので終わる必要があります:あなたのadd()方法として

public void print() { 
    NodeFN current = head; 

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

を、アイデアは、基本的にはあなたのリスト内の最後のノードのnext基準として、任意の新しいノードを置くことです。 Queueクラスのlastノードへの参照を保持するだけです。追加するときは、lastノードのnextを新しく作成したノードに設定し、新しいノードをlastに設定します。

0

アドバイスの方法については、適切な考えを持っていました。まだリストに要素がない場合は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();   
    } 
} 
関連する問題