目录
(一)基本概念
- 功能:将数据进行链式存储
- 链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的
- STL中的链表是一个双向循环链表
- 链表的优点:采用动态存储分配,不会造成内存空间浪费和溢出;可以对任意位置进行快速插入和删除元素,不需要移动大量元素
- 链表的缺点:容器遍历速度没有数组快;占用空间比数组大
- 注意:list的插入和删除操作都不会造成原有list迭代器的失效
- 由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器
(二)常使用操作
1. 构造
list<T>L;//默认构造
list<T>L1(L.begin(), L.end());//区间构造
list<T>L2(L);//拷贝构造
list<T>L3(int n, T elem);//将n个T类型的数据elem赋值给L3
#include<iostream>
#include<list>
using namespace std;
void print(const list<int>L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void text()
{
list<int>L1;//默认构造
for (int i = 0; i < 10; i++)
{
int x = rand() % 100 + 1;
L1.push_back(x);//尾插,添加数据
}
print(L1);//输出42 68 35 1 70 25 79 59 63 65
//区间构造
list<int>L2(L1.begin(), L1.end());
print(L2);//输出42 68 35 1 70 25 79 59 63 65
//拷贝构造
list<int>L3(L1);
print(L3);//输出42 68 35 1 70 25 79 59 63 65
//n个elem
list<int>L4(5, 11);//输出11 11 11 11 11
print(L4);
}
int main()
{
text();
return 0;
}
2. 赋值和交换
list<T>L;
list<T>L1 = L;//等号赋值
list<T>L2.assign(L.begin(), L.end());//区间赋值
list<T>L3.assign(int n, T elem);//将n个T类型的数据elem赋值给L3
L4.swap(L);//容器L4和L中的数据互换
#include<iostream>
#include<list>
using namespace std;
void print(const list<int>L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void text()
{
list<int>L1;//默认构造
for (int i = 0; i < 10; i++)
{
int x = rand() % 200 + 1;
L1.push_back(x);//尾插,添加数据
}
print(L1);//输出42 68 135 101 170 125 79 159 163 65
//赋值
list<int>L2;
L2 = L1;//等号赋值
print(L2);//输出42 68 135 101 170 125 79 159 163 65
list<int>L3;
L3.assign(L1.begin(), L1.end());//区间赋值
print(L3);//输出42 68 135 101 170 125 79 159 163 65
list<int>L4;
L4.assign(5,76);//n个elem赋值
print(L4);//输出 76 76 76 76 76
//交换
cout << "L1容器交换前:";
print(L1);//输出42 68 135 101 170 125 79 159 163 65
cout << "L4容器交换前:";
print(L4);//输出 76 76 76 76 76
L1.swap(L4);
cout << "L1容器交换后:";
print(L1);//输出 76 76 76 76 76
cout << "L4容器交换后:";
print(L4);//输出42 68 135 101 170 125 79 159 163 65
}
int main()
{
text();
return 0;
}
3. 大小操作
list<T>L;
list<T>L1.empty();//判空
list<T>L2.size();//返回元素个数
list<T>L31.resize(int n);//指定元素个数为n个
#include<iostream>
#include<list>
using namespace std;
void print(const list<int>L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void text()
{
list<int>L1;//默认构造
for (int i = 0; i < 10; i++)
{
int x = rand() % 300 + 1;
L1.push_back(x);//尾插,添加数据
}
print(L1);//输出42 168 35 101 270 125 79 259 263 165
if (L1.empty())//判空
{
cout << "L1为空!" << endl;
}
else
{
cout << "L1不为空!" ;
cout << "L1中元素个数为:" << L1.size() << endl;
}
//重新指定大小
L1.resize(20);//指定大小比原来长,剩下部分默认用0填充
//L1.resize(20,11);指定大小比原来长,剩下部分用11填充
print(L1);//42 168 35 101 270 125 79 259 263 165 0 0 0 0 0 0 0 0 0 0
L1.resize(5); //指定大小比原来短,删除超出部分的元素
print(L1);//42 168 35 101 270
}
int main()
{
text();
return 0;
}
4. 插入和删除
list<T>L;
list<T>L1.push_back(T elem);//尾插
list<T>L2.push_front(T elem);//头插
list<T>L3.pop_back(T elem);//尾删
list<T>L4.pop_front(T elem);//头删
list<T>L5.insert(pos,T elem);//在pos位置插入elem,返回新数据的位置
list<T>L6.insert(pos, int n, T elem);//在pos位置插入n个T类型是elem数据,无返回值
list<T>L7.insert(pos, beg, end);//在pos位置插入[beg,end]区间的元素,无返回值
list<T>L8.erase(beg, end);//删除[beg,end]区间的元素,返回下一个数据的位置
list<T>L8.erase(pos);//删除pos位置的数据,返回下一个数据的位置
list<T>L9.remove(elem);//删除容器中所有与elem匹配的数据
list<T>L10.clear();//移除容器中所有数据
#include<iostream>
#include<list>
using namespace std;
void print(const list<int>L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void text()
{
list<int>L1;//默认构造
L1.push_back(11);//尾插
L1.push_back(12);
L1.push_back(13);
L1.push_back(14);
print(L1);//输出11 12 13 14
L1.push_front(34);//头插
L1.push_front(67);
L1.push_front(89);//输出89 67 34 11 12 13 14
print(L1);
L1.pop_back();//尾删
print(L1);//输出89 67 34 11 12 13
L1.pop_front();//头删
print(L1);//输出67 34 11 12 13
L1.insert(L1.begin(), 123);//头部插入123
print(L1);//输出123 67 34 11 12 13
//任意插入
list<int>::iterator it = L1.begin();
L1.insert(++it, 345);//在下标为1的位置插入345
print(L1);//输出123 345 67 34 11 12 13
//任意删除
it = L1.begin();
L1.erase(it);
print(L1);//输出345 67 34 11 12 13
//移除
L1.remove(11);//移除11
print(L1);//输出345 67 34 12 13
L1.push_back(34);
L1.push_back(34);
print(L1);//输出345 67 34 12 13 34 34
L1.remove(34);
print(L1);//输出345 67 12 13(将34全部移除了)
}
int main()
{
text();
return 0;
}
5. 数据的存取
list<T>L;
L.front();//访问容器L中的第一个元素
L.back();//访问容重的最后一个元素
- 注意:L[0]是错误的,不可以用[]访问list容器中的元素;L.at(0)也是错误的,不可以用at方式访问list容器中的元素
- 原因:是list本质是链表,不是用连续线性空间存储数据,迭代器也是不支持随机访问的
#include<iostream>
#include<list>
using namespace std;
void print(const list<int>L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void text()
{
list<int>L1;//默认构造
L1.push_back(11);//尾插
L1.push_back(12);
L1.push_back(13);
L1.push_back(14);
print(L1);//输出11 12 13 14
cout << "L1中的元素个数:" << L1.size() << endl;//4
cout << "第一个元素是:" << L1.front() << endl;//11
cout << "L1中的元素个数是:" << L1.size() << endl;//4
cout << "最后一个元素是:" << L1.back() << endl;//14
}
int main()
{
text();
return 0;
}
6. 反转和排序
list<T>L;
L.reverse();//L容器中的数据反转
L.sort();//实现L中的数据升序排列
- 注意:所有不支持随机访问迭代器的容器,不可以用标准算法;不支持随机访问迭代器的容器,内部会提供对应的一些算法
#include<iostream>
#include<list>
#include<stdbool.h>
using namespace std;
void print(const list<int>L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
bool Compare(int a,int b)
{
return a > b ? true : false;//a大于b,返回true
}
void text()
{
list<int>L1;//默认构造
for (int i = 0; i < 10; i++)
{
int x = rand() % 100 + 1;//产生随机数
L1.push_back(x);
}
cout << "反转前:";
print(L1);//42 68 35 1 70 25 79 59 63 65
cout << "反转后:";
L1.reverse();
print(L1);//65 63 59 79 25 70 1 35 68 42
//排序
L1.sort();
//错误写法:sort(L1.begin(),L1.end())
cout << "升序:";
print(L1);//1 25 35 42 59 63 65 68 70 79
L1.sort(Compare);
cout << "降序:";
print(L1);//79 70 68 65 63 59 42 35 25 1
}
int main()
{
text();
return 0;
}
7. 清空
clear用于清空容器,清空后容器的size为0
#include<iostream>
#include<list>
using namespace std;
int main()
{
list<int>lt(6, 1);
cout << "开始的size:" << lt.size()<<endl;
for (auto ch : lt)
{
cout << ch << " ";
}
cout << endl;
lt.clear();
cout << "清理后size:" << lt.size() << endl;
return 0;
}
8. 拼接(splice)
void splice (iterator position, list& x);//将整个容器x拼接到另外一个容器的position位置
void splice (iterator position, list& x, iterator i);
//将容器x的第i个迭代器所指的数据拼接到另外一个容器的position位置
void splice (iterator position, list& x, iterator first, iterator last);
//将容器x的指定区间拼接到另外一个容器的position位置
8.1 情况1
#include<iostream>
#include<list>
using namespace std;
void print(const list<char>L)
{
for (list<char>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
list<char>lt1(5, 'a');
list<char>lt2(5, 'c');
cout << "拼接前:" << endl;
cout << "lt1:" << endl;
print(lt1);
cout << "lt2:" << endl;
print(lt2);
lt1.splice(lt1.begin(), lt2);//将容器lt2拼接到容器lt1的开头
cout << "拼接后:" << endl;
cout << "lt1:" << endl;
print(lt1);
cout << "lt2:" << endl;
print(lt2);
return 0;
}
8.2 情况2
#include<iostream>
#include<list>
using namespace std;
void print(const list<char>L)
{
for (list<char>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
list<char>lt1(5, 'a');
list<char>lt2;
lt2.push_back('c');
lt2.push_back('d');
lt2.push_back('e');
lt2.push_back('f');
lt2.push_back('g');
cout << "拼接前:" << endl;
cout << "lt1:" << endl;
print(lt1);
cout << "lt2:" << endl;
print(lt2);
lt1.splice(lt1.begin(), lt2,++lt2.begin());//将容器lt2的第2个数据拼接到容器lt1的开头
cout << "拼接后:" << endl;
cout << "lt1:" << endl;
print(lt1);
cout << "lt2:" << endl;
print(lt2);
return 0;
}
8.3 情况3
#include<iostream>
#include<list>
using namespace std;
void print(const list<char>L)
{
for (list<char>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
list<char>lt1(5, 'a');
list<char>lt2;
lt2.push_back('c');
lt2.push_back('d');
lt2.push_back('e');
lt2.push_back('f');
lt2.push_back('g');
cout << "拼接前:" << endl;
cout << "lt1:" << endl;
print(lt1);
cout << "lt2:" << endl;
print(lt2);
lt1.splice(lt1.begin(), lt2,lt2.begin(),lt2.end());//将容器lt2的指定区间拼接到容器lt1的开头
cout << "拼接后:" << endl;
cout << "lt1:" << endl;
print(lt1);
cout << "lt2:" << endl;
print(lt2);
return 0;
}
(三)结点模拟实现
template <class T>
struct list_node//定义一个结点类型
{
T _data;
list_node<T>* _prev;//指针将指向一个具有模板参数类型 T 的 list_node 节点
list_node<T>* _next;
list_node(const T& x = T())//用于初始化list_node节点
:_data(x)
, _prev(nullptr)
, _next(nullptr)
{
;
}
};
(四)迭代器模拟实现
- 迭代器的核心是节点的指针
1. 普通迭代器
//模拟实现链表的迭代器
//迭代器的核心是节点的指针
template<class T>
struct __list_iterator
{
typedef list_node<T> Node;
typedef __list_iterator<T> self;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{
;
}
self& operator++()//重载前置++,迭代器++
{
_node = _node->_next;
return *this;
}
self operator++(int)//后置++
{
Node* tmp = _node;
_node = _node->_next;
return tmp;
}
self& operator--()//前置--
{
_node = _node->_prev;
return *this;
}
self operator--(int)//后置--
{
self tmp = this->_node;
this->_node = this->_node->_prev;
return tmp;
}
T& operator*()//支持修改
{
return _node->_data;
}
T* operator->()
{
return &_node->_data;//返回数据的地址
}
bool operator!=(const self& s)//迭代器进行比较
{
return _node != s._node;
}
bool operator==(const self& s)
{
return _node == s._node;
}
};
2. const迭代器
迭代器指向的内容不能修改,而不是迭代器本身不能修改!!!
//const迭代器
//迭代器指向的内容不能修改!!!
template<class T>
struct __list_const_iterator
{
typedef list_node<T> Node;
typedef __list_const_iterator<T> self;
Node* _node;
__list_const_iterator(Node* node)
:_node(node)
{
;
}
self& operator++()//重载前置++,迭代器++
{
_node = _node->_next;
return *this;
}
self operator++(int)//后置++
{
Node* tmp = _node;
_node = _node->_next;
return tmp;
}
self& operator--()//前置--
{
_node = _node->_prev;
return *this;
}
self operator--(int)//后置--
{
self tmp = this->_node;
this->_node = this->_node->_prev;
return tmp;
}
const T& operator*()//支持修改
{
return _node->_data;
}
const T* operator->()
{
return &_node->_data;//返回数据的地址
}
bool operator!=(const self& s)//迭代器进行比较
{
return _node != s._node;
}
bool operator==(const self& s)
{
return _node == s._node;
}
};
3. 链表
typedef __list_iterator<T> iterator;
typedef __list_const_iterator<T> const_iterator;
(五)改进迭代器实现
// T T& T*
// T cosnt T& const T*
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)
{
;
}
self& operator++()//重载前置++,迭代器++
{
_node = _node->_next;
return *this;
}
self operator++(int)//后置++
{
Node* tmp = _node;
_node = _node->_next;
return tmp;
}
self& operator--()//前置--
{
_node = _node->_prev;
return *this;
}
self operator--(int)//后置--
{
self tmp = this->_node;
this->_node = this->_node->_prev;
return tmp;
}
Ref& operator*()//支持修改
{
return _node->_data;
}
Ptr* operator->()
{
return &_node->_data;//返回数据的地址
}
bool operator!=(const self& s)//迭代器进行比较
{
return _node != s._node;
}
bool operator==(const self& s)
{
return _node == s._node;
}
};
(六)链表模拟实现
1. typedef
typedef list_node<T> Node;
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
2. 初始化函数和成员函数
2.1初始化函数
void empty_init()
{
_head = new Node;
_head->_prev = _head;
_head->_next = _head;
_size = 0;
}
2.2构造函数
list()
{
empty_init();
}
2.3析构函数
~list()
{
clear();
delete _head;//释放头结点
_head = nullptr;
}
2.4拷贝构造
//l1(lt)
list(list<T>& lt)
{
this->empty_init();
for (auto ch : lt)
{
this->push_back(ch);
}
}
2.5赋值重载
//赋值重载
//原始写法:
list<T>operator=(list<T>& lt)
{
if (this != <)//防止自己给自己赋值
{
clear();
for (auto ch : lt)
{
push_back(ch);
}
}
return *this;
}
void swap(list<T>& lt)
{
std::swap(_head, lt._head);
std::swap(_size, lt._size);
}
list<T>operator=(list<T> lt)
{
swap(lt);
return *this;
}
3. 常见操作
3.1. 尾插
void push_back(const T& x)//尾插
{
Node* tail = _head->_prev;
Node* newnode = new Node(x);
//连接
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;
_size++;
}
3.2 任意位置插
iterator insert(iterator pos, const T& x)
{
Node* newnode = new Node(x);
Node* cur = pos._node;
Node* prev = cur->_prev;
//连接
//prev newnode cur
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
++_size;
return iterator(newnode);
}
3.3 头插
void push_front(const T& x)
{
insert(begin(), x);
}
3.4 删除
iterator erase(iterator pos)//删除
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
delete cur;
//prev cur next
prev->_next = next;
next->_prev = prev;
--_size;
return iterator(next);//因为删除操作会使得pos失效,所以返回下一个位置
}
3.5 头删尾删
void pop_front()//头删
{
erase(begin());
}
void pop_back()
{
erase(--end());
}
3.6清理
void clear()//清理
{
iterator it = begin();
while (it != end())
{
it = erase(it);
}
}
(七)完整代码
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace Imitate_list
{
template <class T>
struct list_node//定义一个结点类型
{
T _data;
list_node<T>* _prev;//指针将指向一个具有模板参数类型 T 的 list_node 节点
list_node<T>* _next;
list_node(const T& x = T())//用于初始化list_node节点
:_data(x)
, _prev(nullptr)
, _next(nullptr)
{
;
}
};
// T T& T*
// T cosnt T& const T*
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)
{ ; }
self& operator++()//重载前置++,迭代器++
{
_node = _node->_next;
return *this;
}
self operator++(int)//后置++
{
Node* tmp = _node;
_node = _node->_next;
return tmp;
}
self& operator--()//前置--
{
_node = _node->_prev;
return *this;
}
self operator--(int)//后置--
{
self tmp = this->_node;
this->_node = this->_node->_prev;
return tmp;
}
Ref& operator*()//支持修改
{
return _node->_data;
}
Ptr* operator->()
{
return &_node->_data;//返回数据的地址
}
bool operator!=(const self& s)//迭代器进行比较
{
return _node != s._node;
}
bool operator==(const self& s)
{
return _node == s._node;
}
};
template <class T>
class list
{
public:
typedef list_node<T> Node;
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&,const T*> const_iterator;
iterator begin()
{
return iterator(_head->_next);
//等价形式 return _head->_next;
}
iterator end()
{
return iterator(_head);
//等价形式 return _head;
}
const_iterator begin() const
{
return const_iterator(_head->_next);
//等价形式 return _head->_next;
}
const_iterator end() const
{
return const_iterator(_head);
}
void empty_init()
{
_head = new Node;
_head->_prev = _head;
_head->_next = _head;
_size = 0;
}
list()
{
empty_init();
}
~list()
{
clear();
delete _head;//释放头结点
_head = nullptr;
}
//l1(lt)
list(list<T>& lt)
{
this->empty_init();
for (auto ch : lt)
{
this->push_back(ch);
}
}
赋值重载
原始写法:
//list<T>operator=(list<T><)
//{
// if (this != <)//防止自己给自己赋值
// {
// clear();
// for (auto ch : lt)
// {
// push_back(ch);
// }
// }
// return *this;
//}
void swap(list<T>& lt)
{
std::swap(_head, lt._head);
std::swap(_size, lt._size);
}
list<T>operator=(list<T> lt)
{
swap(lt);
return *this;
}
void push_back(const T& x)//尾插
{
Node* tail = _head->_prev;
Node* newnode = new Node(x);
//连接
tail->_next = newnode;
newnode->_prev = tail;
newnode->_next = _head;
_head->_prev = newnode;
_size++;
}
iterator insert(iterator pos,const T& x)
{
Node* newnode = new Node(x);
Node* cur = pos._node;
Node* prev = cur->_prev;
//连接
//prev newnode cur
prev->_next = newnode;
newnode->_prev = prev;
newnode->_next = cur;
cur->_prev = newnode;
++_size;
return iterator(newnode);
}
//void push_back(const T& x)
//{
// insert(end(), x);
//}
void push_front(const T& x)
{
insert(begin(), x);
}
iterator erase(iterator pos)//删除
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
delete cur;
//prev cur next
prev->_next = next;
next->_prev = prev;
--_size;
return iterator(next);//因为删除操作会使得pos失效,所以返回下一个位置
}
void pop_front()//头删
{
erase(begin());
}
void pop_back()
{
erase(--end());
}
void clear()//清理
{
iterator it = begin();
while (it!= end())
{
it = erase(it);
}
}
size_t size() const
{
/*size_t n = 0;
const_iterator it = begin();//如果用const_iterator迭代器,对象this指针也应该是const类型
while (it != end())
{
n++;
++it;
}
return n;*/
return _size;
}
private:
Node* _head;
size_t _size;
};
void print(const list<int>& lt)
{
list<int>::const_iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
void test1()
{
list<int>l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
l1.push_back(5);
l1.push_back(6);
cout << "尾插数据后:" << endl;
print(l1);
cout << "数据个数:" << l1.size() << endl;
cout << "insert之前:" << endl;
print(l1);
//测试任意位置插入
l1.insert(l1.begin(), 9);
cout << "insert之前:" << endl;
print(l1);
//删除
l1.erase(--l1.end());
l1.erase(--l1.end());
l1.erase(--l1.end());
cout << "erase之后:" << endl;
print(l1);
cout << "数据个数:" << l1.size() << endl;
//头插
l1.push_front(60);
cout << "头插60后:" << endl;
print(l1);
cout << "数据个数:" << l1.size() << endl;
l1.pop_front();
cout << "头删后:" << endl;
print(l1);
cout << "数据个数:" << l1.size() << endl;
l1.pop_back();
cout << "尾删后:" << endl;
print(l1);
cout << "数据个数:" << l1.size() << endl;
l1.clear();
cout << "clear后:" << endl;
cout << "数据个数:" << l1.size() << endl;
}
void test2()
{
list<int>l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
print(l1);
list<int>l2(l1);
cout << "拷贝构造l2:" << endl;
print(l2);
list<int>l3;
l3.push_back(11);
l3.push_back(11);
l3.push_back(11);
cout << "l3赋值前:" << endl;
print(l3);
l3 = l1;
cout << "将l1赋值给l3:" << endl;
print(l3);
cout << "l3数据个数:"<<l3.size();
}
}
#include"list.h"
int main()
{
Imitate_list::test1();
Imitate_list::test2();
return 0;
}