Stl-list (doubly linked list)

Source: Internet
Author: User

Introduction to List
List is a doubly linked list container that efficiently inserts and deletes elements.
The list is not allowed to randomly access elements, so at is not supported. (POS) functions and [] operators. it++ (OK) it+5 (ERR)
#include <list>
Default construct for list object
List adopts the template class implementation, the object's default construction form:list<t> LSTT; Such as:
List<int> Lstint; Defines a list container that holds an int.
List<float> lstfloat; Defines a list container that holds float.
List<string> lststring; Defines a list container that holds a string.
...
You can also set pointer types or custom types within angle brackets.

Add remove operation for list kinsoku? List.push_back (Elem);              Add an element to the tail of the container? List.pop_back ();     Delete the last element in the container? List.push_front (Elem);              Insert an element at the beginning of the container? List.pop_front (); Remove the first element from the beginning of the container list<int> lstint;lstint.push_back (1); Lstint.push_back (3); Lstint.push_back (5); lstint.push_ Back (7); Lstint.push_back (9); Lstint.pop_front (); Lstint.pop_front (); Lstint.push_front (one); Lstint.push_front (13);   Lstint.pop_back (); Lstint.pop_back ();//Lstint {13,11,5}list data access? List.front ();  Returns the first element. List.back (); Returns the last element. List<int> Lstint;lstint.push_back (1); Lstint.push_back (3); Lstint.push_back (5); Lstint.push_back (7); Lstint.push_back (9); int ifront = Lstint.front ();//1int iback = Lstint.back ();//9lstint.front () = 11;//11lstint.back () =                     19;//19list and iterators? List.begin ();                       Returns an iterator to the first element in a container. List.end ();         Returns an iterator after the last element in the container. List.rbegin ();         Returns an iterator to the first element in the container. List.rend (); Returns an iterator to the back of the last element in the container. List<int> Lstint;lstint.push_back (1); Lstint.push_back (3); Lstint.push_back (5); Lstint.push_back (7); Lstint.push_back (9); for (List<int>::iterator It=lstint.begin (); It!=lstint.end (); ++it) {cout << *it;cout << "";} For (List<int>::reverse_iterator Rit=lstint.rbegin (); Rit!=lstint.rend (); ++rit) {cout << *rit;cout < < "";}    List object with parameter construction list (beg,end); The constructor copies the elements in the [Beg, end] interval to itself.   Note that the interval is left closed to the right open interval. List (N,elem);  The constructor copies n Elem to itself.? list (const list &lst); Copy constructor. List<int> Lstinta;lstinta.push_back (1); Lstinta.push_back (3); Lstinta.push_back (5); Lstinta.push_back (7); Lstinta.push_back (9);list<int> Lstintb (Lstinta.begin (), Lstinta.end ()),//1 3 5 7 9list<int> LstIntC (5,8)    ;//8 8 8 8 8 list<int> lstintd (Lstinta)//1 3 5 7 9list assignment? List.assign (Beg,end); Assigns a copy of the data in the [Beg, end] interval to itself.  Note that the interval is left closed right open interval. list.assign (N,elem);  Assigns n elem copies to itself.?list& operator= (const list &AMP;LST);//overloaded equals operator? List.swap (LST); The LST is interchanged with its own elements. List<int> Lstinta,lstintb,lstintc,lstintd;lstinta.push_back (1); LstinTa.push_back (3); Lstinta.push_back (5); Lstinta.push_back (7); Lstinta.push_back (9); Lstintb.assign (LstIntA.begin (), Lstinta.end ());//1 3 5 7 9lstintc.assign (5,8)//8 8 8 8 8lstIntD = LSTINTA;//1 3 5 7 9lstintc.swap (LSTINTD);//Interchange list size? li   St.size ();   Returns the number of elements in a container? List.Empty ();   Determine if the container is empty? list.resize (num); The container is re-specified as NUM, and if the container is longer, the new position is populated with the default value.  If the container is shorter, the elements at the end of the container length are removed. List.resize (num, elem); Reassign the container's length to num, and if the container is longer, fill the new position with the Elem value. If the container is shorter, the element at the end of the container length is removed. List<int> Lstinta;lstinta.push_back (1); Lstinta.push_back (3); Lstinta.push_back (5); if (!lstinta.empty ()) {int Isize = Lstinta.size ();//3lstinta.resize (5);//1 3 5 0 0lstinta.resize (7,1);//1 3 5 0 0 1 1lstinta.resize (2);//1 3}list insert?   List.insert (Pos,elem);   Inserts a copy of the Elem element at the POS location, returning the location of the new data. List.insert (Pos,n,elem);   Insert n elem data at POS location, no return value. List.insert (pos,beg,end); The data in the [Beg,end] interval is inserted at the POS location, with no return value. List<int> lsta;list<int> Lstb;lsta.push_back (1); Lsta.push_back (3); Lsta.push_back (5); LstA.push_back (7 ); Lsta.push_back (9); Lstb.push_back (2); LSTB.push_back (4); Lstb.push_back (6); Lstb.push_back (8); Lsta.insert (Lsta.begin (), one);//{11, 1, 3, 5, 7, 9}lsta.insert (+ +) Lsta.begin (), 2,33);//{11,33,33,1,3,5,7,9}lsta.insert (Lsta.begin (), Lstb.begin (), Lstb.end ());//{  2,4,6,8,11,33,33,1,3,5,7,9}list delete? List.clear ();//Remove all data from the container? List.erase (Beg,end);    Delete the data from the [Beg,end] interval and return to the location of the next data. List.erase (POS);   Deletes data from the POS location and returns the location of the next data. Lst.remove (Elem); Removes all elements in the container that match the Elem value. The element lstint in the delete interval is a container declared with list<int>, and now contains an ordered 1,3,5,6,9 element. List<int>::iterator Itbegin=lstint.begin (); + + Itbegin;list<int>::iterator itEnd=lstInt.begin (); + + itend;++ itend;++ itend;lstint.erase (itbegin,itend);//At this time the container lstint contains three elements in order 1,6,9.    Suppose Lstint contains 1,3,2,3,3,3,4,3,5,3, removes the method of the element in the container equal to 3 for a for (List<int>::iterator it=lstint.being (); It!=lstint.end ();)       The parentheses do not need to write ++it{if (*it = = 3) {it = lstint.erase (it);         The iterator is the parameter, the element 3 is removed, and the next element position after the data is deleted is returned to the iterator.   At this point, do not execute ++it;} else {++it; Delete Method two Lstint.remove (3) of the element in the container equal to 3, delete all elements of Lstint LstinT.clear ();//container is an empty list of the reverse order? Lst.reverse (); The list of reverses, such as LST, contains the 1,3,5 element, and when this method is run, LST contains the 5,3,1 element. List<int> Lsta;lsta.push_back (1); Lsta.push_back (3); Lsta.push_back (5); Lsta.push_back (7); LstA.push_back (9); Lsta.reverse ();//9 7 5 3 1
Demo

#include <iostream> #include <cstdio> #include <list> #include <algorithm>using namespace std; void Printlist (list<int> &l) {for (List<int>::iterator it = L.begin (); It! = L.end (); ++it) {cout << *it << ';} cout << Endl;} void Listinit () {list<int> l;cout << "size of L:" << l.size () << endl;//size of l:0for (int i = 0; I < 10;  ++i) {l.push_back (i);//tail interpolation method}cout << "size of L:" << l.size () << endl;//size of l:10printlist (l);//0 1 2 3 4 5 6 7 8 9//list cannot be accessed casually list<int>::iterator it = L.begin (); ++it;++it;++it;//it = it + 5;//does not support random access containers L.insert (it, 100); Inserted in front of 3 printlist (L);//0 1 2 100 3 4 5 6 7 8 9//Conclusion 1: Link List node index is starting from position No. 0//In position 3rd to insert element, so that the original 3rd position into the 4th position, the original 4th position becomes 5th position}voi  D listdelete () {list<int> l;for (int i = 0; i <; ++i) {l.push_back (i);//tail interpolation}printlist (l);//0 1 2 3 4 5 6 7 8 9list<int>::iterator it1 = L.begin (); List<int>::iterator it2 = L.begin (); ++it2;++it2;++it2;l.erase (it1, it2);p rintlist (L);//3 4 5 6 7 8 9l.insert (L.begin (),); L.insert (L.begin (), +); L.insert ( L.begin (),;p rintlist (l);//3 4 5 6 7 8 9l.erase (L.begin ());p rintlist (l);//100 3 4 5 6 7 8 9l.remove (1 XX);p rintlist (L);//3 4 5 6 7 8 9}int Main () {listinit (); Listdelete (); return 0;}

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

Stl-list (doubly linked list)

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.