简体   繁体   English

添加新节点将在LinkedList的开头和LinkedList的末尾添加

[英]Adding new Nodes add the beginning of LinkedList and at the end of LinkedList

public class InsertithNode { 公共类InsertithNode {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    LinkedList list=new LinkedList();
    list.head=new Node(2);
    list.head.next=new Node(3);


    addFront(list.head,8);

    System.out.println(list);

    addEnd(list.head,11);

    System.out.println(list);
     addEnd(list.head,15);

    System.out.println(list);

  }
public static void addFront(Node head,int value)
{
   Node newHead=new Node(value);
   newHead.next=head;
   head=newHead;

}

 public static void addEnd(Node head,int value) {

     Node newHead=new Node(value);
     Node ref=head;
     Node last=ref;

     while(ref!=null) {
         last=ref;
         ref=ref.next;
     }
     last.next=newHead;


 }

} }

Hi, The code is above implementation of LinkedList add head and add last.However,When I run the code,I aam able to add new node as a last node on Linked List,but I can't add new node to the begging of the Linked List. 嗨,上面的代码是LinkedList的实现的添加头并最后添加。但是,当我运行代码时,我可以在Linked List上添加新节点作为最后一个节点,但是我无法将新节点添加到链接列表。

When I run this code the output is: 当我运行此代码时,输​​出为:

 head 2 --> 3 -->  null

 head 2 --> 3 --> 11 -->  null

 head 2 --> 3 --> 11 --> 15 -->  null

AddEnd method works but why doesn't AddFront work? AddEnd方法有效,但是为什么AddFront不起作用?

In the below call, you are actually passing the memory location of list's head node and not the actual head object: 在下面的调用中,您实际上是传递列表的头节点的存储位置,而不是实际的头对象:

addFront(list.head,8);

The memory location is then held by reference of the method signature ie, Node head. 然后通过引用方法签名即节点头来保存该存储位置。

head ->List's head node memory location head->列表的头节点内存位置

Then inside the body of the method you are just resetting the reference to a new memory location. 然后,在方法的内部,您只是将引用重置为新的存储位置。 :

head -> New node memory location. head->新节点的内存位置。

Please note : list.head is a non primitive object and in java non primitive objects are passed by reference. 请注意:list.head是非原始对象,在Java中,非原始对象通过引用传递。

Because Node head is a local variable of addFront() that points to list.head . 因为Node headaddFront()的局部变量,它指向list.head When you do head=newHead you are simply making your local variable point to the new Node you just created. 当您执行head=newHead您只是在将本地变量指向刚刚创建的新Node The list in main() is not affected at all. main()的列表完全不受影响。

With AddEnd , you don't need to modify the first element of the list, so you don't need to send the list. 使用AddEnd ,您无需修改​​列表的第一个元素,因此无需发送列表。 You don't modify the main list either, but just the last element, and that's enough for that method. 您也不需要修改主列表,而只需修改最后一个元素,这对于该方法就足够了。

You would need to send the list to the method, so it can access its head property. 您需要将列表发送到方法,以便它可以访问其head属性。

I work with C#, not Java, but my guess is this should work: 我使用C#,而不是Java,但是我猜这应该可以工作:

    ...
    addFront(list, 8);
    ...
}

public static void addFront(LinkedList list,int value)
{
   Node newHead=new Node(value);
   newHead.next=list.head;
   list.head=newHead;
}

Here list is also a local variable, but it points to the main list, so list.head is the same memory position and any modification affects the main list.head . 这里list也是一个局部变量,但是它指向主list,所以list.head是相同的内存位置,任何修改都会影响main list.head If you happen do to list = something , again, it won't affect your main list but just your local variable. 如果您碰巧再次执行list = something ,那么它不会影响您的主列表,而只会影响您的局部变量。

BTW, try to use appropriate variable names so the code is not confusing. 顺便说一句,请尝试使用适当的变量名,以使代码不会引起混淆。 In addEnd your variable shouldn't be named newHead . addEnd您的变量不应命名为newHead

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

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