#Doubly linked list & iterator

12 messages · Page 1 of 1 (latest)

tiny rampart
#

Hey so i was hoping someone could help me understand how to go about implementing this class. I have to make this class called dlist, which makes a doubly linked list from an array of data or from a std container like vector, and has functions that work on it and an iterator class that can work with functions/algorithms from std library.

Im just really confused about how the iterator class works, like why're there a lot of duplicate functions inside class dlist and class iterator? Shouldnt there only be one definition for those functions? I just dont know how the relation between the 2 classes work and idk how to implement it. I know how a doubly linked list works though. Thanks in advance

ancient bayBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

tiny rampart
#
template <typename T>
    class dlist
    {
    public:
        // Types
        class iterator;
        typedef size_t size_type;
        typedef T value_type;
        typedef const T const_reference;
        // Default constructor
        dlist();
        // Copy constructor
        dlist(const dlist &);
        // Iterator range constructor
        template <typename InputIterator> dlist(InputIterator first,
            InputIterator last);
            // Destructor
            ~dlist();
        // Copy assginment operator
        dlist &operator=(const dlist &);
        // empty() & size()
        bool empty() const;
        size_type size() const;
        // front() & back()
        T &front();
        const T &front() const;
        T &back();
        const T &back() const;
        // Modifiers
        void push_front(const T &);
        void pop_front();
        void push_back(const T &);
        void pop_back();
        iterator insert(iterator, const T &);
        iterator erase(iterator);
        // Comparision
        bool operator==(const dlist &) const;
        bool operator!=(const dlist &) const;
        bool operator<(const dlist &) const;
        bool operator<=(const dlist &) const;
        bool operator>(const dlist &) const;
        bool operator>=(const dlist &) const;
        // Iterators
        iterator begin();
        const iterator begin() const;
        iterator end();
        const iterator end() const;
    private:
        // You decide what goes here
    };
    

}```
#

iterator class:

template <typename T>
    class dlist<T>::iterator : public std::iterator<bidirectional_iterator_tag, T>
    {
        friend class dlist<T>;
    public:
        typedef const T const_reference;
        iterator();
        explicit iterator(typename dlist<T>::node *);
        bool operator==(const iterator &) const;
        bool operator!=(const iterator &) const;
        T &operator*();
        const T &operator*() const;
        // Destructor
        ~dlist();
        // Copy assginment operator
        dlist &operator=(const dlist &);
        // empty() & size()
        bool empty() const;
        size_type size() const;
        // front() & back()
        T &front();
        const T &front() const;
        T &back();
        const T &back() const;
        // Modifiers
        void push_front(const T &);
        void pop_front();
        void push_back(const T &);
        void pop_back();
        iterator insert(iterator, const T &);
        iterator erase(iterator);
        // Comparision
        bool operator==(const dlist &) const;
        bool operator!=(const dlist &) const;
        bool operator<(const dlist &) const;
        bool operator<=(const dlist &) const;
        bool operator>(const dlist &) const;
        bool operator>=(const dlist &) const;
        // Iterators
        iterator begin();
        const iterator begin() const;
        iterator end();
        const iterator end() const;
    private:
        // You decide what goes here
    };```
#

Those are the 2 classes declarations

#

And im not really sure what the purpose of typedef size_t size_type; typedef T value_type; typedef const T const_reference;
is?

languid falcon
tiny rampart
#

Hm okay, im not familiar with iterator_traits and such, Ig ill have to look into it

#

Thanks for the links ^^

ancient bayBOT
#

@tiny rampart Has your question been resolved? If so, run !solved :)

tiny rampart
#

!solved