Java 如何在链表中添加数据值?

Java 如何在链表中添加数据值?,java,linked-list,Java,Linked List,我正在使用Java中的链表。我试图获取存储在节点(AB)中的值,并将其添加到存储在另一个节点(BC)中的值中。到目前为止,我已成功地将这些值存储在链表中。现在我想检索int数据并将其分配给一个变量,然后将这些变量添加到一起。 例如ABC=AB+BC 列车路线表代码: public class TrainRouteList { Node head; Node tail; public void add(Node node){ if (tail == null){ h

我正在使用Java中的链表。我试图获取存储在节点(AB)中的值,并将其添加到存储在另一个节点(BC)中的值中。到目前为止,我已成功地将这些值存储在链表中。现在我想检索int数据并将其分配给一个变量,然后将这些变量添加到一起。 例如ABC=AB+BC

列车路线表代码:

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

这取决于
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();
   }
}
同样,这取决于链接列表的设置方式:

根->根的子->根的子->等

还请注意,如果您有大量数据,则可以通过使用树状数据结构来提高效率

奥塞斯

根据您对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;
    }
}
您应该直接在Node类中建立节点之间的关系

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

这样,从长远来看,您可以使事情更具凝聚力,更易于处理

我回顾了代码,从评论中看到了我的错误,感谢大家的参与。第一个是在my.add()中,我向节点添加了一个数组,而不是int值。第二个是我在节点中存储了一个字符串和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);


}}

列车路线列表
的代码是什么?什么是
节点
列车路线列表
?你试了什么?。。。为什么
newstring(“aString”)
?TranRouteList的代码:公共类TrainRouteList{Node head;Node tail;public void add(Node Node){if(tail==null){head=Node;tail=Node;}tail.next=Node;tail=Node;}}new int[5]为您提供一个包含5个int项的数组。在新版本发布后,这5个条目的值完全未定义,它们可以是任何内容。