2016-11-07 4 views
1

リンクリストを作成しようとしていますが、各ノードはノードの位置である1つの整数とノード名である文字列を持ちます。Javaのカスタムリンクリストのノードに複数の異なるデータ型

public class Node { 

    public int position; 
    public Node next; 
    public String shipname; 

    public Node(int num, String shipname) { 
     position = num; 
     shipname = shipname; 
    } 

    public void insert(int x, String shipname) { 
     Node newNode = new Node(num, shipname); 
     Node previous = null; 
     Node current = first; 

     while (current != null && num < current.position) { 
      previous = current; 
      current = current.next; 
     } 

     if (previous == null) { 
      newNode.next = first; 
      first = newNode; 
     } else { 
      previous.next = newNode; 
      newNode.next = current; 
     } 
    } 

    public void display() { 
     Node current = first; 

     while (current != null) { 
      System.out.println(current.shipname + " " + current.position); 
      current = current.next; 
     } 

     System.out.println(" "); 
    } 

} 

このコードは正しく位置データを与えない理由があるが、その代わりに、画面への書き込みshipnamesの「ヌル」?ここには簡単な出力があります:

Largest priority to Smallest 
null 7 
null 4 
null 3 
+1

代入のlhsのコンストラクタで 'this.position'と' this.shipname'を実行してみてください。 – 0x499602D2

+0

'first'はどこに定義されていますか? –

+0

insert関数内には何numですか? – 0x499602D2

答えて

0

あなたのコードには多少の誤差があります。たとえば、Nodeはノードであり、リストではありません。リストは複数のノードで構成されています。だから、外側のクラスがリストである必要があり、それはノードが含まれている必要があります...ここで

は、単一リンクのリストについては、私のコードです:私は宣言するとリストクラス内のノードを使用しています

import java.util.*; 
import java.lang.*; 
import java.io.*; 

class Template 
{ 
    public static Scanner in = new Scanner(System.in); 

    public static void main (String[] args) throws java.lang.Exception 
    { 
     SingleLinkedList<Integer> list = new SingleLinkedList<Integer>(); 
     list.add(in.nextInt()); 
     list.add(in.nextInt()); 
     list.add(in.nextInt()); 
     list.add(in.nextInt()); 
     list.add(in.nextInt()); 
     list.print(); 

     list.reverse(); 
     list.print(); 
    } 
} 

class SingleLinkedList<Item> 
{ 
    private class Node 
    { 
     private Item data; 
     private Node next; 

     public Node(Item data) 
     { 
      this.data = data; 
      this.next = null; 
     } 

     public void setData(Item data) 
     { 
      this.data = data; 
     } 

     public Item getData() 
     { 
      return this.data; 
     } 

     public Node getNext() 
     { 
      return this.next; 
     } 

     public void setNext(Node next) 
     { 
      this.next = next; 
     } 
    } 

    private int size; 
    private Node head; 

    public SingleLinkedList() 
    { 
     size = 0; 
     head = new Node(null); 
    } 

    //Add at end 
    public void add(Item data) 
    { 
     Node node = new Node(data); 
     Node current = head; 

     while(current.getNext() != null) 
     { 
      current = current.getNext(); 
     } 

     current.setNext(node); 
     size++; 
    } 

    //Add at a specific index (Node after head = index 1) 
    public void add(Item data, int index) 
    { 
     int i=0; 
     Node current = head; 
     Node node = new Node(data); 
     while(i<index-1 && current.getNext() != null) 
     { 
      current = current.getNext(); 
      i++; 
     } 

     if(index - i == 1) 
     { 
      node.setNext(current.getNext()); 
      current.setNext(node); 
      size++; 
     } 
     else 
     { 
      System.out.println("Invalid index"); 
      return; 
     } 
    } 

    //Print linked list 
    public void print() 
    { 
     Node current = head.getNext(); 

     String output = "[head] -> "; 
     while(current != null) 
     { 
      output += "["+current.getData().toString()+"] -> "; 
      current = current.getNext(); 
     } 
     output+="[null]"; 
     System.out.println(output); 
    } 

    //Remove node at an index (1 based) 
    public void remove(int index) 
    { 
     int i=0; 
     Node current = head; 

     if(index < 1 || index > size) 
     { 
      System.out.println("Invalid Index"); 
      return; 
     } 

     while(i<index-1 && current.getNext() != null) 
     { 
      current = current.getNext(); 
      i++; 
     } 

     current.setNext(current.getNext().getNext()); 
     size--; 
    } 

    //Reverse the linked list 
    public void reverse() 
    { 
     Node current = head.getNext(); 
     Node prevNode = null; 
     Node nextNode; 
     if(size < 2) 
     { 
      return; 
     } 

     while(current != null) 
     { 
      nextNode = current.getNext(); 
      current.setNext(prevNode); 
      prevNode = current; 
      current = nextNode; 
     } 
     head.setNext(prevNode); 
    } 
} 

注意してください。

0

"this.shipname"と "this.position"でコンストラクタを編集すると、私の問題が解決しました。

public Node(int num, String shipname) { 
     this.position = num; 
     this.shipname = shipname; 
    } 
0

まず、コンストラクタ内で同じローカル変数を参照しています。

public Node(int num, String shipname) { 
    position = num; 
    shipname = shipname; 
} 

変更これは、

this.shipname = shipname; 

を割り当てるので、最終的なコードは次のようになります。

public Node(int num, String shipname) { 
    position = num; 
    this.shipname = shipname; 
} 

あなたは '最初の' 変数に宣言していない別の問題があるようです。 'insert'メソッドで変数 'x'を 'num'に変更します。

JavaはLinkedListクラスを提供しています。私はあなたのコードをJavaのLinkedListを使って更新しました。それが役に立てば幸い。

public class NodeLinkedList { 
    private class Node { 
     int position; 
     String shipName; 

     Node(int position, String shipName) { 
      this.position = position; 
      this.shipName = shipName; 
     } 
    } 

    private LinkedList<Node> nodeList; 

    NodeLinkedList() { 
     nodeList = new LinkedList<>(); 
    } 

    void insert(int position, String name) { 
     nodeList.add(new Node(position, name)); 
    } 

    void display(){ 
     for (Node node : nodeList) { 
      System.out.println(node.shipName + " " + node.position); 
     } 
    } 
} 
関連する問題