<<< List size and distance between the nodes     Index     Node copy constructor >>>

10. Print contents of the list

  • Again, list traversal by linear search algorithm:

    
    #include <iostream>
    using namespace std;
    // Node.h
    class Node {
        friend void print_list( Node const& head );
        Node* pnext;
    public:
        int data;
        // ...
    };
    
    // Print contents of the list:
    void print_list( Node const& head )
    {
        cout << '[' << head.size() << "] ";
        Node const* iter = &head;
        do {
            cout << iter->data << ' ';
            iter = iter->pnext;
    
        } while ( iter != NULL );
        cout << '\n';
    }
    
    
  • 
    #include "Node.h"
    int main ( )
    {
        Node A;
        Node B;
        Node C;
    
        A.data = 12;
        B.data = 99;
        C.data = 37;
    
        A.insert( &C );
        A.insert( &B );
    
        print_list( A );
    
        return 0;
    }
    /* Program output:
    [3] 12 99 37
    */
    
    

      Singly-linked list

<<< List size and distance between the nodes     Index     Node copy constructor >>>