1.list介绍
2.默认成员函数
3.迭代器
4.增删查改
1.list介绍
list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。
2.默认成员函数
typedef ListNode<T> Node;
typedef Node* PNode;
private:
Node* head;
public:
typedef ListIterator<T, T&, T*> iterator;
typedef ListIterator<T, const T&, const T&> const_iterator;
public:
///
// List的构造
void Initlist()
{
head = new Node;
head->_pNext = head;
head->_pPre = head;
head->_val = 0;
}
list()
{
Initlist();
}
list(int n, const T& value = T())
{
Initlist();
for (int i = 0; i < n; ++i)
push_back(value);
}
template <class Iterator>
list(Iterator first, Iterator last)
{
Initlist();
while (first != last)
{
push_back(*first);
first++;
}
}
list(const list<T>& l)
{
Initlist();
for (auto e : l)
{
push_back(e);
}
}
list<T>& operator=(const list<T> l)
{
for (auto it:l)
{
push_back(it);
}
return *this;
}
~list()
{
clear();
delete head;
head = nullptr;
}
3.迭代器
a.begin+end:返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器。
b.rbegin+rend:返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的 reverse_iterator,即begin位置。
迭代器失效:迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代 器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
class ListIterator
{
typedef ListNode<T>* PNode;
typedef ListIterator<T, Ref, Ptr> Self;
public:
ListIterator(PNode pNode = nullptr)
:_pNode(pNode)
{}
ListIterator(const Self& l)
{
_pNode = l._pNode;
}
T& operator*()
{
return _pNode->_val;
}
T* operator->()
{
return &_pNode->_val;
}
Self& operator++()
{
_pNode = _pNode->_pNext;
return *this;
}
Self operator++(int)
{
Self tmp(this);
_pNode = _pNode->_pNext;
return tmp;
}
Self& operator--()
{
_pNode = _pNode->_pPre;
return *this;
}
Self& operator--(int)
{
Self tmp(this);
_pNode = _pNode->_pPre;
return tmp;
}
bool operator!=(const Self& l)
{
return _pNode!=l._pNode;
}
bool operator==(const Self& l)
{
return _pNode == l._pNode;
}
private:
PNode _pNode;
};
///
// List Iterator
iterator begin()
{
return head->_pNext;
}
iterator end()
{
return head;
}
const_iterator begin()
{
return head->_pNext;
}
const_iterator end()
{
return head;
}
5.增删查改
a.empty:检测list是否为空,是返回true,否则返回false。
b.size:返回list中有效节点的个数。
c.front:返回list的第一个节点中值的引用。
d.back:返回list的最后一个节点中值的引用。
e.push_front:在list首元素前插入值为val的元素。
f.pop_back:删除list中第一个元素。
g.push_back::在list尾部插入值为val的元素。
h.pop_back: 删除list中最后一个元素。
i.insert: 在list position 位置中插入值为val的元素。
j.erase: 删除list position位置的元素。
k.swap: 交换两个list中的元素。
l.clear: 清空list中的有效元素。
///
// List Capacity
size_t size()const
{
size_t i = 0;
auto it = begin();
while (it!= end())
{
i++;
it++;
}
}
bool empty()const
{
return size() == 0;
}
// List
T& front()
{
assert(!empty());
return _pHead->_pNext->_val;
}
const T& front()const
{
assert(!empty());
return _pHead->_pNext->_val;
}
T& back()
{
assert(!empty());
return _pHead->_pPre->_val;
}
const T& back()const
{
assert(!empty());
return _pHead->_pPre->_val;
}
// List Modify
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._pNode;
cur->_pPre->_pNext = newnode;
newnode->_pPre = cur->_pPre;
newnode->_pNext = cur;
cur->_pPre = newnode;
return (iterator)newnode;
//return newnode;
}
// 删除pos位置的节点,返回该节点的下一个位置
iterator erase(iterator pos)
{
Node* cur = pos._pNode;
Node* next = cur->_next;
cur->_pPre->_pNext = cur->_pNext;
cur->_pNext->_pPre = cur->_pPre;
delete cur;
return (iterator)next;
}
void clear()
{
auto it = begin();
while (it != end())
{
erase(it);
}
}
void swap(list<T>& l)
{
std::swap(head, l.head);
}
完整代码如下:
namespace bite
{
// List的节点类
template<class T>
struct ListNode
{
ListNode(const T& val = T());
ListNode<T>* _pPre;
ListNode<T>* _pNext;
T _val;
ListNode(const T*x=T())
:_pPre(nullptr)
, _pNext(nullptr)
,_val(x)
{}
};
//List的迭代器类
template<class T, class Ref, class Ptr>
class ListIterator
{
typedef ListNode<T>* PNode;
typedef ListIterator<T, Ref, Ptr> Self;
public:
ListIterator(PNode pNode = nullptr)
:_pNode(pNode)
{}
ListIterator(const Self& l)
{
_pNode = l._pNode;
}
T& operator*()
{
return _pNode->_val;
}
T* operator->()
{
return &_pNode->_val;
}
Self& operator++()
{
_pNode = _pNode->_pNext;
return *this;
}
Self operator++(int)
{
Self tmp(this);
_pNode = _pNode->_pNext;
return tmp;
}
Self& operator--()
{
_pNode = _pNode->_pPre;
return *this;
}
Self& operator--(int)
{
Self tmp(this);
_pNode = _pNode->_pPre;
return tmp;
}
bool operator!=(const Self& l)
{
return _pNode!=l._pNode;
}
bool operator==(const Self& l)
{
return _pNode == l._pNode;
}
private:
PNode _pNode;
};
//list类
template<class T>
class list
{
typedef ListNode<T> Node;
typedef Node* PNode;
private:
Node* head;
public:
typedef ListIterator<T, T&, T*> iterator;
typedef ListIterator<T, const T&, const T&> const_iterator;
public:
///
// List的构造
void Initlist()
{
head = new Node;
head->_pNext = head;
head->_pPre = head;
head->_val = 0;
}
list()
{
Initlist();
}
list(int n, const T& value = T())
{
Initlist();
for (int i = 0; i < n; ++i)
push_back(value);
}
template <class Iterator>
list(Iterator first, Iterator last)
{
Initlist();
while (first != last)
{
push_back(*first);
first++;
}
}
list(const list<T>& l)
{
Initlist();
for (auto e : l)
{
push_back(e);
}
}
list<T>& operator=(const list<T> l)
{
for (auto it:l)
{
push_back(it);
}
return *this;
}
~list()
{
clear();
delete head;
head = nullptr;
}
///
// List Iterator
iterator begin()
{
return head->_pNext;
}
iterator end()
{
return head;
}
const_iterator begin()
{
return head->_pNext;
}
const_iterator end()
{
return head;
}
///
// List Capacity
size_t size()const
{
size_t i = 0;
auto it = begin();
while (it!= end())
{
i++;
it++;
}
}
bool empty()const
{
return size() == 0;
}
// List
T& front()
{
assert(!empty());
return _pHead->_pNext->_val;
}
const T& front()const
{
assert(!empty());
return _pHead->_pNext->_val;
}
T& back()
{
assert(!empty());
return _pHead->_pPre->_val;
}
const T& back()const
{
assert(!empty());
return _pHead->_pPre->_val;
}
// List Modify
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._pNode;
cur->_pPre->_pNext = newnode;
newnode->_pPre = cur->_pPre;
newnode->_pNext = cur;
cur->_pPre = newnode;
return (iterator)newnode;
//return newnode;
}
// 删除pos位置的节点,返回该节点的下一个位置
iterator erase(iterator pos)
{
Node* cur = pos._pNode;
Node* next = cur->_next;
cur->_pPre->_pNext = cur->_pNext;
cur->_pNext->_pPre = cur->_pPre;
delete cur;
return (iterator)next;
}
void clear()
{
auto it = begin();
while (it != end())
{
erase(it);
}
}
void swap(list<T>& l)
{
std::swap(head, l.head);
}
private:
PNode _pHead;
};
};