1

まず、ヘッド ノードを作成します。

Node head = new Node();

先頭ノードを次のノードにリンクします。node オブジェクトの Node 型のフィールドに代入します

ゼロはノードの番号を表します。このノードは 0 番です。

Node node = new Node(0,head);

public class Node {

  private Object data;
  private Node next;

  public Node()
  {
    data = null;
    next = null;
  }

  public Node(Object x)
  {
    data = x;
    next = null;
  }

  public Node(Object x, Node nextNode)
  {
    data = x;
    next = nextNode;
  }
}

これはノードをリンクする適切な方法ですか?

4

2 に答える 2

2

私が通常見ている方法は、LinkedList を使用することです。

public class Node {
    public Object data;
    public Node next = null;

    Node(data) {
        this.data = data;
    }
}

class LinkedList{
    public Node head = null;
    public Node end = null;

    void insert(Object data) {
        if(head == null) {
            head = new Node(data);
            end = head;
        } else {
            end.next = new Node(data);
            end = end.next;
        }
    }
}

これは次のように使用されます。

LinkedList= new LinkedList();
list.insert(2);
list.insert(3);
list.head;
于 2013-03-12T17:29:47.710 に答える
1

Javaでは、参照(つまり、ポインター)を介してすべてのオブジェクトを参照します。実際の値を処理するのは、プリミティブ型を使用する場合のみです。

そのnext = nextNodeためnext、を指すのと同じ場所をnextNode指すようになります。

TL; DR; はい。:)

于 2013-03-12T17:21:34.040 に答える