<<< C++ Node class for singly-linked list     Index     Inserting a node into a singly-linked list >>>

6. The Node class constructor

  • Each node keeps user data and a pointer to the next node in the list.

  • It is a good idea to initialize the pointer with NULL, indicating that no further linkage is present.

  • NULL becomes the end marker of the singly-linked list.

  • The definition of NULL comes from a number of the standard library headers, such as <iostream> and <cstddef>.

  • NULL can be assigned to a pointer of any type.

  • 
    class Node {
        Node* pnext;
    public:
        int data;
    
        // Constructor taking initial value:
        Node( int value = 0 )
        : pnext( NULL ), data( value )
        {
        }
    };
    
    

      Single node with NULL pointer

<<< C++ Node class for singly-linked list     Index     Inserting a node into a singly-linked list >>>