<<< C-style Linked List Example     Index     The Node class constructor >>>

5. C++ Node class for singly-linked list

  • A typical C++ class has private member variables and public member functions to access and modify data.

  • This is appropriate design model for a linked list constructed from node objects with basic techniques for:

    • data read/write access

    • building and maintaining linkage between nodes to form a list.

  • As before, the linked list is constructed using pointers:

      Singly-linked list

  • 
    class Node {
        Node* pnext;
    public:
        int data;
    
        // Constructor taking initial value:
        Node( int value = 0 );
    
        // Insert node in front:
        void insert( Node* newNode );
    
        // Remove node in front:
        void remove_next();
    
        // Calculate number of nodes in the list:
        size_t size() const;
    
        // Calculate distance to other node:
        int distance( Node const* other ) const;
    };
    
    
<<< C-style Linked List Example     Index     The Node class constructor >>>