在看底层代码之前我们需要了解几个问题
问题一:STL链表是一个什么结构?
STL链表是一个双向循环链表。
问题二:
那么list类中怎么构建这样的一个双向链表呢?
先构建一个无任何作用的头结点,如果链表需要新增节点,那么只需要插入即可
问题三:
解决完链表的构造,那么另一个重要的问题:迭代器的实现
我们知道迭代器支持前置++ 、- -、后置++ 、- -以及解引用、->等等、*
对于string、vector等容器、迭代器可以看作一个指针;
++、- -等只需要指针进行++ 、–进行前后移动即可 。解引用等更加简单、不再赘述。
但是链表呢?由于地址不连续、无法像string、vector一样进行操作。
为了实现上述的操作
链表迭代器的实现与string、vector不同,不再是指针,而是***一个封装的类***
代码实现:
namespace List{
//定义链表节点
template <class T>
struct _list_node{
T _data;
_list_node<T>* _prev;
_list_node<T>* _next;
_list_node(const T& data= T())
:_data(data),
_prev(nullptr),
_next(nullptr)
{}
};
//链表节点定义结束
//用一个结构体封装迭代器
template<class T,class Ref,class Ptr>
struct _list_iterator{
typedef _list_iterator<T, Ref, Ptr> Self;
typedef _list_node<T> node;
node* _node;
_list_iterator(node* node)
:_node(node)
{}
//解引用,使用这个可以获得该节点的数值,即要返回一个变量(节点的数据)
Ref operator*()
{
return _node->_data;
}
// -> 返回一个指针
Ptr operator->()
{
return &(operator*() )
}
Self& operator++ ()
{
_node = _node->_next;
return *this;
}
Self& operator++ (int)
{
Self self = _node;
++(*this);
return self;
}
Self& operator-- ()
{
_node = _node->_prev;
return *this;
}
Self& operator-- (int)
{
Self self = *this;
--(*this);
return self;
}
bool operator== (const Self& x)
{
return _node == x._node;
}
bool operator!= (const Self& x)
{
return _node != x._node;
}
};
//链表迭代器封装完毕
template <class T>
class list{
typedef _list_node<T> node;
public:
list()
{
_head = new node;
_head->_next = _head;
_head->_prev = _head;
}
list(const list<T>& lt)
{
_head = new node;
_head->_prev = _head;
_head->_next = _head;
for (auto e : lt)
{
push_back(e);
}
}
//拷贝构造
//list<T>& operator=(const list<T>& lt)
//{
// if (this != (<))
// {
// //将原有节点全部清除
// clear();
// //将lt的节点拷贝过来
// for (auto e : lt)
// {
// push_back(e);
// }
// }
//
// return *this;
//}
list<T>& operator=(list<T>& lt)
{
swap(this->_head, lt._head);
return *this;
}
~list()
{
clear();
delete _head;
_head = nullptr;
}
public:
typedef _list_iterator<T ,T& ,T*> iterator;
typedef _list_iterator< T,const T&,const T*> const_iterator;
//typedef _list_iterator<const T, const T&, const T*> const_iterator;
iterator begin()
{
return (iterator)(_head->_next);
}
iterator end()
{
return (iterator)(_head);
}
const_iterator begin() const
{
return (const_iterator)(_head->_next);
}
const_iterator end() const
{
return (const_iterator)(_head);
}
size_t size()const
{
size_t ret = 0;
iterator start = begin();
while (start != end())
{
ret++;
start++;
}
return ret;
}
bool empty()const
{
return _head->_next == _head;
}
T& front()
{
return *begin();
}
const T& front()const
{
return *begin();
}
T& back()
{
return *(--end());
}
const T& back()const
{
return *(--end());
}
void push_back(const T& val)
{
insert( end() , val);
}
void pop_back()
{
erase(--end());
}
void push_front(const T& val)
{
insert(begin(), val);
}
void pop_front()
{
erase(begin());
}
// 在pos位置前插入值为val的节点
iterator insert(iterator pos, const T& val)
{
node* newnode=new node(val);
node* cur = pos._node;
node* cur_prev = cur->_prev;
cur_prev->_next = newnode;
newnode->_prev = cur_prev;
newnode->_next = cur;
cur->_prev = newnode;
return (iterator)newnode;
}
// 删除pos位置的节点,返回该节点的下一个位置
iterator erase(iterator pos)
{
node* cur = pos._node;
node* pre = cur->_prev;
node* next = cur->_next;
pre->_next = next;
next->_prev = pre;
delete cur;
cur = nullptr;
return (iterator)next;
}
void clear()
{
iterator start = begin();
while (start != end())
{
iterator it = ++start;
erase(start);
start = it;
}
_head->_prev = _head;
_head->_next = _head;
}
void swap(list<T>& lt)
{
::swap(_head, lt._head);
}
private:
node* _head;
};
}