[18] Linked list of C + + STL container

Keywords: C++ data structure linked list list

Linked list of C++STL container

The underlying principle of C++STL list

In fact, its bottom layer is the two-way linked list of data structure. It can be traversed from the beginning or from the end, but it is not cyclic. In STL, such a two-way linked list is encapsulated into a class, which is convenient for developers to use directly and avoid repeated wheel making.

How to create a list

For the list method, it depends on how many constructors there are in the list class. The fact is that there are many kinds.

The following creation methods are correct:

list<int> myList;
list<int> myList2(3);
list<int> myList3(myList2);

In fact, there are other creation methods besides those listed above, but the first one is recommended. When necessary, use the third one. If the second one is not necessary, it depends on your personal needs. What if you use it one day.

  • The first is to create only one linked list without nodes. When you want to use it, you can insert several nodes into it.
  • The second is to assign three nodes to the linked list at the beginning. The default initialization is zero. (the value of the value class is zero, and the string is an empty string)
  • The third is to assign an existing linked list to a new linked list.

Beginners can use the above three list s almost. Others can play on the compiler by themselves.

Traversal mode of list

Because the list is not sequential like array and vector, the data in the list cannot be accessed by brackets and subscripts. However, there are many traversal methods. I know four types:

  • New for loop traversal
  • Iterator traversal
  • Get the header element, delete the header element, and cycle through
  • Get the tail element, delete the tail element, and cycle through

Let's start with the first one. It's very simple. It doesn't need to be done internally. An auto inference is done. Let's take a look at the specific code implementation:

list<int> myList2(3);
for (auto v : myList2)
{
	cout << v << '\t';
}
cout << endl;

Everything printed out is zero. (default initialization is zero)

The second traversal method is to use iterators to traverse linked list elements. It takes a long time to write. Let's take a look at the code implementation:

list<int> myList;
list<int>::iterator iter;
for (iter = myList.begin(); iter != myList.end(); iter++)
{
	cout << *iter << '\t';
}
cout << endl;

The third traversal method is a little interesting. It is to use a loop to traverse and print the elements of the first node. After printing, delete the first node, and then use the second node as the first node until there are no nodes in the linked list. Traversal in this way will make the linked list empty. Traversal in the first two methods will not delete the nodes in the linked list.

Some common member functions of list

Function nameFunction function
sizeReturns the number of nodes in the current linked list
emptyJudge whether the current linked list is empty. If it is empty, 1 will be returned; if it is false, 0 will be returned
assignThere are multiple overloaded functions. The main function is to assign values to the linked list
frontReturns the reference of the first node in the linked list, which is equivalent to the first node in the linked list
backReturns the reference of the last node in the linked list, which is equivalent to the last node in the linked list
push_frontInsert a new node from the head of the linked list
push_backInsert a new node from the end of the linked list
beginReturns the address of the first node in the linked list. The iterator is returned, not the pointer
endReturns the next address of the last node in the linked list. The iterator is returned, not the pointer
insertThere are multiple overloads. The main function is to specify the location of the linked list and insert one or more nodes
sortSort the data of nodes in the linked list. By default, it is sorted from small to large. Remember to write overload for user-defined types
mergeMerge: merge the A-linked list into the B-linked list, and then the size of the A-linked list becomes 0. The size of the B-linked list is the sum of the size of the original A-linked list and the size of the B-linked list
pop_frontDelete a node from the head of the linked list
pop_backDelete a node from the end of the linked list
eraseA parameter overloaded function that specifies the location point to delete the node of the linked list
eraseThe overloaded function with two parameters specifies the position segment, deletes the node of the linked list, and closes before opening
clearClear all nodes in the linked list

Test the main function

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> List;
	cout << "The size of the linked list is:" << List.size() << endl;
	List.assign(3, 6);
	for (auto v : List)
	{
		cout << v << '\t';
	}
	cout << endl;
	List.push_front(99);
	List.push_back(1001);
	cout << List.front() << endl;
	cout << List.back() << endl;
	List.insert(List.begin(), 2, 88);
	for (auto v : List)
	{
		cout << v << '\t';
	}
	cout << endl;
	List.sort();
	list<int>::iterator iter;
	for (iter = List.begin(); iter != List.end(); iter++)
	{
		cout << *iter << '\t';
	}
	cout << endl;
	list<int> result = {1,2,3};
	cout << result.size() << endl;
	List.merge(result);
	for (auto v : List)
	{
		cout << v << '\t';
	}
	cout << endl;
	//1       2       3       6       6       6       88      88      99      1001
	cout << result.size() << endl;
	List.pop_front();
	List.pop_back();
	for (auto v : List)
	{
		cout << v << '\t';
	}
	cout << endl;
	//2       3       6       6       6       88      88      99
	list<int>::iterator iter1, iter2;
	iter1 = iter2 = List.begin();
	iter1++;
	advance(iter2, 5);
	iter1 = List.erase(iter1);
	iter2 = List.erase(iter2);
	for (auto v : List)
	{
		cout << v << '\t';
	}
	cout << endl;
	//2       6       6       6       88      99
	cout << List.size() << '\t' << *iter1 << '\t' << *iter2 << endl;
	List.erase(iter1, iter2);
	cout << List.size() << endl;
	for (auto v : List)
	{
		cout << v << '\t';
	}
	cout << endl;
	//2       88      99
	List.clear();
	cout << List.size() << endl;	//0
	return 0;
}

Output results:

The size of the linked list is: 0
6       6       6
99
1001
88      88      99      6       6       6       1001
6       6       6       88      88      99      1001
3
1       2       3       6       6       6       88      88      99      1001
0
2       3       6       6       6       88      88      99
2       6       6       6       88      99
6       6       88
3
2       88      99
0

Posted by cute_girl on Sat, 09 Oct 2021 22:23:16 -0700