Why is sLL.node.value not working but sLL.head.value shows correct output in singly linked list with only one node in Java? Unclear of concept

Viewed 35

I am a beginner in java and have just started working with linked lists. I have written a simple program in java that creates a linked list having only one node with head and tail pointing to the same single node. However, I had some doubts that needed to be clarified and would be grateful to anyone who can clear them.

Here is the code:

  1. Node.java class
public class Node{
  public int value;//Declared value field for node 
  public Node next;//Declared link to next node in java
}
  1. SinglyLinkedList.java class

public class SinglyLinkedList{
  public  Node head;//DEclared head node
  public  Node tail;//Declared tail node

  public Node createsSinglyLinkedList(int inputvalue){
    head=new Node();//Memory allocation using new operator
    tail=new Node();//Memory allocation using new operator
    Node node=new Node();//Creation of node object and allocation of memory using new operator 
    node.next=null;//Since there is single node it point to null as there are no nodes after it
    node.value=inputvalue;//input value field
    head=node;//Memory address of node allocated to next of head as it id first element.
    tail=node;//Memory address of node allocated to next of tail as it is also last element.

    return head;
  } 
}
  1. Main.java class
class Main {
  public static void main(String[] args) {
    SinglyLinkedList sLL=new SinglyLinkedList();//sLL object creation and memory allocation
    sLL.createsSinglyLinkedList(5);//Value inserted
    System.out.println(sLL.head.value);//Linked list printed
  }
}

Here are my questions:

1.System.out.println(sLL.head.value); gives correct output 5. However, System.out.println(sLL.node.value) shows Symbol not found? Is is because Node node=new Node(); creates node object that is local variable while head is instance variable that can be accessed by sLL object .

2.If it is really a local variable will the node object be stored on heap memory or stack memory as read somewhere that local variables are stored on stack memory.

3.Will start be stored in stack or heap memory?

4.Are these Statements correct:

head=node;//Memory address of node allocated to next of head as it id first element.

tail=node;//Memory address of node allocated to next of tail as it is also last element.

5.Also,what does (sLL.head.value) actually do?

2 Answers

The concept of SinglyLinkedLists is, that you have one Node, which is denoted as head and from there you can traverse the next notes until you hit an empty node (in some cases there is an predefined guardian instead).

public class Node{
  public int value;
  public Node next;
}

public class SinglyLinkedList{
  public Node head;
  private Node guardian; // no one needs to know the guardian

  public SinglyLinkedList(){
    guardian = new Node();
    head = guardian;
  }

  public void add(int inputvalue){
    // Create and set new node
    Node newNode = new Node();
    newNode.value(inputvalue);

    // append it to the chain
    newNode.next = head.next;

    // the newly added node is now the new head
    head = newNode;
  }

  public boolean contains(int valueToCheck){
  Node iterator = head;  
  while(iterator != guardian){
    if (iterator.value == valueToCheck){
      return true;
    }
    iterator = iterator.next;
  }

    return false;
  }
}

In the initialization you don't set any values.

A DoublyLinkedList works similiar except, that you can append, and search from both directions. In those situations you have to be careful to not undershoot while traversing. But you can use the same guardian for both thresholds.

  1. System.out.println(sLL.node.value) shows Symbol not found? Is is because Node node=new Node(); creates node object that is local variable while head is instance variable that can be accessed by sLL object

Yes. Objects can access instance variables and not local variables, here node is the instance variable.

  1. If it is really a local variable will the node object be stored on heap memory or stack memory

Local variables are stored on Stack Memory, the stack objects pop as per the program execution.

  1. Will start be stored in stack or heap memory?

There is no start but head, and the value of head will keep changing as per the elements added in the list. head itself will be stored on heap memory as it is the instance variable.

  1. Also,what does (sLL.head.value) actually do?

It will fetch the value (int) of the node which is currently assigned as head

Related