Introduction:
List is a generalized container of a two-way linked list. Its data elements can be concatenated into linear tables in a logical sense through the linked list pointer. Unlike vector and deque containers that use linear table sequence storage structures, element search, insertion, and deletion at any position in list two-way linked lists are supported, they all have efficient constant order algorithm time complexity O (1 ).
Basic list applications:
Create a list object:
1. List (const A & A = a () creates an empty list object.
For example, list <int> L;
2. List (size_type N) creates a list object with n elements. Each list element has its default value under its type.
For example, list <int> L (10 );
3. List (size_type N, const T & Value) creates a list object with n elements. Each element has an initial value.
For example, list <int> L (10, 5 );
4. List (const list &) creates a new list object by copying each element value of a list object.
For example: List <int> L1 (10, 5); List <int> L2 (L1 );
5. List (const inputiterator first, const inputiterator last, const A & A = a () by copying the element values of the iterator range [first, last, in creating a new list object, the memory distributor can be omitted.
For example: int iarray [] = {1, 2, 3}; List <int> L (iarray, iarray + 3 );
Initialization assignment:
The push_back function provided by list is often used to initialize the list container. The push_back function inserts a new element value at the end of the container.
Void push_back (const T & value)
Element traversal access:
Because the data in the linked list needs to be traversed by one element, the traversal of the list element only uses the iterator.
Element insertion:
Because the insertion of list linked list elements does not require shift copying of other elements, the element insertion function of list has the O (1) algorithm complexity of constant order. In addition to adding elements to the end of the push_back function, the list function also includes the push_front function that inserts elements at the beginning of the chain and the insert function inserted at any iterator.
Iterator insert (iterator POs, const T & X)
Void push_front (const T &)
Element deletion:
Iterator erase (iterator POS) deletes the linked list elements referred to by the iterator POS.
Iterator erase (iterator first, iterator last) deletes all linked list elements in the iterator range [first, last)
Void clear () delete all linked list elements
Void pop_front () deletes the first linked list element of the list.
Void pop_back () deletes the last linked list element of the list.
Void remove (const T & Value) deletes all elements whose element values are value in the list linked list.
Reverse traversal of elements:
Reverse_iterator rbegin ()
Reverse_iterator rend ()
List exchange:
The swap function of the List container implements the exchange of list elements by simply exchanging the header pointers of two lists.
Void swap (List &)
Merge list:
Void splice (iterator position, list & X) clears list object X before merging the linked list of X to the position of the current list linked list.
Void splice (iterator position, list &, iterator I) merges the elements referred to by the list iterator I value into the current list linked list, and delete the elements from the original linked list.
Void merge (List & X) combines the linked list of list object X into the current list linked list and clears the X linked list. The merge function is meaningful only when the elements of the current linked list and the merged X linked list are sorted by the "<" Relationship of element values in advance, the elements of the merged linked list are also sorted by the "<" relationship.
Sort list elements:
The void sort function provided by list sorts the elements in the list linked list according to the "<" relationship, with smaller elements at the top.
Delete the continuous repeating elements of the list:
Using the void unique function of list, you can delete consecutive duplicate elements and retain only one element.
Example:
1,
// Insert list linked list elements
# Include <list>
# Include <iostream>
Using namespace STD;
Int main (void)
{
List <int> L;
L. push_back (6 );
L. push_back (8 );
L. push_back (9 );
// Insert a linked list element
List <int>: iterator I, iend;
I = L. Begin ();
I ++;
L. insert (I, 7 );
L. push_front (5 );
// Print the linked list element
Iend = L. End ();
For (I = L. Begin (); I! = Iend; I ++)
Cout <* I <"";
Return 0;
}
2,
// Delete a list linked list element
# Include <list>
# Include <iostream>
Using namespace STD;
Int main (void)
{
List <int> L;
L. push_back (5 );
L. push_back (6 );
L. push_back (7 );
L. push_back (8 );
L. push_back (9 );
L. push_back (9 );
L. push_back (9 );
L. push_back (10 );
// Delete the element. The remaining elements are 7, 8, and 9.
List <int>: iterator I, iend;
I = L. Begin ();
I ++;
L. Erase (I );
L. pop_back ();
L. pop_front ();
L. Remove (9 );
// Print
Iend = L. End ();
For (I = L. Begin (); I! = Iend; I ++)
{
Cout <* I <"";
}
Return 0;
}
3,
// Merge list linked lists
# Include <list>
# Include <iostream>
Using namespace STD;
Void print (list <int> & L)
{
List <int>: iterator I, iend;
Iend = L. End ();
For (I = L. Begin (); I! = Iend; I ++)
{
Cout <* I <"";
}
}
Int main (void)
{
List <int> L;
For (Int J = 1; j <= 10; j ++)
{
L. push_back (j );
}
// Splice () function
List <int> carry;
Carry. splice (carry. Begin (), l, l. Begin ());
// Print carry
Cout <"The linked list element of carry is :";
Print (carry );
Cout <Endl;
// Print l
Cout <"l linked list element :";
Print (L );
Cout <Endl;
// Merge () function usage
List <int> X;
X. push_back (30 );
X. push_back (31 );
X. push_back (32 );
L. Merge (X );
// Print x
Cout <"X linked list element: NULL ";
Print (X );
Cout <Endl;
// Print l
Cout <"l linked list element :";
Print (L );
Cout <Endl;
Return 0;
}
4,
// Sorting of list linked list elements
# Include <list>
# Include <iostream>
Using namespace STD;
Void print (list <int> & L)
{
List <int>: iterator I, iend;
Iend = L. End ();
For (I = L. Begin (); I! = Iend; I ++)
Cout <* I <"";
Cout <Endl;
}
Int main (void)
{
List <int> L;
For (Int J = 18; j> = 0; j --)
L. push_back (j );
Cout <"Before sorting :";
Print (L );
// Call the list <int>: Sort () function for sorting.
L. Sort ();
Cout <"after sorting :";
Print (L );
Return 0;
}
5,
// Delete a list of continuous repeated Elements
# Include <list>
# Include <iostream>
Using namespace STD;
Int main (void)
{
List <int> L;
L. push_back (6 );
L. push_back (8 );
L. push_back (6 );
L. push_back (6 );
L. push_back (6 );
L. push_back (9 );
L. push_back (13 );
L. push_back (6 );
L. Unique ();
List <int>: iterator I, iend;
Iend = L. End ();
For (I = L. Begin (); I! = Iend; I ++)
Cout <* I <"";
Cout <Endl;
Return 0;
}