0

I am new to Java. I have a been trying linked list using class but I encountered some problem.

class Node
{
  int data;
  Node next;
}
class LinkedListOperations
{
  public static void main(String args[])throws IOException
  {
    Node element = new Node();
    Node head;
    .......
    .......
    .......
  }
}

As per my knowledge, the statement Node element = new Node() creates an object called element consisting of 2 fields namely next and data, but I am not sure what does Node head do? What will be stored in Node head? Will there be only next field or data field or both the fields? Can someone explain using an example so that I can have a clear picture?

  • It declares a variable of type Node, named head. This variable will be null by default until assigned a value. Also, your question will likely soon be deleted as it's already been addressed. No big deal, just letting you know. – William Morrison Oct 13 '19 at 14:10
  • Will it have both the fields, data and next? If so, then what is the difference between `Node element=new Node()` and `Node next`? – Gaurav Khandelwal Oct 13 '19 at 14:14

0 Answers0