STL list源码解析

1.list的底层实现

template<class T>
struct __list_node 
{
    typedef __list_node<T>* node_pointer;
    node_pointer prev;
    node_pointer next;
    T data;
}

底层是双向链表

2.迭代器的实现

template <class T>
class __list_iterator
{
    typedef __list_iterator<T> self;
    typedef __list_node<T>* link_type;
    
    link_type ptr;
    __list_iterator(link_type p = NULL): ptr(p) {}
    ......

    T& operator *()
    {
        return ptr->data;
    }

    T* operator->()
    {
        return &(operator*());
    }

    self& operator++(){
        ptr = ptr->next;
        return *this;
    }

    self operator++(int){
        self tmp = *this;
        ++(*this);
        return *tmp;
    }

    self& operator--(){
        ptr = ptr->prev;
        return *this;
    }

    self operator--(int){
        self tmp = *this;
        --(*this);
        return tmp;
    }

    bool operator==(const __list_iterator& rhs){
        return ptr == rhs.ptr;
    }

    bool operator!=(const __list_iterator& rhs){
        return !(*this==rhs);
    }
}

3.list的实现

template<class T, Alloc=alloc>
class list
{
protected:
    typedef __list_node<T> list_node;
    typedef simple_alloc<Alloc, list_node> list_node_allocator;

public:
    typedef T                  value_type;
    typedef T&                 reference;
    typedef value_type*        pointer;
    typedef list_node*         link_type;
    typedef const value_type*  const_pointer;

    typedef size_t             size_type;

public:
    typedef __list_iterator<value_type> iterator;

private:
    link_type node; // 只要一个指针,便可表示整个环状双向链表
public:
    iterator begin() {return (link_type)(node->next);} // 构造函数
    iterator end() {return node;}
    size_type size() {
        size_type result = 0;
        distance(begin(), end(), result);
        return result;
    }
    bool empty() const 
    {
        return node->next == node;
    }
    reference front() {return *begin();}
    refrence back(); {return *(--end());}
}

下面的操作用来配置,释放,构造,销毁一个节点

申请一个节点的内存

link_type get_node() {return list_node_allocator::allocate();}

释放一个节点

void put_node(link_type p) {list_node_allocator::deallocate(p);}

申请内存并构造一个节点

link_type create_node(const T& x)
{
    link_type p = get_node();
    construct(&p->data, x);
    return p;
}

析构并释放一个节点

void destroy_node(link_type p)
{
    destroy(&p->data);
    put_node(p);
}

5.构造函数

public:
list() {empty_initialize();}
protected:
    void empty_initialize()
{
    node = get_node();
    node->next = node;
    node->prev = node;
}

6.插入

iterator insert(iterator position, const T& x)
{
    link_type tmp = create_node(x);
    tmp->next = position.node;
    tmp->prev = position.node->prev;
    (link_type(position.node->prev))->next = tmp;
    position.node->prev = tmp;
    return tmp;
}

push_front():

void push_front(const T& x) {insert(begin(), x);}

push_back():

void push_back(const T& x) {insert(end(), x);}

erase(iterator position)

void erase(iterator position)
{
    link_type prev_node = (link_type)(position.node->prev);
    link_type next_node = (link_type)(position.node->next);
    prev_node->next = next_node;
    next_node->prev = prev_node;
    destroy_node(position.node);
    return iterator(next_node);
}

移除头节点

void pop_front(){erase(begin());}

移除末尾节点

void pop_back() {erase(--end());}

清空节点

template<class T, Alloc=alloc>
void list<T, Alloc> clear()
{
    link_type cur = begin();
    while (cur != node)
    {
        link_type tmp = cur;
        cur = cur->next;
        destroy_node(tmp);
    }
    node->prev = node;
    node->next = node;
}

将值为value的元素都去掉

void remove(const T& x)
{
    iterator first = begin();
    iterator last = end();
    while (first != last)
    {
        iterator next = first;
        ++next;
        if (*first == value) erase(first);
        first = next;
    }

}

双向链表去重

void unique()
{
    iterator first = begin();
    iterator last = end();
    if (first == last) return;
    iterator next = first;
    while (++next != last)
    {
        if (*next == *first) erase(next);
        else first = next;
        next = first;
    }
}
发布了81 篇原创文章 · 获赞 4 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/m0_37313888/article/details/105353245