C + + Primer learning Note _13_ Standard Template Library _list doubly linked list container
The list container realizes the data structure of the doubly linked list, and the data elements are concatenated into a logical linear table through the linked list pointers, so that the insertion, deletion, and lookup of the elements in any position of the linked list are super-fast. Is the structure of a two-way circular linked list.
Each node of the list has three fields: the precursor element pointer field, the data field, and the successor element pointer field. The precursor element pointer field holds the first address of the precursor element, the data field is the data of this node, and the successor element Pointer field holds the first address of the successor element. The precursor element of the list's head node holds the first address of the tail element in the linked list, and the successor of the list's tail node holds the first address of the head node, thus constituting a two-way loop chain.
Because the node of the list object is not required to be in a contiguous amount of memory, for a list iterator, the iterator can be moved to the successor/precursor element only through the operation "+ +" or "--". Instead of +n or-n operations, this is a different place from vectors.
1. Create a List Object
(1) Create an empty list
list<int>num;
(2) Create a linked list with n elements
List<int>num (10);
2. Element insertion and traversal
There are three ways to insert new elements into the list
(1) using the Push_back () method to insert a new element into the tail, the list automatically expands.
(2) using the Push_front () method to insert a new element to the first, the list automatically expands.
(3) using the Insert () method inserts a new element at the iterator position, and the linked list expands automatically. Note: only the "+ +" or "--" actions are allowed.
#include <iostream> #include <list> using namespace std; int main (int argc, char* argv[]) { list<int> num; Num.push_back (1); Num.push_back (2); Num.push_front (8); List<int>::iterator iter = Num.begin (); iter++; Num.insert (ITER); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << Endl; return 0;}
Operation Result:
8
20
1
2
3. Reverse Traversal
#include <iostream> #include <list> using namespace std; int main (int argc, char* argv[]) { list<int> num; Num.push_back (1); Num.push_back (2); Num.push_back (3); List<int>::reverse_iterator iter = Num.rbegin (); for (iter = Num.rbegin (); ITER! = Num.rend (); iter++) cout << *iter << ""; return 0;}
Operation Result:
3 2 1
4. Element deletion
(1) You can use the Remove () method to delete an element in the linked list, and the same value will be deleted.
#include <iostream> #include <list> using namespace std; int main (int argc, char* argv[]) { list<int> num; Num.push_back (1); Num.push_back (2); Num.push_back (3); Num.push_back (1); List<int>::iterator iter = Num.begin (); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << ""; cout << Endl; Num.remove (1); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << ""; return 0;}
Operation Result:
1 2 3 1
2 3
(2) Use the Pop_front () method to delete the first element of the list, using the Pop_back () method to delete the chain footer element
#include <iostream> #include <list> using namespace std; int main (int argc, char* argv[]) { list<int> num; Num.push_back (1); Num.push_back (2); Num.push_back (3); Num.push_back (1); List<int>::iterator iter = Num.begin (); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << ""; cout << Endl; Num.pop_front (); Num.pop_back (); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << ""; return 0;}
Operation Result:
1 2 3 1
2 3
(3) using the Erase () method to delete an element on an iterator position
#include <iostream> #include <list> using namespace std; int main (int argc, char* argv[]) { list<int> num; Num.push_back (1); Num.push_back (2); Num.push_back (3); Num.push_back (1); List<int>::iterator iter = Num.begin (); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << ""; cout << Endl; Delete 2nd Element (counting starting from 0) iter = Num.begin (); iter++; iter++; Num.erase (ITER); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << ""; return 0;}
Operation Result:
1 2 3 1
1 2 1
(4) Use the Clear () method to clear the list
#include <iostream> #include <list> using namespace std; int main (int argc, char* argv[]) { list<int> num; Num.push_back (1); Num.push_back (2); Num.push_back (3); Num.push_back (1); List<int>::iterator iter = Num.begin (); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << ""; cout << Endl; Num.clear (); cout << num.size () << Endl; return 0;}
Operation Result:
1 2 3 1
0
5. Element Lookup
The find () lookup algorithm finds the element in the linked list, returns the iterator position of the element if it is found, and returns the end () iterator position if it is not found.
The find () algorithm needs to declare "#include <algorithm>"
#include <iostream> #include <list> #include <algorithm> using namespace std; int main (int argc, char* argv[]) { list<int> num; Num.push_back (1); Num.push_back (2); Num.push_back (3); Num.push_back (1); List<int>::iterator iter = Num.begin (); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << ""; cout << Endl; iter = Find (Num.begin (), Num.end (), 3); if (iter! = Num.end ()) cout << "Find it" << Endl; else cout << "not find it" << Endl; iter = Find (Num.begin (), Num.end (), 5); if (iter! = Num.end ()) cout << "Find it" << Endl; else cout << "not find it" << Endl; return 0;}
Operation Result:
1 2 3 1
Find it
Not find it
6. Ordering of elements
Use the sort () method to sort the list elements in ascending order
#include <iostream> #include <list> #include <algorithm> using namespace std; int main (int argc, char* argv[]) { list<int> num; Num.push_back (1); Num.push_back (2); Num.push_back (3); Num.push_back (1); List<int>::iterator iter = Num.begin (); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << ""; cout << Endl; Num.sort (); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << ""; return 0;}
Operation Result:
1 2 3 1
1 1 2 3
7. Eliminate continuous repeating elements
Using the unique () method, you can reject successive repeating elements and keep only one.
#include <iostream> #include <list> #include <algorithm> using namespace std; int main (int argc, char* argv[]) { list<int> num; Num.push_back (1); Num.push_back (2); Num.push_back (3); Num.push_back (1); List<int>::iterator iter = Num.begin (); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << ""; cout << Endl; General is first sort, after go to heavy num.sort (); Num.unique (); for (iter = Num.begin (); ITER! = Num.end (); iter++) cout << *iter << ""; return 0;}
Operation Result:
1 2 3 1
1 2 3
Reference:
"C + + Primer Fourth Edition"
"ACM Program Design"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Primer learning Note _13_ Standard Template Library _list doubly linked list container