目录
结构
节点结构
template<class T>
struct list_node
{
list_node<T>* _next;
list_node<T>* _prev;
T _data;
list_node(const T& val=T())
:_next(nullptr)
,_prev(nullptr)
,_data(val)
{
}
};
list结构
list是一个带头循环双链表
template<class T>
class list
{
typedef list_node<T> Node;
private:
Node* _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++;
}
}
拷贝构造
同样的,头节点要先处理,这里采用现代写法:用迭代器区间构造一个tmp临时对象,交换他们的_head,就达到了目的。
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(_head, lt._head);
return *this;
}
迭代器实现
由于list的空间不连续,所以不能直接对其进行++,--等操作,因此需要创建一个迭代器类,并对操作符进行重载。
正向迭代器
template<class T, class Ref, class Ptr>
struct list_iterator
{
typedef list_node<T> Node;
typedef list_iterator<T, Ref, Ptr> self;
Node* _node;
list_iterator(Node* node)
:_node(node)
{
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &operator*();
}
bool operator==(const self& it)const
{
return _node == it._node;
}
bool operator!=(const self& it)const
{
return _node != it._node;
}
self& operator++()
{
_node = _node->_next;
return *this;
}
self operator++(int)
{
self tmp(*this);
_node = _node->_next;
return tmp;
}
self& operator--()
{
_node = _node->_prev;
return *this;
}
self operator--(int)
{
self tmp(*this);
_node = _node->_prev;
return tmp;
}
};
反向迭代器
template<class Iterator,class Ref,class Ptr>
class _reverse_iterator
{
typedef _reverse_iterator<Iterator, Ref, Ptr> self;
public:
_reverse_iterator(Iterator it)
:_it(it)
{
}
Ref operator*()
{
Iterator prev = _it;
return *--prev;
}
Ptr operator->()
{
return &operator*();
}
bool operator==(const self& rit)const
{
return _it == rit._it;
}
bool operator!=(const self& rit)const
{
return _it != rit._it;
}
self& operator++()
{
_it--;
return *this;
}
self operator++(int)
{
self tmp(*this);
_it--;
return tmp;
}
self& operator--()
{
_it++;
return *this;
}
self operator--(int)
{
self tmp(*this);
_it++;
return tmp;
}
private:
Iterator _it;
};
然后在list类中对迭代器进行封装。
typedef list_iterator<T, T&, T*> iterator;
typedef list_iterator<T, const T&, const T*> const_iterator;
typedef _reverse_iterator<iterator, T&, T*> reverse_iterator;
typedef _reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;
reverse_iterator rbegin()
{
return reverse_iterator(end());
}
reverse_iterator rend()
{
return reverse_iterator(begin());
}
const_reverse_iterator rbegin()const
{
return const_reverse_iterator(end());
}
const_reverse_iterator rend()const
{
return const_reverse_iterator(begin());
}
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);
}
修改相关函数
insert
//insert后迭代器不失效
iterator insert(iterator pos, const T& val)
{
Node* newnode = new Node(val);
Node* next = pos._node;
Node* prev = next->_prev;
newnode->_next = next;
next->_prev = newnode;
newnode->_prev = prev;
prev->_next = newnode;
return iterator(newnode);
}
erase
//erase后迭代器一定失效
iterator erase(iterator pos)
{
assert(pos != _head);
Node* prev = pos._node->_prev;
Node* next = pos._node->_next;
prev->_next = next;
next->_prev = prev;
delete pos._node;
return iterator(next);
}
clear
void clear()
{
iterator it = begin();
while (it != end())
{
/*iterator del = it++;
delegate del._node;*/
erase(it++);
}
_head->_next = _head->_prev = _head;
}
push_back
void push_back(const T& val)
{
/*Node* tail = _head->_prev;
Node* newnode = new Node(val);
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;*/
insert(end(), val);
}
push_front
void push_front(const T& val)
{
/*Node* newnode = new Node(val);
Node* next = _head->_next;
_head->_next = newnode;
newnode->_prev = _head;
newnode->_next = next;
next->_prev = newnode;*/
insert(begin(), val);
}
pop_back
void pop_back()
{
erase(--end());
}
pop_front
void pop_front()
{
erase(begin());
}
析构函数
~list()
{
clear();
delete _head;
_head = nullptr;
}