简体   繁体   English

添加到链接列表的末尾

[英]Adding to the tail of a linked list

I have an append method for my linked list where I want to add to the tail, however when a new node is added to the list, headNode and tailNode both become the newly inserted node. 我对链接列表有一个append方法,要在其中添加到尾部,但是当将新节点添加到列表中时,headNode和tailNode都成为新插入的节点。 How do I keep headNode to stay as the first node that was entered into the list and not have it become the same thing as tailNode. 如何使headNode保持为输入到列表中的第一个节点,而不让它成为tailNode。

 public static void append()
{
 for(int x = 0; x < gradeArray.length; x++)
 {
   if(gradeArray[x] < 70)
   {
     StudentNode newNode = new StudentNode(nameArray[x], gradeArray[x], null);
     if(headNode == null)
     {
       headNode = newNode;
     }
     else
     {
       tailNode.nextNode = newNode;
     }
     tailNode = newNode;
}
 }
 }

I am not sure what mistake are you doing but see below this code is working perfectly fine. 我不确定您在犯什么错误,但是请看下面的代码是否工作正常。

    public class Grades {

    public static String[] nameArray = new String[50];
    public static int[] gradeArray = new int[50];

    public static StudentNode headNode;
    public static StudentNode tailNode;

    public static void append() {
        for (int x = 0; x < gradeArray.length; x++) {
            if (gradeArray[x] < 70) {

                String name = nameArray[x];
                int grade = gradeArray[x];
                StudentNode newNode = new StudentNode(name, grade, null);
                if (headNode == null) {
                    headNode = newNode;
                } else {
                    tailNode.nextNode = newNode;
                }
                tailNode = newNode;
            }
        }
    }

    public static void main(String[] args) throws java.lang.Exception {
        for (int i = 0; i < 50; i++) {
            nameArray[i] = "name-" + i;
            gradeArray[i] = i;
        }

        append();

        for(int i=0; i<50; i++) {
            nameArray[i] = "name-" + (i + 50);
            gradeArray[i] = i + 50;
        }

        append();

        System.out.println(headNode.toString());
        System.out.println(tailNode.toString());
    }
 }

 class StudentNode {

    public int grade;
    public String name;
    public StudentNode nextNode;

    public StudentNode(String n, int g, StudentNode sn) {
        name = n;
        grade = g;
        nextNode = sn;
    }

    public String toString() {
        return name + ", " + grade;
    }
}

Even if you change grade and name arrays and run append again it still keeps the head correct. 即使您更改成绩和名称数组并再次运行append,它仍然可以保持正确的状态。

Ideone link for running code Ideone链接,用于运行代码

Why did you append this statement ? 您为什么附加此声明? tailNode = newNode;

Imagine, thread goes through by if(headNode == null) he assign newNode adress to headNode . 想象一下,线程通过if(headNode == null)将newNode地址分配给headNode。 After that, tailNode = newNode; 之后, tailNode = newNode; is executed and tail pointing to newNode. 被执行并且尾巴指向newNode。 Finally, tailNode and headNode point to the same object : newNode. 最后,tailNode和headNode指向同一个对象:newNode。

I think you have to delete this statement tailNode = newNode; 我认为您必须删除此语句tailNode = newNode;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM