BoomirajP
BoomirajP

Reputation: 327

Object is getting updated without that object reference?

//I have a Node.java Class

public class Node{

    int data;
    Node next;

    public Node(int d) {
        data = d;
    }
}

//And another java class

class LinkedList {

    Node head;

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        //Executing this loop
        for (int i = 0; i < 5; i++) {

            **list.add(i);**

        }
    }

     void add(int value){
        Node newNode = new Node(value);

        if(head == null )//Very first time its create the head object when i = 0
        {
            head = newNode;
        }else if(head.next == null){//This is for when i value is 1
            head.next  = newNode;
        }else{ //else part execute i >= 2
            //Created new node with head.next which mean value 1.And head is 0
            Node temp = head.next; 
            // Just need this object initialization for reference
            Node temp1 = newNode; 
             //Checking head.next is null or not if its null skip this loop execution
            while(temp != null)
            {
                temp1 = temp; 
                 temp = temp.next;
            }
            // Here we set newNode.next to null
            newNode.next = temp1.next; 
            temp1.next = newNode;
        }
    }
}

My Question is here , when temp1.next = newNode; line execute head object have added one next value.

** //For example if head = 0,head.next = 1 when temp1.next = newNode; line execute head.next.next = 2 is getting added with head. How its happening when we do not have head object reference.

Upvotes: 2

Views: 67

Answers (2)

screab
screab

Reputation: 179

You are not updating the head object. You are updating the head.next object.

So

head.next.next

can be written like this:

Node nextFromHead = head.next; // nextFromHead is 1
Node nextFromNextFromHead = nextFromHead.next; // nextFromNextFromHead is 2

head.next.next is the same object as nextFromNextFromHead but it ( the Node that is 2 ) doesn't have any direct connection to the head node.

I think this will help better understand how references work in java.

public class LinkedList {

    static Node head;

    public static void main(String[] args) {

        LinkedList list = new LinkedList();
        for(int i = 0; i < 5; i++)

            list.add(i);

        Node currentNode = head; // in java we don't need object initialization for reference. Node temp1; would work just fine

        System.out.println("==head node== " + currentNode);
        while(currentNode.next != null) {

            // here we increment
            currentNode = currentNode.next;

//            System.out.println("Last time we in here, next is null so print only current");
            System.out.println("==next node== " + currentNode);
        }
    }

    void add(int value){
        Node newNode = new Node(value);

        if(head == null )//Very first time its create the head object when i = 0
        {
            head = newNode;
        }else if(head.next == null){//This is for when i value is 1
            head.next  = newNode;
        }else{ //else part execute i >= 2
            //Created new node with head.next which mean value 1.And head is 0
            Node temp = head.next;
            // Just need this object initialization for reference
            Node temp1 = newNode;
            //Checking head.next is null or not if its null skip this loop execution
            while(temp != null)
            {
                temp1 = temp;
                temp = temp.next;
            }
            // Here we set newNode.next to null
            System.out.println("  ==temp1== " + temp1);// before
            newNode.next = temp1.next;
            temp1.next = newNode;
            System.out.println("  ==temp1== " + temp1);// and after
        }

        System.out.println("==current node== " + head);
        System.out.println();
    }
}

And the Node class with an additional toString() for properly viewing the objects.

public class Node {

    int data;
    Node next;

    public Node(int d) {
        data = d;
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                ", next=" + next +
                '}';
    }
}

Upvotes: 1

GhostCat
GhostCat

Reputation: 140457

"You" do have the head element.

Have a look at your code: your LinkedList class has a field head; and whenever you call the add() method of your list; that field is accessible by that method.

So, adding works like this:

  1. If the head is not set, a new one is created
  2. If the head is set, but has no "next", then that next node is created and linked to head
  3. If the head is set, and his "next", then you keep retrieving the "next" next ... until you find one the last one; which doesn't have a next (yet) ...

That is all there is to understand. Or lets try some non IT-example.

Assume you some hook and short ropes; and you want to build a "list of ropes".

  1. No list yet. You take the first rope and attach it to the hook.
  2. The first rope, your head, is there. You add another rope, by connecting it to the end of the first one (probably making a knot)
  3. Adding another rope ... you start at the hook, and you keep following the ropes/knots ... until you have a loose end.

Hope that helps.

Upvotes: 0

Related Questions