目录
list lst; //list采用采用模板类实现,对象的默认构造形式:list(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。list(n,elem); //构造函数将n个elem拷贝给本身。list(const list &lst); //拷贝构造函数。- #include
- #include
- using namespace std;
- #include
-
- void printList(const list<int>& L){
- for(list<int>::const_iterator it =L.begin();it!=L.end();it++){
- cout<<*it<<" ";
- }
- cout<
- }
-
- void test01()
- {
- list<int>L1;//默认构造
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
-
- printList(L1);
-
- list<int>L2(L1.begin(),L1.end());//区间拷贝
- printList(L2);
-
- list<int>L3(L2);//拷贝构造
- printList(L3);
-
- list<int>L4(10, 1000);//构造函数将n个elem拷贝给本身
- printList(L4);
- }
-
- int main() {
-
- test01();
-
- system("pause");
-
- return 0;
- }
-
-
10 20 30 40
10 20 30 40
10 20 30 40
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
list 赋值和交换
assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。assign(n, elem); //将n个elem拷贝赋值给本身。list& operator=(const list &lst); //重载等号操作符swap(lst); //将lst与本身的元素互换。
- #include
- #include
- using namespace std;
- #include
-
- void printList(const list<int>& L) {
-
- for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
- cout << *it << " ";
- }
- cout << endl;
- }
-
- //赋值和交换
- void test01()
- {
- list<int>L1;
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
- printList(L1);
-
- //赋值
- list<int>L2;
- L2 = L1;
- printList(L2);
-
- list<int>L3;
- L3.assign(L2.begin(), L2.end());
- printList(L3);
-
- list<int>L4;
- L4.assign(10, 100);
- printList(L4);
-
- }
-
- //交换
- void test02()
- {
-
- list<int>L1;
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
-
- list<int>L2;
- L2.assign(10, 100);
-
- cout << "交换前: " << endl;
- printList(L1);
- printList(L2);
-
- cout << endl;
-
- L1.swap(L2);
-
- cout << "交换后: " << endl;
- printList(L1);
- printList(L2);
-
- }
-
- int main() {
-
- test01();
-
- test02();
-
- system("pause");
-
- return 0;
- }
10 20 30 40
10 20 30 40
10 20 30 40
100 100 100 100 100 100 100 100 100 100
交换前:
10 20 30 40
100 100 100 100 100 100 100 100 100 100
交换后:
100 100 100 100 100 100 100 100 100 100
10 20 30 40
list 大小操作
-
size(); //返回容器中元素的个数
-
empty(); //判断容器是否为空
-
resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
//如果容器变短,则末尾超出容器长度的元素被删除。
- #include
- #include
- using namespace std;
- #include
-
- void printList(const list<int>& L) {
-
- for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
- cout << *it << " ";
- }
- cout << endl;
- }
-
- //大小操作
- void test01()
- {
- list<int>L1;
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
-
- if (L1.empty())
- {
- cout << "L1为空" << endl;
- }
- else
- {
- cout << "L1不为空" << endl;
- cout << "L1的大小为: " << L1.size() << endl;
- }
-
- //重新指定大小
- L1.resize(10);
- printList(L1);
-
- L1.resize(2);
- printList(L1);
- }
-
- int main() {
-
- test01();
-
- system("pause");
-
- return 0;
- }
L1不为空
L1的大小为: 4
10 20 30 40 0 0 0 0 0 0
10 20
list 插入和删除
- push_back(elem);//在容器尾部加入一个元素
- pop_back();//删除容器中最后一个元素
- push_front(elem);//在容器开头插入一个元素
- pop_front();//从容器开头移除第一个元素
- insert(pos,elem);//在pos位置插elem元素的拷贝,返回新数据的位置。
- insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。
- insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。
- clear();//移除容器的所有数据
- erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置。
- erase(pos);//删除pos位置的数据,返回下一个数据的位置。
- remove(elem);//删除容器中所有与elem值匹配的元素。
- #include
- #include
- using namespace std;
- #include
-
- void printList(const list<int>& L) {
-
- for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
- cout << *it << " ";
- }
- cout << endl;
- }
-
- //插入和删除
- void test01()
- {
- list<int> L;
- //尾插
- L.push_back(10);
- L.push_back(20);
- L.push_back(30);
- //头插
- L.push_front(100);
- L.push_front(200);
- L.push_front(300);
-
- printList(L);
-
- //尾删
- L.pop_back();
- printList(L);
-
- //头删
- L.pop_front();
- printList(L);
-
- //插入
- list<int>::iterator it = L.begin();
- L.insert(++it, 1000);//链表不可跳跃访问
- printList(L);
-
- //删除
- it = L.begin();
- L.erase(++it);
- printList(L);
-
- //移除
- L.push_back(10000);
- L.push_back(10000);
- L.push_back(10000);
- printList(L);
- L.remove(10000);
- printList(L);
-
- //清空
- L.clear();
- printList(L);
- }
-
- int main() {
-
- test01();
-
- system("pause");
-
- return 0;
- }
-
300 200 100 10 20 30
300 200 100 10 20
200 100 10 20
200 1000 100 10 20
200 100 10 20
200 100 10 20 10000 10000 10000
200 100 10 20
(清空时打印空字符串)
list 数据存取
front(); //返回第一个元素。back(); //返回最后一个元素。
cout << L1.at(0) << endl;//错误 不支持at访问数据
cout << L1[0] << endl; //错误 不支持[]方式访问数据
list容器的迭代器是双向迭代器,不支持随机访问
it = it + 1;//错误,不可以跳跃访问,即使是+1
- #include
- #include
- using namespace std;
- #include
-
- //数据存取
- void test01()
- {
- list<int>L1;
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
-
-
- //cout << L1.at(0) << endl;//错误 不支持at访问数据
- //cout << L1[0] << endl; //错误 不支持[]方式访问数据
- cout << "第一个元素为: " << L1.front() << endl;
- cout << "最后一个元素为: " << L1.back() << endl;
-
- //list容器的迭代器是双向迭代器,不支持随机访问
- list<int>::iterator it = L1.begin();
- //it = it + 1;//错误,不可以跳跃访问,即使是+1
- }
-
- int main() {
-
- test01();
-
- system("pause");
-
- return 0;
- }
-
第一个元素为: 10
最后一个元素为: 40
list 反转和排序
reverse(); //反转链表sort(); //链表排序
- #include
- #include
- using namespace std;
- #include
- void printList(const list<int>& L) {
-
- for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
- cout << *it << " ";
- }
- cout << endl;
- }
-
- bool myCompare(int val1 , int val2)
- {
- return val1 > val2;
- }
-
- //反转和排序
- void test01()
- {
- list<int> L;
- L.push_back(90);
- L.push_back(30);
- L.push_back(20);
- L.push_back(70);
- printList(L);
-
- //反转容器的元素
- L.reverse();
- printList(L);
- //排序
- L.sort(); //默认的排序规则 从小到大
- printList(L);
-
- L.sort(myCompare); //指定规则,从大到小
- //L.sort(greater
());也可以 - printList(L);
- }
-
- int main() {
-
- test01();
-
- system("pause");
-
- return 0;
- }
90 30 20 70
70 20 30 90
20 30 70 90
90 70 30 20
list去重与缝合
unique 去重

去重之前是有要求的,去重之前一定要先排序!如果不排序可能会去不干净。
- #include
- #include
- using namespace std;
- #include
- #include
-
- void test11() {
- list<int> L;
- L.push_back(2);
- L.push_back(1);
- L.push_back(2);
- L.push_back(1);
- for (auto e : L) cout << e << " "; cout << endl;
-
- L.sort(); // 去重前排个序
-
- cout << "去重后:";
- L.unique();
- for (auto e : L) cout << e << " "; cout << endl;
- }
-
- int main() {
-
- test11();
-
- system("pause");
-
- return 0;
- }
-
-
输出:
2 1 2 1
去重后:1 2
splice 接合

- #include
- #include
- using namespace std;
- #include
- #include
-
- void test11() {
- list<int> L1;
- L1.push_back(1);
- L1.push_back(2);
- L1.push_back(3);
- cout << "L1: ";
- for (auto e : L1) cout << e << " "; cout << endl;
-
- list<int> L2;
- L2.push_back(10);
- L2.push_back(20);
- L2.push_back(30);
- cout << "L2: ";
- for (auto e : L2) cout << e << " "; cout << endl;
-
- list<int>::iterator pos = L1.begin();
- L1.splice(pos, L2); // 把L2的内容,接到L1的begin()前面
- cout << "接合后:";
- for (auto e : L1) cout << e << " "; cout << endl;
- }
-
- int main() {
-
- test11();
-
- system("pause");
-
- return 0;
- }
-
-
输出:
L1: 1 2 3
L2: 10 20 30
接合后:10 20 30 1 2 3
list删除导致迭代器失效问题
- template<typename T>
-
- void removeDuplicates(list
& aList) -
- {
-
- T curValue;
-
- list
::iterator cur, p; -
- cur = aList.begin();
-
- while (cur != aList.end())
-
- {
-
- curValue = *cur;
-
- //空白行 1
-
- while (p != aList.end())
-
- {
-
- if (*p == curValue)
-
- {
-
- //空白行 2
-
- }
-
- else
-
- {
-
- p++;
-
- }
-
- }
-
- }
-
- }
A. p=cur+1;aList.erase(p++);
B.p=++cur; p == cur ? cur = p = aList.erase(p) : p = aList.erase(p);
C.p=cur+1;aList.erase(p);
D.p=++cur;aList.erase(p);
分析:迭代p需要迭代不重复节点的下一节点,重要的是cur迭代器需要往下迭代,因此cur需要往前移动,二答案A C的cur都不会改变,空白行2是当需要找到重复值时进行节点删除,当删除时当前迭代器会失效,因此需要将迭代器p往后迭代,所以答案为 B
list代码实现
模拟实现list类函数接口
- namespace bite
-
- {
-
- // List的节点类
-
- template<class T>
-
- struct ListNode
-
- {
-
- ListNode(const T& val = T());
-
- ListNode
* _pPre; -
- ListNode
* _pNext; -
- T _val;
-
- };
-
-
-
- //List的迭代器类
-
- template<class T, class Ref, class Ptr>
-
- class ListIterator
-
- {
-
- typedef ListNode
* PNode; -
- typedef ListIterator
Self; -
- public:
-
- ListIterator(PNode pNode = nullptr);
-
- ListIterator(const Self& l);
-
- T& operator*();
-
- T* operator->();
-
- Self& operator++();
-
- Self operator++(int);
-
- Self& operator--();
-
- Self& operator--(int);
-
- bool operator!=(const Self& l);
-
- bool operator==(const Self& l);
-
- private:
-
- PNode _pNode;
-
- };
-
-
-
- //list类
-
- template<class T>
-
- class list
-
- {
-
- typedef ListNode
Node; -
- typedef Node* PNode;
-
- public:
-
- typedef ListIterator
iterator; -
- typedef ListIterator
const T&, const T&> const_iterator; -
- public:
-
- ///
-
- // List的构造
-
- list();
-
- list(int n, const T& value = T());
-
- template <class Iterator>
-
- list(Iterator first, Iterator last);
-
- list(const list
& l); -
- list
& operator=(const list l); -
- ~list();
-
-
-
- ///
-
- // List Iterator
-
- iterator begin();
-
- iterator end();
-
- const_iterator begin();
-
- const_iterator end();
-
-
-
- ///
-
- // List Capacity
-
- size_t size()const;
-
- bool empty()const;
-
-
-
-
-
- // List Access
-
- T& front();
-
- const T& front()const;
-
- T& back();
-
- const T& back()const;
-
-
-
-
-
- // List Modify
-
- void push_back(const T& val){insert(begin(), 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);
-
- // 删除pos位置的节点,返回该节点的下一个位置
-
- iterator erase(iterator pos);
-
- void clear();
-
- void swap(List
& l) ; -
- private:
-
- void CreateHead();
-
- PNode _pHead;
-
- };
-
- };
list.h
- #define _CRT_SECURE_NO_WARNINGS 1
- #include
- #include
- using namespace std;
-
- namespace chaos
- {
- /* 定义结点 */
- template<class T>
- struct ListNode {
- T _data; // 用来存放结点的数据
- ListNode
* _next; // 指向后继结点的指针 - ListNode
* _prev; // 指向前驱结点的指针 -
- ListNode(const T& data = T()) // 全缺省构造(初始化)
- : _data(data)
- , _next(nullptr)
- , _prev(nullptr)
- {}
- };
-
- /* 定义迭代器 */
- template<class T, class Ref, class Ptr>
- struct __list_iterator {
- typedef ListNode
Node; - typedef __list_iterator
self; // 为了方便我们重命名为self -
- typedef Ref reference;
- typedef Ptr pointer;
-
- Node* _node;
-
- __list_iterator(Node* x)
- : _node(x)
- {}
-
- /* 解引用 */
- Ref operator*() {
- return _node->_data; // 返回结点的数据
- }
- Ptr operator->() {
- return &_node->_data;
- }
-
- /* ++it */
- self& operator++() {
- _node = _node->_next; // 让 _node 指向下一个结点
- return *this; // 返回加加后的值
- }
- /* it++ */
- self operator++(int) {
- self tmp(*this); // 拷贝构造一个tmp存储原来的值
- _node = _node->_next;
- return tmp;
- }
- /* != */
- bool operator!=(const self& it) {
- return _node != it._node; // 它们结点的指针不一样吗?T or F
- }
-
- /* --it */
- self& operator--() {
- _node = _node->_prev;
- return *this;
- }
- /* it-- */
- self operator--(int) {
- self tmp(*this);
- _node = _node->_prev;
- return tmp;
- }
- };
-
- ///* 定义const迭代器 */
- //template
- //struct __const_list_iterator {
- // typedef ListNode
Node; - // Node* _node;
-
- // __const_list_iterator(Node* x)
- // : _node(x)
- // {}
- // /* 解引用 */
- // const T& operator*() {
- // return _node->_data; // 返回结点的数据
- // }
- // /* ++it */
- // __const_list_iterator
& operator++() { - // _node = _node->_next; // 让 _node 指向下一个结点
- // return *this; // 返回加加后的值
- // }
- // /* it++ */
- // __const_list_iterator
operator++(int) { - // __const_list_iterator
tmp(*this); // 拷贝构造一个tmp存储原来的值 - // _node = _node->_next;
- // return tmp;
- // }
- // /* != */
- // bool operator!=(const __const_list_iterator
& it) { - // return _node != it._node; // 它们结点的指针不一样吗?T or F
- // }
- // /* --it */
- // __const_list_iterator
& operator--() { - // _node = _node->_prev;
- // return *this;
- // }
- // /* it-- */
- // __const_list_iterator
operator--(int) { - // __list_iterator
tmp(*this); - // _node = _node->_prev;
- // return tmp;
- // }
- //};
-
- /* 定义链表 */
- template<class T>
- class list {
- typedef ListNode
Node; // 重命名为Node - public:
- /* 迭代器 */
- typedef __list_iterator
iterator; - typedef __list_iterator
const T&, const T*> const_iterator; - iterator begin() {
- return iterator(_pHead->_next);
- }
- iterator end() {
- return iterator(_pHead);
- }
- const_iterator begin() const {
- return const_iterator(_pHead->_next);
- }
- const_iterator end() const {
- return const_iterator(_pHead);
- }
-
- /* 构造函数:初始化头结点 */
- list() {
- _pHead = new Node();
- _pHead->_next = _pHead;
- _pHead->_prev = _pHead;
- }
- list(size_t n, const T& val = T()) { // 初始化n个结点
- _pHead = new Node();
- _pHead->_next = _pHead;
- _pHead->_prev = _pHead;
- for (size_t i = 0; i < n; i++) {
- push_back(val);
- }
- }
- list(int n, const T& val = T()) { // 初始化n个结点
- _pHead = new Node();
- _pHead->_next = _pHead;
- _pHead->_prev = _pHead;
- for (int i = 0; i < n; i++) {
- push_back(val);
- }
- }
-
- template<class InputIterator>
- list(InputIterator first, InputIterator last) {
- _pHead = new Node();
- _pHead->_next = _pHead;
- _pHead->_prev = _pHead;
-
- while (first != last) {
- push_back(*first);
- first++;
- }
- }
- /* 拷贝构造(现代写法):L2(L1) */
- list(const list
& L) { - _pHead = new Node();
- _pHead->_next = _pHead;
- _pHead->_prev = _pHead;
-
- list
tmp(L.begin(), L.end()) ; - swap(_pHead, tmp._pHead);
- }
-
- /* 拷贝构造:L2(L1) */
- /*list(const list
& L) { - _pHead = new Node();
- _pHead->_next = _pHead;
- _pHead->_prev = _pHead;
- for (auto e : L) {
- push_back(e);
- }
- }*/
-
- ///* 赋值:L2 = L1 */
- //list
& operator=(list L) { - // if (this != &L) {
- // clear();
- // for (auto e : L) {
- // push_back(e);
- // }
- // }
- // return *this;
- //}
-
-
- /* 赋值(现代写法):L2 = L1 */
- list
& operator=(list L) { - swap(_pHead, L._pHead);
- return *this;
- }
-
-
- /* 在pos位置前插入 */
- void insert(iterator pos, const T& x) {
- Node* cur = pos._node; // 找到pos位置的结点
- Node* cur_prev = cur->_prev; // 因为要在pos位置前插入,所以要找到当前pos位置的前一个结点
- Node* new_node = new Node(x); // 创建新节点
-
- // 缝合: cur_prev <-> new_node <-> cur
- cur_prev->_next = new_node;
- new_node->_prev = cur_prev;
- new_node->_next = cur;
- cur->_prev = new_node;
- }
-
- /* 尾插:push_back */
- void push_back(const T& x) {
- //Node* pTail = _pHead->_prev; // pHead的前驱就是pTail
- //Node* new_node = new Node(x); // 创建新结点(会调用构造,自动创建)
- //
- //pTail->_next = new_node;
- //new_node->_prev = pTail;
- //new_node->_next = _pHead;
- //_pHead->_prev = new_node;
-
- insert(end(), x); // 在end(头结点)前插入,即尾插
- }
-
- /* 头插:push_front */
- void push_front(const T& x) {
- insert(begin(), x); // 在begin(头结点的下一个结点)前插入,即头插
- }
-
-
- /* 任意位置删除 */
- iterator erase(iterator pos) {
- assert(pos != end()); // 防止头结点被删除
-
- Node* cur = pos._node; // 找到pos位置的结点
- Node* cur_prev = cur->_prev; // 找到pos的前驱
- Node* cur_next = cur->_next; // 找到pos的后继
-
- // 删除cur
- delete cur;
-
- // 缝合: cur_prev <-> cur(删) <-> cur_next
- cur_prev->_next = cur_next;
- cur_next->_prev = cur_prev;
-
- return iterator(cur_next);
- }
-
- /* 尾删 */
- void pop_back() {
- erase(--end()); // 删除最后一个元素,即尾结点
- }
-
- /* 头删 */
- void pop_front() {
- erase(begin()); // 删除头结点的下一个结点(即begin位置的结点)
- }
-
- /* 清空链表所有数据 */
- void clear() {
- 利用迭代器去遍历整个链表
- //iterator it = begin();
- //while (it != end()) {
- // iterator del = it++; // 巧妙利用后置++的特性
- // delete del._node; // 删除当前结点,后置++生效
- //}
-
- 删完之后我们还需要将其恢复到初始状态
- //_pHead->_next = _pHead;
- //_pHead->_prev = _pHead;
-
- iterator it = begin();
- while (it != end()) {
- // iterator del = it++;
- erase(it++); // 直接反复调用erase删除结点
- }
- }
-
- ~list() {
- // 清空链表有效数据
- clear();
- // 干掉头结点
- delete _pHead;
- _pHead = nullptr;
- }
-
- private:
- Node* _pHead;
- };
-
- void print_list(const list<int>& L) {
- list<int>::const_iterator it = L.begin();
- while (it != L.end()) {
- // *it = 100;
- cout << *it << " ";
- it++;
- }
- cout << endl;
- }
-
-
- void test_list1() {
- list<int> L;
- L.push_back(1);
- L.push_back(2);
- L.push_back(3);
- L.push_back(4);
-
- list<int>::iterator it = L.begin();
- while (it != L.end()) {
- *it *= 2;
- cout << *it << " ";
- it++;
- }
- cout << endl;
- }
-
- void test_list2() {
- list<int> L;
- L.push_back(2);
- L.push_back(4);
- L.push_back(6);
- L.push_back(8);
-
- print_list(L);
- }
-
-
- struct Date {
- int _year;
- int _month;
- int _day;
-
- Date(int year = 1, int month = 1, int day = 1)
- : _year(year)
- , _month(month)
- , _day(day)
- {}
- };
-
- void test_list3() {
- list
L; - L.push_back(Date(2022, 5, 1));
- L.push_back(Date(2022, 5, 2));
- L.push_back(Date(2022, 5, 3));
-
- list
::iterator it = L.begin(); - while (it != L.end()) {
- cout << it->_year << "/" << it->_month << "/" << it->_day << endl;
- it++;
- }
- cout << endl;
- }
-
- void test_list4() {
- list<int> L1;
- L1.push_back(1);
- L1.push_back(2);
- L1.push_back(3);
-
- list<int> L2(L1);
- for (auto e : L2) cout << e << " ";
- }
-
- void test_list5() {
- list<int> L;
- L.push_back(1);
- L.push_back(2);
- L.push_back(3);
- cout << "删除前:";
- print_list(L);
-
- cout << "删除后:";
- L.clear();
- print_list(L);
- }
- }
-
-
- -------------------------------------------------------------------
- namespace bite
-
- {
-
- // List的节点类
-
- template<class T>
-
- struct ListNode
-
- {
-
- ListNode(const T& val = T()): _pPre(nullptr), _pNext(nullptr), _val(val)
-
- {}
-
- ListNode
* _pPre; -
- ListNode
* _pNext; -
- T _val;
-
- };
-
-
-
- //List的迭代器类
-
- template<class T, class Ref, class Ptr>
-
- class ListIterator
-
- {
-
- typedef ListNode
* PNode; -
- typedef ListIterator
Self; -
- public:
-
- ListIterator(PNode pNode = nullptr):_pNode(pNode)
-
- {}
-
- ListIterator(const Self& l): _pNode(l._pNode)
-
- {}
-
- T& operator*()
-
- {
-
- return _pNode->_val;
-
- }
-
- T* operator->()
-
- {
-
- return &*this;
-
- }
-
- Self& operator++()
-
- {
-
- _pNode = _pNode->_pNext;
-
- return *this;
-
- }
-
- Self operator++(int)
-
- {
-
- Self temp(*this);
-
- _pNode = _pNode->_pNext;
-
- return temp;
-
- }
-
- Self& operator--()
-
- {
-
- _pNode = _pNode->_pPre;
-
- return *this;
-
- }
-
- Self& operator--(int)
-
- {
-
- Self temp(*this);
-
- _pNode = _pNode->_pPre;
-
- return temp;
-
- }
-
- bool operator!=(const Self& l)
-
- {
-
- return _pNode != l._pNode;
-
- }
-
- bool operator==(const Self& l)
-
- {
-
- return !(*this!=l);
-
- }
-
- private:
-
- PNode _pNode;
-
- };
-
-
-
- //list类
-
- template<class T>
-
- class list
-
- {
-
- typedef ListNode
Node; -
- typedef Node* PNode;
-
- public:
-
- typedef ListIterator
iterator; -
- typedef ListIterator
const T&, const T&> const_iterator; -
- public:
-
- ///
-
- // List的构造
-
- list()
-
- {
-
- CreateHead();
-
- }
-
- list(int n, const T& value = T())
-
- {
-
- CreateHead();
-
- for (int i = 0; i < n; ++i)
-
- push_back(value);
-
- }
-
- template <class Iterator>
-
- list(Iterator first, Iterator last)
-
- {
-
- CreateHead();
-
- while (first != last)
-
- {
-
- push_back(*first);
-
- ++first;
-
- }
-
- }
-
- list(const list
& l) -
- {
-
- CreateHead();
-
- // 用l中的元素构造临时的temp,然后与当前对象交换
-
- list
temp(l.cbegin(), l.cend()) ; -
- this->swap(temp);
-
- }
-
- list
& operator=(const list l) -
- {
-
- this->swap(l);
-
- return *this;
-
- }
-
- ~list()
-
- {
-
- clear();
-
- delete _pHead;
-
- _pHead = nullptr;
-
- }
-
-
-
- ///
-
- // List Iterator
-
- iterator begin()
-
- {
-
- return iterator(_pHead->_pNext);
-
- }
-
- iterator end()
-
- {
-
- return iterator(_pHead);
-
- }
-
- const_iterator begin()const
-
- {
-
- return const_iterator(_pHead->_pNext);
-
- }
-
- const_iterator end()const
-
- {
-
- return const_iterator(_pHead);
-
- }
-
-
-
- ///
-
- // List Capacity
-
- size_t size()const
-
- {
-
- size_t size = 0;
-
- ListNode *p = _pHead->_pNext;
-
- while(p != _pHead)
-
- {
-
- size++;
-
- p = p->_pNext;
-
- }
-
- 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(begin(), 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 pNewNode = new Node(val);
-
- PNode pCur = pos._pNode;
-
- // 先将新节点插入
-
- pNewNode->_pPre = pCur->_pPre;
-
- pNewNode->_pNext = pCur;
-
- pNewNode->_pPre->_pNext = pNewNode;
-
- pCur->_pPre = pNewNode;
-
- return iterator(pNewNode);
-
- }
-
- // 删除pos位置的节点,返回该节点的下一个位置
-
- iterator erase(iterator pos)
-
- {
-
- // 找到待删除的节点
-
- PNode pDel = pos._pNode;
-
- PNode pRet = pDel->_pNext;
-
- // 将该节点从链表中拆下来并删除
-
- pDel->_pPre->_pNext = pDel->_pNext;
-
- pDel->_pNext->_pPre = pDel->_pPre;
-
- delete pDel;
-
- return iterator(pRet);
-
- }
-
- void clear()
-
- {
-
- iterator p = begin();
-
- while(p != end())
-
- {
-
- p = erase(p);
-
- }
-
- }
-
- void swap(List
& l) -
- {
-
- pNode tmp = _pHead;
-
- _pHead = l._pHead;
-
- l._pHead = tmp;
-
- }
-
- private:
-
- void CreateHead()
-
- {
-
- _pHead = new Node;
-
- _pHead->_pPre = _pHead;
-
- _pHead->_pNext = _pHead;
-
- }
-
- PNode _pHead;
-
- };
-
- }
test.cpp
- #define _CRT_SECURE_NO_WARNINGS 1
- #include "list.h"
-
- int main(void)
- {
- chaos::test_list1();
-
- return 0;
- }
reverse_iterator.h
- #define _CRT_SECURE_NO_WARNINGS 1
- #include "list.h"
-
- namespace chaos
- {
- // Iterator是哪个容器的迭代器,reverse_iterator
就可以 - // 适配出哪个容器的反向迭代器。复用的体现
- //template
-
- /* 定义反向迭代器 */
- template <class Iterator>
- class reverse_iterator {
- typedef reverse_iterator
self; - typedef typename Iterator::Ref reference;
- typedef typename Iterator::Ptr pointer;
- public:
- reverse_iterator(Iterator it)
- :_it(it)
- {}
-
- Ref operator*() {
- //return *_it;
- Iterator prev = _it;
- return *--prev;
- }
-
- Ptr operator->() {
- return &operator*();
- }
-
- self& operator++() {
- --_it;
- return *this;
- }
-
- self& operator--() {
- ++_it;
- return *this;
- }
-
- bool operator!= (const self& rit) const {
- return _it != rit._it;
- }
-
- private:
- Iterator _it;
- };
- }
-