Skip to the content of the web site.

Lesson 5.7: A linked list class with templates

This class expands on the previous linked list class by including templates. Thus, rather than having only a linked list of doubles, this allows for the user to specify the type that is being stored.

template <typename T>
class Linked_list {
    public:
        Linked_list();   // Constructor
       ~Linked_list();   // Destructor

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

        void push_front( T const &new_value );
        void push_back( T const &new_value );
	Linked_list &operator+=( Linked_list<T> const &list ) const;
        void pop_front();
        void clear();

    private:
	Node<T> *p_list_head_; // Pointer to head node
	Node<T> *p_list_tail_; // Pointer to tail 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.