List doubly linked list container

Source: Internet
Author: User

List is a generalization container for doubly linked lists, which provides splice and merge merge functions, and the sort function merges and sorts elements using the data structure features of list.

Create a List Object

There are several main ways to create a list object.
(1) List ()

list<int> l;

(2) List (size_type N)

list<int> l(10);

(3) List (Size_type n,const t&value)

list<int> l(10,2.5);

(4) List (const list&)

list<char> l1(5,’k’);list<char> l2(l1);

(5) List (inputiterator first, Inputiterator last)

intarray[]={1,2,3,4,5};list<int> l(array,array+5);
Initialization

Using the Push_back function for initialization, the prototype is:

void push_back(const T&);list<int> l;l.push_back(1);l.push_back(2);l.push_back(3);
Traverse

The traversal of a list element can only be implemented using iterators.

#include <iostream>#include <list>using namespace STD;structstudent{Char*name;intAge;};intMain () {student s[]={{"Li", -},{"Shi", -},{"Wang", -}}; list<student>L L.push_back (s[0]); L.push_back (s[1]); L.push_back (s[2]);//Iterator traversal     list<student>:: Iterator Begin,end; End=l.end (); for(Begin=l.begin (); begin!=end;begin++) {cout<< (*begin) .name<<" "<< (*begin) .age<<endl; }return 0;}
Insert

With the Push_back function at the end of the insertion, the Push_front function is inserted at the beginning of the chain, insert is inserted anywhere, and the insert operation does not require a shift copy of the other elements, the time complexity is O (1)

#include <iostream>#include <list>using namespace STD;intMain () { list<int>L L.push_back (1); L.push_back (2); L.push_back (3);//Insert     list<int>:: Iterator Begin,end;    Begin=l.begin (); begin++; L.insert (Begin,4); L.push_front (5); End=l.end (); for(Begin=l.begin (); begin!=end;begin++) {cout<<*begin<<" "; }return 0;}
Delete

Using the Pop_front function to delete the first element, the Pop_back function removes the tail element, the erase function deletes the element at any specified iterator position, clear deletes all linked list elements, and removes all elements of the specified value.

#include <iostream>#include <list>using namespace STD;intMain () { list<int>L L.push_back (1); L.push_back (2); L.push_back (3); L.push_back (3); L.push_back (3); L.push_back (4); L.push_back (5); list<int>:: Iterator Begin,end;    Begin=l.begin ();    begin++;    L.erase (begin);    L.pop_front ();    L.pop_back (); L.remove (3); End=l.end (); for(Begin=l.begin (); begin!=end;begin++) {cout<<*begin<<" "; }return 0;}
Reverse traversal

Reverse iterators reverse_iterator and Const_reverse_iterator

list<int>::reverse_iterator rbegin,rend;rend=l.rend();for(rbegin=l.rbegin();rbegin!=rend;rbegin++){    cout<<*rbegin<<" ";}
Exchange

Swapping with the SWAP function, the prototype is

void swap(list&)//list<int> l1,l2;l1.swap(l2);
Merge

Merge using the splice function and the merge function.
(1) Splice (iterator pos,list&x)
The list object x is emptied before the linked list of X is merged into the POS position of the current linked list.
(2) Splice (iterator Pos,list&,iterator i)
Merges the elements of a list's iterator I value into the current list linked list, and the merged elements are removed from the original linked list.
(3) Merge (list&x)
Merges the list object X's linked list into the current list link list and empties the X's list.

#include <iostream>#include <list>using namespace STD;voidPrint list<int>&AMP;L) { list<int>:: Iterator Begin,end; End=l.end (); for(Begin=l.begin (); begin!=end;begin++) {cout<<*begin<<" "; } }intMain () { list<int>L for(intI=1; i<=Ten; i++) L.push_back (i);//splice Merge     list<int>C C.splice (C.begin (), L,l.begin ());cout<<"-------C:"; print (c);cout<<endl;cout<<"-------L:"; Print (l);cout<<endl;//merge Merge     list<int>X X.push_back (Ten); X.push_back ( -); X.push_back ( -); X.push_back ( +); L.merge (x);cout<<"-------x:"; print (x);cout<<endl;cout<<"-------L:"; Print (l);cout<<endl;return 0;}

Sort

Sort by using the sort function provided by the list container, ascending by default.

#include <iostream>#include <list>using namespace STD;voidPrint list<int>&AMP;L) { list<int>:: Iterator Begin,end; End=l.end (); for(Begin=l.begin (); begin!=end;begin++) {cout<<*begin<<" "; }cout<<endl;}intMain () { list<int>L for(intI=Ten; i>=0; i--) {l.push_back (i); }cout<<"before sorting:";p rint (L); L.sort ();cout<<"After sorting:";p rint (L);return 0;}
Delete consecutive repeating elements

The unique function provided by the list container allows you to delete continuously repeating elements.

#include <iostream>#include <list>using namespace STD;intMain () { list<int>L L.push_back (1); L.push_back (1); L.push_back (1); L.push_back (2); L.push_back (3); L.push_back (4); L.push_back (1); L.push_back (1); L.push_back (1);//Remove elements for continuous repetitionL.unique (); list<int>:: Iterator Begin,end; End=l.end (); for(Begin=l.begin (); begin!=end;begin++) {cout<<*begin<<" "; }return 0;}

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

List doubly linked list container

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.