1. Introduction to List
A linked list is a non-contiguous, non-sequential storage structure on a physical storage unit, and the logical sequence of data elements is achieved through the link order of pointers in the linked list.
The linked list is composed of a series of nodes (each element in the linked list is called a node), which can be dynamically generated at runtime. Each node includes two parts: one is the data field for storing data elements, and the other is the pointer field for storing the address of the next node.
Compared with the continuous linear space of the vector, the advantage of the list is that each time an element is inserted or deleted, it is to configure or release the space of an element. Therefore, the list is absolutely accurate in the use of space, and it is not wasteful at all. Moreover, for element insertion or element removal at any position, the list is always constant time.
List and vector are the two most commonly used containers.
The list container is a doubly linked list container , which can efficiently insert and delete elements.
The list cannot access elements randomly, so the at.(pos) function and [] operator are not supported.
#include <list>
Second, the default structure of the list object
The list is implemented using template classes, and the default structure of the object: list<T> LIST; such as:
list<int> lstInt; //定义一个存放int的list容器。
list<float> lstFloat; //定义一个存放float的list容器。
list<string> lstString; //定义一个存放string的list容器。
//尖括号内还可以设置指针类型或自定义类型。
Three, add and remove operations at the beginning and end of the list
list.push_back(elem); //Add an element to the end of the container
list.pop_back(); //Delete the last element in the container
list.push_front(elem); //Insert an element at the beginning of the container
list.pop_front(); //Remove the first element from the beginning of the container
list<int> lstInt;
lstInt.push_back(1);
lstInt.push_back(3);
lstInt.push_back(5);
lstInt.push_back(7);
lstInt.push_back(9);
lstInt.pop_front();
lstInt.pop_front();
lstInt.push_front(11);
lstInt.push_front(13);
lstInt.pop_back();
lstInt.pop_back();
// lstInt {13,11,5}
Four, list data access
list.front(); //Return the first element.
list.back(); //Return the last element.
list<int> lstInt;
lstInt.push_back(1);
lstInt.push_back(5);
lstInt.push_back(9);
int iFront = lstInt.front(); //1
int iBack = lstInt.back(); //9
Five, list and iterator
list.begin(); //Return the iterator of the first element in the container.
list.end(); //Returns the iterator after the last element in the container.
list.rbegin(); //Returns the iterator of the last element in the container.
list.rend(); //Returns the iterator behind the last-to-last element in the container.
for (list<int>::iterator it=lstInt.begin(); it!=lstInt.end(); ++it) {
cout << *it<<cout << " ";
}
Six, the parameter structure of the list object
list(beg,end); //The constructor copies the elements in the [beg, end) range to itself. Note that the interval is left closed and right opened.
list(n,elem); //The constructor copies n elems to itself.
list(const list &lst); //Copy constructor.
list<int> mlist1;
list<int> mlist2(10, 10); //有参构造
list<int> mlist3(mlist2);//拷贝构造
list<int> mlist4(mlist2.begin(), mlist2.end());
for (list<int>::iterator it = mlist4.begin(); it != mlist4.end(); it++) {
cout << *it << " ";
}
cout << endl;
/*
结果:
10 10 10 10 10 10 10 10 10 10
*/
Seven, list assignment
list.assign(beg,end); //Assign a copy of the data in the interval [beg, end) to itself. Note that the interval is left closed and right opened.
list.assign(n,elem); //Assign n copies of elem to itself.
list& operator=(const list &lst); //Overload the equal sign operator
list.swap(lst); // Swap lst with its own elements.
list<int> lstIntA,lstIntB,lstIntC,lstIntD;
lstIntA.push_back(1);
lstIntA.push_back(5);
lstIntA.push_back(9);
lstIntB.assign(lstIntA.begin(),lstIntA.end()); //1 5 9
lstIntC.assign(5,8); //8 8 8 8 8
lstIntD = lstIntA; //1 5 9
lstIntC.swap(lstIntD); //互换
Eight, the size of the list
list.size(); //Returns the number of elements in the container
list.empty(); //Determine whether the container is empty
list.resize(num); //Re-specify the length of the container as num. If the container becomes longer, fill the new position with the default value. If the container becomes shorter, the elements at the end that exceed the length of the container are deleted.
list.resize(num, elem); //Re-specify the length of the container as num. If the container becomes longer, fill the new position with the elem value. If the container becomes shorter, the elements at the end that exceed the length of the container are deleted.
list<int> lstIntA;
lstIntA.push_back(11);
lstIntA.push_back(33);
lstIntA.push_back(55);
if (!lstIntA.empty())
{
int iSize = lstIntA.size(); //3
lstIntA.resize(5); //11 33 55 0 0
lstIntA.resize(7,1); //11 33 55 0 0 1 1
lstIntA.resize(2); //11 33
}
Nine, list insertion
list.insert(pos,elem); //Insert a copy of the element element at the pos position and return the position of the new data.
list.insert(pos,n,elem); //Insert n elem data at position pos, no return value.
list.insert(pos,beg,end); //Insert the data in the [beg,end) interval at position pos, no return value.
list<int> lstA;
list<int> lstB;
lstA.push_back(1);
lstA.push_back(5);
lstA.push_back(9);
lstB.push_back(2);
lstB.push_back(6);
lstA.insert(lstA.begin(), 11); //{11, 1, 5, 9}
lstA.insert(++lstA.begin(),2,33); //{11,33,33,1,5,9}
lstA.insert(lstA.begin() , lstB.begin() , lstB.end() ); //{2,6,11,33,33,1,5,9}
Ten, delete the list
list.clear(); //Remove all data of the container
list.erase(beg,end); //**Delete the data in the [beg,end)** interval and return the position of the next data.
list.erase(pos); //Delete the data at the pos position and return the position of the next data.
lst.remove(elem); //Remove all elements in the container that match the elem value.
//删除区间内的元素
//lstInt是用list<int>声明的容器,现已包含按顺序的1,3,5,6,9元素。
list<int>::iterator itBegin=lstInt.begin();
++ itBegin;
list<int>::iterator itEnd=lstInt.begin();
++ itEnd;
++ itEnd;
++ itEnd;
lstInt.erase(itBegin,itEnd);
//此时容器lstInt包含按顺序的1,6,9三个元素。
//假设 lstInt 包含1,3,2,3,3,3,4,3,5,3,删除容器中等于3的元素的方法一
for(list<int>::iterator it=lstInt.being(); it!=lstInt.end(); ) //小括号里不需写 ++it
{
if(*it == 3){
it = lstInt.erase(it); //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。
//此时,不执行 ++it;
}
else{
++it;
}
}
//删除容器中等于3的元素的方法二
lstInt.remove(3);
//删除lstInt的所有元素
lstInt.clear(); //容器为空
Eleven, the reverse order of the list
lst.reverse(); //Reverse linked list
list<int> lstA;
lstA.push_back(1);
lstA.push_back(3);
lstA.push_back(5);
lstA.push_back(7);
lstA.push_back(9);
lstA.reverse(); //9 7 5 3 1