2016-09-30 17 views
1

私はJavaのLinked Listで作業しています。私はノード(AB)に保存しておいた値を別のノード(BC)に保存した値に追加しようとしています。これまでリンクリストに値を正常に格納しました。今私はデータを取得し、それらを変数に割り当てて、それらの変数を一緒に追加したいと思います。 たとえばABC = AB + BC。 TrainRouteListためリンクされたリストにデータ値を追加する方法は?

コード:テストクラスの

public class TrainRouteList { 


Node head; 
Node tail; 

public void add(Node node){ 

    if (tail == null){ 
     head = node; 
     tail = node; 
    } 

    tail.next = node; 
    tail = node; 
}} 

コード:

public class LinkedListTest { 
@Test 
public void test(){ 
    TrainRouteList list = new TrainRouteList(); 

    list.add(new Node(new int[5], new String("AB")));//AB5 
    list.add(new Node(new int[4], new String("BC")));//BC4 
    list.add(new Node(new int[8], new String("CD")));//CD8 
    list.add(new Node(new int[8], new String("DC")));//DC8 
    list.add(new Node(new int[6], new String("DE")));//DE6 
    list.add(new Node(new int[5], new String("AD")));//AD5 
    list.add(new Node(new int[2], new String("CE")));//CE2 
    list.add(new Node(new int[3], new String("EB")));//EB3 
    list.add(new Node(new int[7], new String("AE")));//AE7 
} } 
+0

「TrainRouteList」のコードは何ですか? –

+2

'Node'と' TrainRouteList'とは何ですか?何を試しましたか? –

+0

...なぜ新しい文字列( "aString") '? –

答えて

1

これは、あなたが構造化あなたのTrainRouteListオブジェクトを持っているかに依存します。通常、あなたがリストのルートへのポインタを持っているリンクリストのために:その後、このルートを使用して

Node currNode = TrainRouteList.getRoot(); 

、あなたがリンクリストトラバースすることができます

再び
int globalInt = 0; 
while(currNode != null) 
{ 
    if(currNode.getStr().equalsIgnoreCase("ab")) 
    { 
    globalInt += currNode.getVal(); 
    } 
    else if(currNode.getStr().equalsIgnoreCase("bc")) 
    { 
    globalInt += currNode.getVal(); 
    } 
    else 
    { 
    currNode = currNode.getChild(); 
    } 
} 

を、これはどのように依存していますあなたのリンクリストを設定している:

ルート - ルートの>子 - >ルートの子の子供 - >など

はまた、あなたが大量のデータを持っている場合、あなたは効率を向上させることができることに注意してください木のようなデータ構造を使用しています。

Ossss

あなたはこのようにそれを修正するためTrainRouteListクラスの実装に基づいて、それが容易になることがあります。

public class TrainRouteList{ 

    private Node root; 

    public TrainRouteList(Node root) 
    { 
     this.root = root; 
    } 

    public Node getRoot(){ 
     return this.root; 
    } 

    public void setRoot(Node r) 
    { 
     this.root = r; 
    } 
} 

あなたが直接ノード間の関係を確立する必要がありますノードクラス。

public class Node{ 

    private int val = 0; 
    private String str; 
    private Node child = null; 

    public Node(int val, String strVal) 
    { 
     this.val = val; 
     this.str = strVal; 
    } 

    //Getter + setter for object properties 
    public void setVal(int val)  {this.val = val}; 
    public int getVal()    {return this.val}; 
    public void setStr(String str) {this.str = str;} 
    public String getStrt()   {return this.str;} 

    //Getter + Setter for child node 
    public void setChild(Node c) {this.child = c;} 
    public Node getChild()   {return this.child} 
} 

これにより、長期的にはより一貫性があり作業が容易になりました。

0

私はコードを取りに行きました。コメントの手がかりを借りて私の間違いを見ました。最初は私の.add()にありました。私はノードにint値ではない配列を追加していました。 2番目は、intデータを持つノードに文字列を格納していたことです。私は各ノードの位置を知っているので、int型の文字列を格納する必要はありません(文字列とintを同じノードに格納できるかどうかはわかりません)。ノードに文字列を格納する必要はありませんでした。

public class LinkedListTest { 



@Test 
public void test(){ 
    TrainRouteList list = new TrainRouteList(); 

    list.add(new Node(5));//AB5 
    list.add(new Node(4));//BC4 
    list.add(new Node(8));//CD8 
    list.add(new Node(8));//DC8 
    list.add(new Node(6));//DE6 
    list.add(new Node(5));//AD5 
    list.add(new Node(2));//CE2 
    list.add(new Node(3));//EB3 
    list.add(new Node(7));//AE7 

    // 
    int AB = list.head.data; 
    int BC = list.head.next.data; 
    int ABC = AB + BC; 

    System.out.println("The Distance of A-B-C is " + ABC); 


}} 
関連する問題