一.list的节点类
// List的节点类
template
struct ListNode
{
T _val;
ListNode* _pPre;
ListNode* _pNext;
ListNode(const T& val = T())
:_val(val)
,_pPre(nullptr)
,_pNext(nullptr)
{}
};
二.list的迭代器类
//List的迭代器类
template
struct ListIterator
{
typedef ListNode* PNode;
typedef ListIterator Self;
PNode _pNode;
ListIterator(PNode pNode = nullptr)
:_pNode(pNode)
{}
ListIterator(const Self& lt)
: _pNode(lt._pNode)
{}
Ref operator*()
{
return _pNode->_val;
}
Ptr 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& lt)
{
return _pNode != lt._pNode;
}
bool operator==(const Self& lt)
{
return _pNode == lt._pNode;
}
};
三.list类
1.须知 typedef 私有成员 模板声明
//私有成员
void empty_init()
{
_pHead = new Node;
_pHead->_pPre = _pHead;
_pHead->_pNext = _pHead;
_Size = 0;
}
PNode _pHead;
size_t _Size;
//typedef
//私有
typedef ListNode Node;
typedef ListNode* PNode;
//公有
typedef ListIterator iterator;
typedef ListIterator const_iterator;
//模板声明
template
template
2.构造函数 拷贝构造函数
// List的构造
list()
{
empty_init();
}
list(int n, const T& value = T())
{
assert(n >= 0);
empty_init();
for (int i = 0; i < n; i++)
{
push_back(value);
}
}
template
list(Iterator first, Iterator last)
{
empty_init();
while (first != last)
{
push_back(*first);
first++;
}
}
//拷贝构造函数
list(const list& lt)
{
empty_init();
for (auto e : lt)
{
push_back(e);
}
}
3.析构函数
~list()
{
clear();
delete _pHead;
_pHead = nullptr;
}
4.赋值重载 =
list& operator=(list lt)
{
swap(lt);
return *this;
}
5.begin end
// List Iterator
iterator begin()
{
//也可这样写 iterator(_pHead->_pNext) 实际就是一个匿名对象
return _pHead->_pNext;
}
iterator end()
{
//也可这样写 iterator(_pHead) 实际就是一个匿名对象
return _pHead;
}
const_iterator begin() const
{
//return const_iterator(_pHead->_pNext);
return _pHead->_pNext;
}
const_iterator end() const
{
//return const_iterator(_pHead);
return _pHead;
}
6.size
size_t size()const
{
return _Size;
}
7.empty
bool empty()const
{
return _Size == 0;
}
8.front back
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;
}
9.push_back pop_back push_front pop_front
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());
}
10.insert
// 在pos位置前插入值为val的节点
iterator insert(iterator pos, const T& val)
{
PNode cur = pos._pNode;
PNode prev = cur->_pPre;
PNode newnode = new Node(val);
prev->_pNext = newnode;
newnode->_pPre = prev;
newnode->_pNext = cur;
cur->_pPre = newnode;
_Size++;
return iterator(newnode);
}
11.erase
// 删除pos位置的节点,返回该节点的下一个位置
iterator erase(iterator pos)
{
assert(!empty());
PNode cur = pos._pNode;
PNode prev = cur->_pPre;
PNode next = cur->_pNext;
prev->_pNext = next;
next->_pPre = prev;
delete cur;
_Size--;
return iterator(next);
}
12.clear
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
13.swap
void swap(list& lt)
{
std::swap(_pHead, lt._pHead);
std::swap(_Size, lt._Size);
}
14.全部代码+测试代码
list.h
#include
namespace bit
{
// List的节点类
template
struct ListNode
{
T _val;
ListNode* _pPre;
ListNode* _pNext;
ListNode(const T& val = T())
:_val(val)
,_pPre(nullptr)
,_pNext(nullptr)
{}
};
//List的迭代器类
template
struct ListIterator
{
typedef ListNode* PNode;
typedef ListIterator Self;
PNode _pNode;
ListIterator(PNode pNode = nullptr)
:_pNode(pNode)
{}
ListIterator(const Self& lt)
: _pNode(lt._pNode)
{}
Ref operator*()
{
return _pNode->_val;
}
Ptr 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& lt)
{
return _pNode != lt._pNode;
}
bool operator==(const Self& lt)
{
return _pNode == lt._pNode;
}
};
//list类
template
class list
{
typedef ListNode Node;
typedef ListNode* PNode;
public:
typedef ListIterator iterator;
typedef ListIterator const_iterator;
///
// List的构造
list()
{
empty_init();
}
list(int n, const T& value = T())
{
assert(n >= 0);
empty_init();
for (int i = 0; i < n; i++)
{
push_back(value);
}
}
template
list(Iterator first, Iterator last)
{
empty_init();
while (first != last)
{
push_back(*first);
first++;
}
}
//拷贝构造函数
list(const list& lt)
{
empty_init();
for (auto e : lt)
{
push_back(e);
}
}
list& operator=(list lt)
{
swap(lt);
return *this;
}
~list()
{
clear();
delete _pHead;
_pHead = nullptr;
}
///
// List Iterator
iterator begin()
{
//也可这样写 iterator(_pHead->_pNext) 实际就是一个匿名对象
return _pHead->_pNext;
}
iterator end()
{
//也可这样写 iterator(_pHead) 实际就是一个匿名对象
return _pHead;
}
const_iterator begin() const
{
//return const_iterator(_pHead->_pNext);
return _pHead->_pNext;
}
const_iterator end() const
{
//return const_iterator(_pHead);
return _pHead;
}
///
// List Capacity
size_t size()const
{
return _Size;
}
bool empty()const
{
return _Size == 0;
}
// List Access
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)
{
PNode cur = pos._pNode;
PNode prev = cur->_pPre;
PNode newnode = new Node(val);
prev->_pNext = newnode;
newnode->_pPre = prev;
newnode->_pNext = cur;
cur->_pPre = newnode;
_Size++;
return iterator(newnode);
}
// 删除pos位置的节点,返回该节点的下一个位置
iterator erase(iterator pos)
{
assert(!empty());
PNode cur = pos._pNode;
PNode prev = cur->_pPre;
PNode next = cur->_pNext;
prev->_pNext = next;
next->_pPre = prev;
delete cur;
_Size--;
return iterator(next);
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
void swap(list& lt)
{
std::swap(_pHead, lt._pHead);
std::swap(_Size, lt._Size);
}
private:
void empty_init()
{
_pHead = new Node;
_pHead->_pPre = _pHead;
_pHead->_pNext = _pHead;
_Size = 0;
}
PNode _pHead;
size_t _Size;
};
//测试构造函数 拷贝构造函数 赋值重载
void test1()
{
listl1(10, 1);
list::iterator it1 = l1.begin();
while (it1 != l1.end())
{
cout << *it1 << ' ';
it1++;
}
cout << endl;
listl2(l1);
it1 = l2.begin();
while (it1 != l2.end())
{
cout << *it1 << ' ';
it1++;
}
cout << endl;
listl3;
l3 = l1;
it1 = l3.begin();
while (it1 != l3.end())
{
cout << *it1 << ' ';
it1++;
}
cout << endl;
listl4(l1.begin(), l1.end());
it1 = l4.begin();
while (it1 != l4.end())
{
cout << *it1 << ' ';
it1++;
}
cout << endl;
}
//测试 push_back push_front 迭代器
void test2()
{
listl1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
l1.push_back(5);
list::iterator it1 = l1.begin();
while (it1 != l1.end())
{
cout << *it1 << ' ';
it1++;
}
cout << '\n';
listl2;
l2.push_front(1);
l2.push_front(2);
l2.push_front(3);
l2.push_front(4);
l2.push_front(5);
it1 = l2.begin();
while (it1 != l2.end())
{
cout << *it1 << ' ';
it1++;
}
cout << '\n';
}
//测试pop_back pop_front
void test3()
{
listl1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
l1.push_back(5);
list::iterator it = l1.begin();
while (it != l1.end())
{
cout << *it << ' ';
it++;
}
cout << '\n';
l1.pop_back();
it = l1.begin();
while (it != l1.end())
{
cout << *it << ' ';
it++;
}
cout << '\n';
l1.pop_front();
l1.pop_front();
it = l1.begin();
while (it != l1.end())
{
cout << *it << ' ';
it++;
}
cout << '\n';
}
//测试insert erase
void test4()
{
listl1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
l1.push_back(5);
list::iterator it = l1.begin();
while (it != l1.end())
{
it++;
l1.insert(it, 100);
}
it = l1.begin();
while (it != l1.end())
{
cout << *it << ' ';
it++;
}
cout << '\n';
it = l1.begin();
while (it != l1.end())
{
it = l1.erase(it);
it++;
}
it = l1.begin();
while (it != l1.end())
{
cout << *it << ' ';
it++;
}
cout << '\n';
}
//测试front back clear empty
void test5()
{
listl1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
l1.push_back(5);
list::iterator it = l1.begin();
while (it != l1.end())
{
cout << *it << ' ';
it++;
}
cout << '\n';
cout << l1.front() << endl;
cout << l1.back() << endl;
cout << l1.empty() << endl;
l1.clear();
cout << l1.empty() << endl;
}
};
test.cpp
#include
using namespace std;
#include "list.h"
int main()
{
bit::test5();
return 0;
}