Article directory
1. Introduction to list doubly linked list container
1. Container characteristics
The list doubly linked list container can efficiently insert/delete elements at any position;
list is a pointer to the element of the doubly linked list container: the element in the container contains 2 pointers, one pointing to the predecessor of the element and one pointing to the successor of the element;
2. Container operation time complexity
List doubly linked list container operation time complexity:
- The time complexity of inserting or deleting elements from the head and tail is O(1);
- Inserting or deleting elements in the middle of the table may require moving n elements in the worst case, and the time complexity is O(n);
3. Traversal access
Iterator: The list doubly linked list container provides the iterator function, and you can use iterators to traverse the elements in the container;
The list doubly linked list container cannot be accessed randomly , that is, the elements cannot be obtained based on the subscript, and the elements in the container cannot be accessed using at()
functions and []
operators;
5. Header file
To use the list doubly linked list container, you need to import the <list> header file;
#include <list>
2. list doubly linked list container constructor
The common operations of list doubly linked list container are basically the same as vector. Here is a brief introduction;
1. Default parameterless constructor
list is the default no-argument constructor of the doubly linked list container. The construction format is as follows:
list<T> lstT
The T generic type in angle brackets is the element type stored in the list doubly linked list container;
lstT is the variable name of the doubly linked list container;
The default no-argument constructor creates an empty list doubly linked list;
Code example:
#include "iostream"
using namespace std;
#include "list"
int main() {
// 默认无参构造函数 会创建空的 list 双向链表
// list 双向链表容器, 存储 int 类型元素
list<int> lstInt;
// list 双向链表容器, 存储 float 类型元素
list<float> lstFloat;
// list 双向链表容器, 存储 string 类型元素
list<string> lstString;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
Results of the :
2. Create a doubly linked list containing n identical elements.
Create a doubly linked list containing n identical elements, and the constructor will copy the n identical elements into the container;
The function prototype is as follows:
list(size_type n, const value_type& value = value_type(), const allocator_type& alloc = allocator_type());
This constructor will create a new list containing n elements, and the value of each element is initialized to value;
If value is not provided, the element is initialized to the default value and the provided alloc is used to allocate memory;
For example: if it is an element of type int, it is initialized to 0;
Code example:
// list 双向链表容器, 存储 3 个 int 类型元素 666
list<int> lstInt(3, 666);
Complete code example:
#include "iostream"
using namespace std;
#include "list"
// 打印 list 容器内容
void printL(list<int>& lst) {
// 获取迭代器起始位置
list<int>::iterator it = lst.begin();
cout << "list 容器内容 : ";
// 循环判定, 如果没有迭代到最后一个元素的后一个位置, 那么一直循环
while (it != lst.end())
{
// 获取元素值
cout << *it << " ";
// 迭代器指向下一个元素
it++;
}
// 回车换行
cout << endl;
}
int main() {
// list 双向链表容器, 存储 3 个 int 类型元素 666
list<int> lstInt(3, 666);
// 打印 list 容器内容
printL(lstInt);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
Results of the :
list 容器内容 : 666 666 666
请按任意键继续. . .
3. Use the initialization list to construct a list doubly linked list
Use the initialization list to construct a list. The prototype of the doubly linked list function is as follows:
list(std::initializer_list<value_type> init, const allocator_type& alloc = allocator_type());
This constructor creates a list whose elements are copied from the init initializer list;
Code example:
// list 双向链表容器 使用初始化列表构造
list<int> lstInt{
1, 2, 3, 4, 5};
Complete code example:
#include "iostream"
using namespace std;
#include "list"
// 打印 list 容器内容
void printL(list<int>& lst) {
// 获取迭代器起始位置
list<int>::iterator it = lst.begin();
cout << "list 容器内容 : ";
// 循环判定, 如果没有迭代到最后一个元素的后一个位置, 那么一直循环
while (it != lst.end())
{
// 获取元素值
cout << *it << " ";
// 迭代器指向下一个元素
it++;
}
// 回车换行
cout << endl;
}
int main() {
// list 双向链表容器 使用初始化列表构造
list<int> lstInt{
1, 2, 3, 4, 5};
// 打印 list 容器内容
printL(lstInt);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
Results of the :
list 容器内容 : 1 2 3 4 5
请按任意键继续. . .
4. Use another list container to construct a list doubly linked list container
There are three ways to construct a list doubly linked list container using another list container:
- The parameter is a reference to another list container: the constructor will create a new list, which is a copy of another list other;
list(const list& other);
// list 双向链表容器 使用初始化列表构造
list<int> lstInt{
1, 2, 3, 4, 5};
// 是 lstInt 的副本
list<int> lstInt2 (lstInt);
- The parameter is an iterator specifying the interval range of another list container: this constructor will create a new list, whose elements are copied from the range [first, last), note that the front is closed and the end is open; this range can be of any type Input iterators, including but not limited to pointers and iterators of std::vector, std::deque and other containers;
list(InputIt first, InputIt last);
// list 双向链表容器 使用初始化列表构造
list<int> lstInt{
1, 2, 3, 4, 5};
// 注意是前闭后开区间
list<int> lstInt3( ++lstInt.begin(), lstInt.end());
Code example:
#include "iostream"
using namespace std;
#include "list"
// 打印 list 容器内容
void printL(list<int>& lst) {
// 获取迭代器起始位置
list<int>::iterator it = lst.begin();
cout << "list 容器内容 : ";
// 循环判定, 如果没有迭代到最后一个元素的后一个位置, 那么一直循环
while (it != lst.end())
{
// 获取元素值
cout << *it << " ";
// 迭代器指向下一个元素
it++;
}
// 回车换行
cout << endl;
}
int main() {
// list 双向链表容器 使用初始化列表构造
list<int> lstInt{
1, 2, 3, 4, 5};
// 是 lstInt 的副本
list<int> lstInt2 (lstInt);
// 注意是前闭后开区间
list<int> lstInt3( ++lstInt.begin(), lstInt.end());
// 打印 list 容器内容
printL(lstInt);
printL(lstInt2);
printL(lstInt3);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
Results of the :
list 容器内容 : 1 2 3 4 5
list 容器内容 : 1 2 3 4 5
list 容器内容 : 2 3 4 5
请按任意键继续. . .