简体   繁体   中英

Adding new Nodes add the beginning of LinkedList and at the end of LinkedList

public class 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.

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?

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

Then inside the body of the method you are just resetting the reference to a new memory location. :

head -> New node memory location.

Please note : list.head is a non primitive object and in java non primitive objects are passed by reference.

Because Node head is a local variable of addFront() that points to list.head . When you do head=newHead you are simply making your local variable point to the new Node you just created. The list in main() is not affected at all.

With AddEnd , you don't need to modify the first element of the list, so you don't need to send the list. 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.

I work with C#, not Java, but my guess is this should work:

    ...
    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 . If you happen do to list = something , again, it won't affect your main list but just your local variable.

BTW, try to use appropriate variable names so the code is not confusing. In addEnd your variable shouldn't be named newHead .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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