A doubly linked list is a common data structure. It's not complicated, and it's not too difficult if we're going to make it ourselves. But since the STL has provided us with one, it may be used directly. To do so, not only saves time and effort, but also the reusability of code.
Header files and template classes
To use a doubly linked list provided by STL, you need to include a header file
#include <list>
This allows you to use the template class list<t>.
Initialization
Initializing a list is simple, using
Std::list<t> L;
You can initialize an empty linked list. Use
std::list<t> l{T1, T2, T3};
You can initialize a linked list with three elements.
For example
Std::list<int> l1{20, 13};
Then L1 is a linked list with 2 int. Again, for example,
std::list<std::string> l2{"Real variable function", "Functional Analysis", "differential geometry", "Abstract algebra"};
Then L2 is a linked list with 4 strings.
Add and delete elements
List<t> 's member function Push_back,push_front,pop_back, Pop_front's role is to add elements at the tail, add elements in the head, delete elements at the tail, delete elements in the head.
The first two function's parameter type is (t&&) (C++11 's "Move Semantics"), the latter two functions have no parameters.
None of the 4 functions have a return value.
Sizes (size)
The size of a list refers to how many elements are in the list, not how many bytes the list occupies in memory.
Gets the size of the list of tables that can be used with the list<t> member function size. It has no parameters and the return value is size_t.
Iterators
An iterator is a class that expresses a position. The following code doubles all elements of L1.
for (Auto it = L1.begin (); It! = L1.cend (); ++it) {(*it) *= 2;}
Removing an element from an iterator
You can use the function list<t>::erase to remove an element from an iterator. Its parameters are iterators that point to the element to be deleted.
Use of the C + + Standard Template Library (STL) doubly linked list (list)