构造
-
- 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
接口
-
- begin end rbegin rend 返回迭代器
- empty 检测list是否为空,是返回true,否则返回false
- size 返回list中有效节点的个
- front 返回list的第一个节点中值的引用
- back 返回list的最后一个节点中值的引用
插入删除
-
- 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的迭代器需要对原生指针进行封装, 对* -> ++ – != == 进行重载, 使其具有指针的行为
#include <iostream>
using namespace std;
template<class T>
struct ListNode
{
ListNode(const T& val = T())
: _val(val)
, _pPre(nullptr)
, _pNext(nullptr){
;
}
ListNode<T>* _pPre;
ListNode<T>* _pNext;
T _val;
};
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->_pNext;
return tmp;
}
bool operator!=(const Self& l){
return _pNode != l._pNode;
}
bool operator==(const Self& l){
return _pNode == l->_pNode;
}
PNode _pNode;
};
template<class T>
class list
{
typedef ListNode<T> Node;
typedef Node* PNode;
public:
typedef ListIterator<T, T&, T*> iterator;
typedef ListIterator<T, const T&, const T*> const_iterator;
public:
list(){
CreateHead();
}
list(int n, const T& value = T()){
CreateHead();
PNode newNode, cur = _pHead;
int i = 0;
for (; i < n; ++i){
newNode = new Node(value);
cur->_pNext = newNode;
newNode->_pPre = cur;
newNode->_pNext = _pHead;
_pHead->_pPre = newNode;
cur = newNode;
}
}
template <class Iterator>
list(Iterator first, Iterator last){
CreateHead();
while (first != last){
push_back(*first);
++first;
}
}
list(list<T>& l){
CreateHead();
list<T> tmp(l.begin(), l.end());
swap(tmp);
}
list<T>& operator=(const list<T> l){
clear();
list<T> tmp(l.begin(), l.end());
swap(tmp);
return *this;
}
~list(){
clear();
delete _pHead;
_pHead = nullptr;
}
iterator begin(){
return iterator(_pHead->_pNext);
}
iterator end(){
return iterator(_pHead);
}
const_iterator const_begin(){
return const_iterator(_pHead->_pNext);
}
const_iterator const_end(){
return const_iterator(_pHead);
}
size_t size()const{
size_t count = 0;
PNode cur = _pHead->_pNext;
while (cur != _pHead){
++count;
cur = cur->_pNext;
}
return count;
}
bool empty()const{
return _pHead->_pNext == _pHead;
}
const T& front()const{
return _pHead->_pNext->_val;
}
T& front(){
return _pHead->_pNext->_val;
}
T& back(){
return _pHead->_pPre;
}
const T& back()const{
return _pHead->_pPre;
}
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());
}
iterator insert(iterator pos, const T& val){
PNode pre = pos._pNode->_pPre;
PNode next = pos._pNode;
PNode newNode = new Node(val);
pre->_pNext = newNode;
newNode->_pPre = pre;
newNode->_pNext = next;
next->_pPre = newNode;
return iterator(newNode);
}
iterator erase(iterator pos){
if (_pHead != _pHead->_pNext){
PNode pre = pos._pNode->_pPre;
PNode next = pos._pNode->_pNext;
pre->_pNext = next;
next->_pPre = pre;
delete pos._pNode;
pos._pNode = nullptr;
return iterator(next);
}
return nullptr;
}
void clear(){
if (_pHead != _pHead->_pNext){
PNode cur = _pHead->_pNext;
PNode next = cur->_pNext;
while (next != _pHead){
delete cur;
cur = next;
next = next->_pNext;
}
delete cur;
cur = nullptr;
}
}
void swap(list<T>& l){
PNode newNode, oldNode;
newNode = _pHead->_pNext;
oldNode = l._pHead->_pNext;
while (oldNode != l._pHead && newNode != _pHead){
T& tmp = oldNode->_val;
oldNode->_val = newNode->_val;
newNode->_val = tmp;
oldNode = oldNode->_pNext;
newNode = newNode->_pNext;
}
if (oldNode != l._pHead){
l._pHead->_pPre = oldNode->_pPre;
oldNode->_pPre->_pNext = l._pHead;
newNode = newNode->_pPre;
while (oldNode != l._pHead){
PNode cur = new Node(oldNode->_val);
newNode->_pNext = cur;
cur->_pPre = newNode;
PNode des = oldNode;
oldNode = oldNode->_pNext;
delete des;
newNode = cur;
}
_pHead->_pPre = newNode;
newNode->_pNext = _pHead;
}
else if (newNode != _pHead){
_pHead->_pPre = newNode->_pPre;
newNode->_pPre->_pNext = _pHead;
oldNode = oldNode->_pPre;
while (newNode != _pHead){
PNode cur = new Node(newNode->_val);
oldNode->_pNext = cur;
cur->_pPre = oldNode;
PNode des = newNode;
newNode = newNode->_pNext;
delete des;
oldNode = cur;
}
l._pHead->_pPre = oldNode;
oldNode->_pNext = l._pHead;
}
}
private:
void CreateHead(){
_pHead = new Node();
_pHead->_pNext = _pHead;
_pHead->_pPre = _pHead;
_pHead->_val = T();
}
PNode _pHead;
};
void testInsert(){
list<int> l;
l.insert(l.begin(), 1);
l.insert(l.begin(), 2);
l.insert(l.begin(), 3);
l.front();
}
void testN_Val(){
list<int> l(10, 1);
size_t s = l.size();
}
void testErase(){
list<int> l;
l.insert(l.begin(), 10);
l.erase(l.begin());
}
void testIterator(){
list<int> l;
l.insert(l.begin(), 1);
l.insert(l.begin(), 2);
l.insert(l.begin(), 3);
list<int> itList(l.begin(), l.end());
}
void testCpy(){
list<int> l;
l.insert(l.begin(), 3);
l.insert(l.begin(), 2);
l.insert(l.begin(), 1);
list<int> l2(l);
list<int> l3 = l;
list<int> l4;
l4.swap(l3);
}
void testOperator(){
list<int> l(10, 2);
list<int> l2 = l;
}
int main(){
return 0;
}