一、list的使用和介绍
list的介绍
1、list底层是
双向循环链表结构,
可以在常数时间内在任意位置进行插入和删除元素
的序列式容器,该容器可以前后双向迭代。
2、list最大的缺陷是不支持随机访问,并且还需要一些额外的空间,以保存每个节点相关联的信息(指向前和指向后的指针)。
list的使用
list的构造
构造函数 | 接口说明 |
---|---|
list() | 构造空的list |
list(size_type n, const value_type& val = value_type()) | 构造的list中包含n个值为val的元素 |
list(const list& x) | 拷贝构造函数 |
list(InputIterator first,InputIterator last) | 用[first, last)区间中的元素构造list |
list iterator的使用
我们可以暂时将迭代器理解成一个指针,指向list中的一个节点。
函数声明 | 接口说明 |
---|---|
begin + end | 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器 |
rbegin + rend | 返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的 reverse_iterator,即begin位置 |
list capacity
函数声明 | 接口说明 |
---|---|
empty | 检测list是否为空,是返回true,否则返回false |
size | 返回list中有效节点个数 |
list element access
函数声明 | 接口说明 |
---|---|
front | 返回list的第一个节点中值的引用 |
back | 返回list的最后一个节点中值的引用 |
list modifiers
函数声明 | 接口说明 |
---|---|
push_front | 在list首元素前插入值为val的元素 |
pop_front | 删除list中第一个元素 |
push_back | 在list尾部插入值为val的元素 |
pop_back | 删除list中最后一个元素 |
insert | 在list position 位置中插入值为val的元素,返回插入节点的指针 |
erase | 删除list position位置的元素,返回删除位置下一个节点的指针 |
swap | 交换两个list中的元素 |
clear | 清空list中的有效元素 |
list迭代器的失效
迭代器失效即迭代器所指向的节点无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行
插入时是不会导致list的迭代器失效的
,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
二、list的模拟实现
list整体框架图
我们分为三部分实现list类,分别是:list的节点类,list的迭代器,以及list类。
list的节点类
list的节点类包含一个数据类型,以及两个额外的指针,分别指向前一个节点和后一个节点。
namespace Mylist{
template<class T>
struct __list_node{
T _date;
__list_node *_next;
__list_node *_prev;
__list_node(const T& x=T())
:_date(x),_next(nullptr),_prev(nullptr)
{}
};
}
list的迭代器
因为list迭代器要模拟指针的行为,所以我们要将原生态指针进行封装,所以我们在自定义的类中必须定义以下方法:
- 指针可以解引用,迭代器的类中必须重载operator*()。
- 指针可以通过->访问其所指空间成员,迭代器类中必须重载operator->()。
- 指针可以++向后移动,迭代器类中必须重载operator++()与operator++(int)。对于list还可以- -向前移动,迭代器类中需要重载operator- -()与operator- -(int)。
- 指针需要进行是否相等的比较,因此还需要重载operator==()与operator!=()。
list的迭代器模拟实现代码
namespace Mylist{
// <T, T&, T*> iterator
// <T, const T&, const T*> const_iterator
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)
{}
bool operator==(const self& s) const{
return _node==s._node;
}
bool operator!=(const self& s) const{
return _node!=s._node;
}
Ref operator*()const{
return _node->_date;
}
Ptr operator->()const{
return &(operator*());
}
self& operator++(){
_node=_node->_next;
return *this;
}
self& operator--(){
_node=_node->_prev;
return *this;
}
self operator++(int){
self tmp(*this);
++*this;
_node=_node->_next;
return tmp;
}
self operator--(){
self tmp(*this);
--*this;
return tmp;
}
};
}
list类
为了符合STL前闭后开的原则,我们可以在循环list的尾部加一个空结点。
namespace Mylist{
template<class T>
class list {
typedef __list_node<T> node;
public:
typedef __list_iterator<T,T&,T*> iterator;
typedef __list_iterator<T,const T&,const T*> const_iterator;
iterator begin(){
return _head->_next;
}
const_iterator begin()const{
return _head->_next;
}
const_iterator end()const{
return _head;
}
iterator end(){
return _head;
}
list() {
_head = new node();
_head->_next = _head;
_head->_prev = _head;
}
template<class InputIterator>
list(InputIterator first, InputIterator last) {
_head = new node;
_head->_next = _head;
_head->_prev = _head;
while (first != last) {
push_back(*first);
++first;
}
}
//拷贝构造函数
list(const list<T>& lt) {
_head = new node();
_head->_next = _head;
_head->_prev = _head;
list<T> tmp(lt.begin(),lt.end());
swap(_head,tmp._head);
}
//赋值运算符
list<T>& operator=(list<T> lt) {
swap(lt._head,_head);
return *this;
}
//清除所有节点包括头节点
~list() {
clear();
delete _head;
_head = nullptr;
}
//清除除头节点以外的节点
void clear() {
iterator it = begin();
while (it != end()) {
//it=erase(it);
erase(it++);
}
}
void push_back(const T& x) {
/*node* newNode = new node(x);
node* tail = _head->_prev;
tail->_next = newNode;
newNode->_prev = tail;
newNode->_next = _head;
_head->_prev = newNode;*/
insert(end(),x);
}
void push_front(const T& x) {
insert(begin(),x);
}
void pop_back() {
erase(--end());
}
void pop_front() {
erase(begin());
}
//在迭代器为pos的节点,插入节点x
iterator insert(iterator pos,const T& x) {
node* newNode = new node(x);
node* cur = pos._node;
node* prev = cur->_prev;
//prev newNode cur连接
prev->_next = newNode;
newNode->_prev = prev;
newNode->_next = cur;
cur->_prev = newNode;
return newNode;
}
//删除迭代器为pos的节点
iterator erase(iterator pos) {
assert(pos != end());//不能删除头节点
node* cur = pos._node;
node* prev = cur->_prev;
node* next = cur->_next;
delete cur;
prev->_next = next;
next->_prev = prev;
return next;
}
private:
//双向带头循环,空结点
node* _head;
};
}