C++数据结构——双向链表list实现

本次实现也是练习而已,如有问题,欢迎指出!

编译环境:GCC 7.3、vs 2005

与标准库list的差异:

1. 没有splice

2. 没有uniqe

3. 没有merge

4. 没有sort

好了,直接上代码:

#ifndef __LIST_H__
#define __LIST_H__

#if __cplusplus >= 201103L
#include  // std::forward、std::move、std::remove_const
#include 
#endif

#if __cplusplus >= 201103L
#define null nullptr
#else
#define null NULL
#endif 

template
class list
{
private:
    struct _C_list_node;
    typedef _C_list_node node_type;

public:
    typedef unsigned long      size_type;
    typedef _Tp                value_type;
    typedef value_type &       reference;
    typedef const value_type & const_reference;
    typedef value_type *       pointer;

#if __cplusplus >= 201103L
    typedef value_type &&   rvalue_reference;
#endif 

public:
    template
    struct _iterator_impl
    {
#if __cplusplus >= 201103L
        typedef typename std::remove_const<__ValueType>::type __IteratorValue;
        typedef typename std::add_const<__IteratorValue>::type __ConstIteratorValue;
        typedef _iterator_impl<__IteratorValue> __iterator;
        typedef _iterator_impl<__ConstIteratorValue> __const_iterator;
#else 
        typedef _iterator_impl __iterator;
        typedef _iterator_impl __const_iterator;
#endif 

        __ValueType *_M_value;

        _iterator_impl(__ValueType *v = null)
         : _M_value(v) { }

        _iterator_impl(const __iterator &it)
         : _M_value(it._M_value) { }

        reference operator*()
        { return *_M_value->value; }

        pointer operator->()
        { return _M_value->value; }

        _iterator_impl operator++()
        { 
            _M_value = _M_value->next;
            return *this; 
        }

        _iterator_impl operator++(int)
        { 
            _iterator_impl ret(_M_value); 
            _M_value = _M_value->next;
            return ret;
        }

        _iterator_impl operator--()
        {
            _M_value = _M_value->prev;
            return *this;
        }

        _iterator_impl operator--(int)
        {
            _iterator_impl ret(_M_value); 
            _M_value = _M_value->prev;
            return ret;
        }

        bool operator==(const __iterator &it)
        { return _M_value == it._M_value; }

        bool operator!=(const __iterator &it)
        { return _M_value != it._M_value; }
        
        bool operator==(const __const_iterator &it)
        { return _M_value == it._M_value; }

        bool operator!=(const __const_iterator &it)
        { return _M_value != it._M_value; }

    };

    template
    struct _reverse_iterator_impl
    {
#if __cplusplus >= 201103L
        typedef typename std::remove_const<__ValueType>::type __IteratorValue;
        typedef typename std::add_const<__IteratorValue>::type __ConstIteratorValue;
        typedef _reverse_iterator_impl<__IteratorValue> __riterator;
        typedef _reverse_iterator_impl<__ConstIteratorValue> __rconst_iterator;
#else 
        typedef _reverse_iterator_impl __riterator;
        typedef _reverse_iterator_impl __rconst_iterator;
#endif 

        __ValueType *_M_value;

        _reverse_iterator_impl(const __riterator &it)
         : _M_value(it._M_value) { }
        
        _reverse_iterator_impl(__ValueType *v = null)
         : _M_value(v) { }

        reference operator*()
        { return *_M_value->value; }

        pointer operator->()
        { return _M_value->value; }

        _reverse_iterator_impl operator++()
        { 
            _M_value = _M_value->prev;
            return *this; 
        }

        _reverse_iterator_impl operator++(int)
        { 
            _reverse_iterator_impl ret(_M_value); 
            _M_value = _M_value->prev;
            return ret;
        }
        _reverse_iterator_impl operator--()
        {
            _M_value = _M_value->prev;
            return *this;
        }

        _reverse_iterator_impl operator--(int)
        {
            _reverse_iterator_impl ret(_M_value); 
            _M_value = _M_value->prev;
            return ret;
        }

        bool operator==(const __riterator &it)
        { return _M_value == it._M_value; }

        bool operator!=(const __riterator &it)
        { return _M_value != it._M_value; }
        
        bool operator==(const __rconst_iterator &it)
        { return _M_value == it._M_value; }

        bool operator!=(const __rconst_iterator &it)
        { return _M_value != it._M_value; }

    };

public:
    typedef _iterator_impl               iterator;
    typedef _iterator_impl         const_iterator;
    typedef _reverse_iterator_impl       reverse_iterator;
    typedef _reverse_iterator_impl const_reverse_iterator;

private:
    // [b, e]
    list(node_type *b, node_type *e)
     : _M_head(b), _M_tail(e), _M_size(0) 
    {
        while(b != e)
        {
            ++_M_size;
            b = b->next;
        }
        ++_M_size;
    }
public:
    list()
     : _M_head(null), _M_tail(null), _M_size(0) { }
    
    list(const list &l)
     : _M_head(null), _M_tail(null), _M_size(0) 
    { insert(end(), l.begin(), l.end()); }

    template
    list(ForwardIterator b, ForwardIterator e)
     : _M_head(null), _M_tail(null), _M_size(0) 
    { insert(end(), b, e); }

#if __cplusplus >= 201103L
    list(list &&l)
     : _M_head(null), _M_tail(null), _M_size(0) 
    { swap(std::move(l)); }

    list(std::initializer_list il)
     : list(il.begin(), il.end()) { }
#endif

    ~list()
    {
        while(_M_head != _M_tail)
        { 
            node_type *tmp = _M_head;
            _M_head = _M_head->next;
            delete tmp;
        }
    }

    void push_back(const_reference v)
    { insert(end(), v); }

#if __cplusplus >= 201103L
    void push_back(rvalue_reference v)
    { insert(end(), std::move(v)); }
#endif 

    void push_front(const_reference v)
    { insert(begin(), v); }

#if __cplusplus >= 201103L
    void push_front(rvalue_reference v)
    { insert(begin(), std::move(v)); }
#endif 

    iterator insert(iterator pos, const_reference v)
    {
#if __cplusplus >= 201103L
        return emplace(pos, v);
#else
        node_type *n = null;
        if(pos == end())
        { n = _M_tail = _M_construct_node(null, _M_tail, v); }
        else if(pos == begin())
        { n = _M_tail = _M_construct_node(_M_head, null, v); }
        else 
        { n = _M_construct_node(pos._M_value, pos._M_value->prev, v); }

        _M_init_node();

        ++_M_size;
        return iterator(n);
#endif
    }

#if __cplusplus >= 201103L
    // 返回插入的元素的位置
    iterator insert(iterator pos, rvalue_reference v)
    {
        node_type *n = null;
        if(pos == end())
        { n = _M_tail = _M_construct_node(null, _M_tail, std::move(v)); }
        else if(pos == begin())
        { n = _M_head = _M_construct_node(_M_head, null, std::move(v)); }
        else 
        { n = _M_construct_node(pos._M_value, pos._M_value->prev, std::move(v)); }

        _M_init_node();
        ++_M_size;
        return iterator(n);
    }
#endif 

    template
    iterator insert(iterator pos, _Iterator b, _Iterator e)
    {
        iterator ret;
        while(b != e)
        { ret = insert(pos, *b++); }
        return ret;
    }

#if __cplusplus >= 201103L
    // 返回插入的元素的位置
    template
    iterator emplace(iterator pos, Args && ... args)
    {
        node_type *n = null;
        if(pos == end())
        { n = _M_tail = _M_emplace_node(null, _M_tail, std::forward(args)...); }
        else if(pos == begin())
        { n = _M_head = _M_emplace_node(_M_head, null, std::forward(args)...); }
        else 
        { n = _M_emplace_node(pos._M_value, pos._M_value->prev, std::forward(args)...); }

        _M_init_node();
        ++_M_size;

        return iterator(n);
    }

    template
    void emplace_back(Args && ... args)
    { emplace(end(), std::forward(args)...); }

    template
    void emplace_front(Args && ... args)
    { emplace(begin(), std::forward(args)...); }
#endif 

    void pop_back()
    {
        if(null == _M_tail)
        { return; }
        _M_tail = _M_tail->prev;
        delete _M_tail->next;
        _M_tail->next = null;
        --_M_size;
    }

    void pop_front()
    {
        if(null == _M_head)
        { return; }
        _M_head = _M_head->next;
        delete _M_head->prev;
        _M_head->prev = null;
        --_M_size;
    }

    reference front()
    { return *_M_head->value; }

    reference back()
    { return *_M_tail->value; }

    const_reference front() const
    { return *_M_head->value; }

    const_reference back() const
    { return *_M_tail->value; }

    size_type size() const 
    { return _M_size; }

    bool empty() const 
    { return size() == 0; }

    iterator begin()
    { return iterator(_M_head); }

    const_iterator begin() const
    { return const_iterator(_M_head); }

    iterator end()
    { return iterator(null); }

    const_iterator end() const 
    { return const_iterator(null); }

    reverse_iterator rbegin()
    { return reverse_iterator(_M_tail); }

    const_reverse_iterator rbegin() const 
    { return const_reverse_iterator(_M_tail); }

    reverse_iterator rend()
    { return reverse_iterator(null); }

    const_reverse_iterator rend() const
    { return const_reverse_iterator(null); }

    const_iterator cbegin() const 
    { return begin(); }

    const_iterator cend() const 
    { return end(); }

    const_reverse_iterator crbegin() const 
    { return rbegin(); }

    const_reverse_iterator crend() const 
    { return rend(); }

#if __cplusplus >= 201103L
    void swap(list &&l)
#else 
    void swap(list &l)
#endif
    {
        _M_swap(_M_head, l._M_head);
        _M_swap(_M_tail, l._M_tail);
        _M_swap(_M_size, l._M_size);
    }

    void erase(iterator it)
    { _M_erase(it._M_value); }

    void erase(const_iterator it)
    { _M_erase(it._M_value); }

    void erase(reverse_iterator it)
    { _M_erase(it._M_value); }

    void erase(const_reverse_iterator it)
    { _M_erase(it._M_value); }

    // 切割范围:[b, e)
    list spilt(iterator b, iterator e)
    {
        list ret(b._M_value, e == end() ? _M_tail : e._M_value);
        _M_size -= ret.size();

        if(b != begin())
        {
            if(e != end())
            { _M_linked_node(e._M_value, b._M_value->prev); }
            else 
            {
                _M_tail = b._M_value->prev;
                _M_tail->next = null;
            }
        }
        else if(e != end()) 
        { _M_head = e._M_value; }
        else 
        { _M_head = _M_tail = null; }
        return ret;
    }

    void clear()
    {
#if __cplusplus >= 201103L
        swap(list());
#else 
        list l;
        swap(l);
#endif
    }

private:
    struct _C_list_node
    {
#if __cplusplus >= 201103L
        _C_list_node(rvalue_reference v)
         : next(null), prev(null), value(new value_type(std::move(v))) { }
#endif
        _C_list_node(const_reference v)
         : next(null), prev(null), value(new value_type(v)) { }
        
        _C_list_node(value_type *v)
         : next(null), prev(null), value(v) { }
        
        ~_C_list_node()
        { delete value; }
        
        _C_list_node *next;
        _C_list_node *prev;
        value_type *value;
    };

#if __cplusplus >= 201103L
    template
    node_type* _M_emplace_node(node_type *next, node_type *prev, Args && ... args)
    { return _M_linked_node(next, prev, new node_type(new value_type(std::forward(args)...))); }

    node_type* _M_construct_node(node_type *next, node_type *prev, rvalue_reference value)
    { return _M_linked_node(next, prev, new node_type(std::move(value))); }
#else
    node_type* _M_emplace_node(node_type *next, node_type *prev, const_reference value)
    { return _M_linked_node(next, prev, new node_type(value)); }
#endif
    node_type* _M_construct_node(node_type *next, node_type *prev, const_reference value)
    { return _M_linked_node(next, prev, new node_type(value)); }

    node_type* _M_linked_node(node_type *next, node_type *prev, node_type *cur)
    {
        _M_linked_node(next, cur);
        _M_linked_node(cur, prev);
        return cur;
    }

    // a 在后,b 在前
    void _M_linked_node(node_type *a, node_type *b)
    {
        if(null != a)
        { a->prev = b; }
        if(null != b)
        { b->next = a; }
    }

    void _M_init_node()
    {
        if(null == _M_head)
        { _M_head = _M_tail; }
        if(null == _M_tail)
        { _M_tail = _M_head; }
    }

    void _M_erase(node_type *node)
    {
        if(null == node)
        { return; }

        if(node == _M_head)
        { _M_head = node->next; }
        else if(node == _M_tail)
        { _M_tail = node->prev; }

        _M_linked_node(node->next, node->prev);
        delete node;
        --_M_size;
    }

    template
    void _M_swap(__Type &t1, __Type &t2)
    {
        __Type tmp = t1;
        t1 = t2;
        t2 = tmp;
    }

private:
    node_type *_M_head;
    node_type *_M_tail;
    size_type _M_size;
};

#endif // __LIST_H__

 

你可能感兴趣的:(C++数据结构)