Skip to the content of the web site.

Lesson 5.5: A linked list class with a size member variable

This class expands on the previous linked list class by including a list size member variable. This member must be updated whenever a node is added to or removed from the linked list, and the functionality of some member functions can be simplified.

class Linked_list {
    public:
        Linked_list();   // Constructor
       ~Linked_list();   // Destructor

        bool empty() const;
        std::size_t size() const;
        double front() const;
        std::string to_string() const;
        std::size_t find( double const value ) const;
        double operator[]( std::size_t const n ) const;

        void push_front( double const new_value );
        void pop_front();
        void clear();

    private:
        Node *p_list_head_; // Pointer to head node
        std:size_t size_;   // Number of nodes in the linked list
};

View this simple linked list at repl.it.

The class declarations and definitions are in the corresponding Linked_list.hpp file.