List doubly linked list container application basics (Create, traverse, insert, delete, merge, sort, and continuously repeat element culling, etc.)

Source: Internet
Author: User

Unlike vector and deque containers with linear table sequential storage structures, element differences, insertions and deletions in any position in a list doubly linked list have an efficient time complexity O (1) for the constant order algorithm.

Header file

#include <list>

Create a List Object

1) list ();// Create a list object without any elements .

2) List (size_type n);//Create aNElements ofListobject, each element takes its default value under its type.

List<int>l,//list object L has 10 elements, each element has an initial value of.
3) list<size_type N, constt& value); //Create aNElements ofListobject, the initial value of these elements isvalue.

4) List (const list&);//listcopy constructor, by copying aListobject to create a new value for theListobject.

5) List (const inputiterator First, const inputiterator last,const a& a=a ());

// the Iteration interval [First,last] The element that is referred to is copied to a newly created List object, where the memory allocator can be defaulted.

Using the int array iarray, create a list object L intiarray[]={1,2,3,4,5,6,7}; List<int>l (IArray, iarray+7); Create a List Object

Initialize Assignment

By using the push_back function provided by list, you can chain elements sequentially into the linked list. The Push_back function is commonly used in the initialization of a list container.

Traversal access of elements

Because elements in the list need to be traversed by elements, the traversal of thelist element can only be done using iterators.

#include <iostream> #include <list>using namespace Std;int main () {int array[] = {16,2,77,29};// Create a List Object list<int> mylist (array,array+sizeof (Array)/sizeof (int)) in the fifth way above;//traverse output for (LIST<INT>:: Iterator it = Mylist.begin (); It! = Mylist.end (); it++) std::cout << *it << '; std::cout << ' \ n '; return 0;}

Insertion of list elements

Because the insertion of the list element does not require a shift copy of the other elements, the insertion function of the list element is the time complexity of the O (1) algorithm with a constant order .

Voidpush_front (constt&);// Head Insertion

Iteratorinsert (iterator pos, const t& x);//pos Insert a new element before the position x

Examples are similar to those used in the previous deque, and there is no longer much to say here.

Deletion of list elements

List also has efficient list element deletion processing, including deletion of the first element Pop_front function, delete the trailing element Pop_back function to delete an element at any position or iteration interval . Erase function, and delete all elements of the Clear function.

1) void Pop_front ();// Delete list 's first linked list element

2) void Pop_back ();// Delete The last linked list element of list

3) iteratorerase (iterator POS);// Delete The linked list element that Pos points to

4) Iteratorerase (iterator first, iterator last);// Delete all linked list elements pointed to by the iterator interval [First, last]

5) void clear ();// Delete all list linked list elements

6) void Remove (constt& value);// Delete all element values in list linked list as value the Elements

examples are similar to those used in the previous deque , and there is no longer much to say here.

Aggregation of list lists

List the ordering of the list elements is the List The list is divided into several parts to sub-sort, and then through the merging process, to achieve List the sort of all elements. To do this,the list container provides the splice and merge merge functions.

1) void splice (iterator position, list& x);// x Span style= "Color:rgb (192,0,0)" > list position Prior to location, list x

void splice (iterator position, list& X, iterator I);// a list I list lists and removes the merged elements from the original linked table.

void merge (list& x);// Span style= "Color:rgb (192,0,0)" >list x list linked list, and empty x from the source of the merge function can be seen, only the current list linked list x elements of a linked list, are pre-determined by the element value of the "<" The relationship is good, merge function is meaningful, and the merged list element is also pressed "<" relationship sort.

#include <list> #include <iostream>using namespace std;void print (list<int>& l); int main () {list <int> l;list<int> carry;for (int j=1; j<=10; J + +) {L.push_back (j);} Splice () function Carry.splice (Carry.begin (), L, ++l.begin ()),//print carrycout << "Carry list element:";p rint (carry);// The elements of the list that print lcout << "L" are: ";p rint (l);//merge () function usage list<int> x;x.push_back (); X.push_back () X.push_back ( L.merge (x);//Print Xcout << "The list element of x is empty";p rint (x);//Print Lcout << "L's list element is:";p rint (l); return 0;} List linked list print void print (list<int>& l) {list<int>::iterator I, Iend;iend = L.end (); for (I=l.begin (); i!= Iend; ++i) cout << *i << "cout << endl << Endl;}

Sort elements of List

List provided by void Sort function, press "<" Relationship to List The list elements are sorted, and the smaller elements are in front.

#include <list> #include <iostream>using namespace std;void print (list<int>& l); int main () {list <int> l;  for (int j=18; j>=0; j--) L.push_back (j); cout << "before sorting:"; Print (L);//Call List<int>::sort () function to sort l.sort (); cout << "after sorting:"; Print (l); return 0;} void print (list<int>& l) {list<int>::iterator i,iend;iend=l.end (); for (I=l.begin (); i!=iend; i++) cout << *i << '; cout << Endl;}

Elimination of continuous repeating elements of list

Use List provided by void Unique function, you can delete successive duplicate elements. Keep Only one.

#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 (+); 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;}


This article for my original, reproduced please indicate the source: http://blog.csdn.net/lsh_2013/article/details/46742541

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

List doubly linked list container application basics (Create, traverse, insert, delete, merge, sort, and continuously repeat element culling, etc.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.