-4
 Public class Node
 {
    int data;
    Node next;

    public Node(int data,Node p)
    {
       this.data=data;
       this.next=p;
    }
}

What is the meaning of Node next; in the above implementation

TheParam
  • 10,113
  • 4
  • 40
  • 51

2 Answers2

0

A linked list is a list where every node in the list has both a data value, plus a link to the next node in the list.

In the above code, each node in the list is represented by a Node object. Each Node object has a data value (data) and the next node in the linked list (next).

Also worth noting is that what you see here is known as a singly-linked list. You can only navigate the list in one direction. There are also doubly-linked lists, where each node has a next and a previous. That allows you to navigate the list in either direction.

Jordan
  • 2,273
  • 9
  • 16
  • does it link to the next object that i create using node class? does "next " contain the pointer address of the next object of the same class? – Dhananjaya Kurukulasooriya Feb 15 '19 at 17:03
  • It doesn't do anything automatically. You'd have to create a new `Node` object, and assign it to the `next` member in your previous `Node` object yourself, using something like `node.next = new Node().` And this raises an interesting issue that I just noticed. You need a constructor that takes an int, but *doesn't* take a new node (and just sets `next` to null). Otherwise, you'll never be able to add the last node in the list. – Jordan Feb 15 '19 at 17:34
  • what is the meaning of "node head=null "mean linked list. – Dhananjaya Kurukulasooriya Feb 16 '19 at 15:57
  • One of the items in your linked list has to be the last item. That item can't have any value in its `next` variable, because there *is* no next; it's the last one. So for the last item, its `next` will be `null`. – Jordan Feb 16 '19 at 16:00
  • Node curNode=head;then curNode.next!=null and curNode!=null mean same thing? – Dhananjaya Kurukulasooriya Feb 19 '19 at 06:12
0
  1. The Node next; refers the next node on the List
  2. This is not a linked list you should have Node previous; as well