C ++ Primer study note _ 13 _ Standard Template Library _ list two-way linked list container, _ 13_list

Source: Internet
Author: User

C ++ Primer study note _ 13 _ Standard Template Library _ list two-way linked list container, _ 13_list

C ++ Primer study note _ 13 _ Standard Template Library _ list two-way linked list container

The list container implements the data structure of a two-way linked list. The data element is a linear table in the logic sense through the linked list pointer string. In this way, inserting, deleting, and searching elements at any position in the linked list is extremely fast. Is the structure of a two-way cyclic linked list.

Each node in the list has three fields: the pre-element pointer field, the data field, and the subsequent element pointer field. The pointer field of the precursor element stores the first address of the precursor element, the data field is the data of the current node, and the pointer field of the subsequent element stores the first address of the subsequent element. The Source Element pointer field of the list header node stores the first address of the End Element in the linked list, while the Source Element pointer field of the End Node of the list stores the first address of the header node, A bidirectional loop chain.

Because the list object node does not need to be in a continuous memory, for the list iterator, you can only move the iterator to the successor/precursor node element through the "+ +" or "-" operation. The operation of + n or-n is not allowed, which is different from that of vector.

 

1. Create a list object

(1) Create an empty linked list

List <int> num;

(2) create a linked list with n elements

List <int> num (10 );

 

2. Insert and traverse Elements

Three new elements are inserted into the linked list.

(1) The push_back () method is used to insert new elements into the tail, and the linked list is automatically expanded.

(2) The push_front () method is used to insert new elements into the header, and the linked list is automatically expanded.

(3) The insert () method is used to insert new elements into the iterator location, and the linked list is automatically expanded. Note: Only "+ +" or "-" operations can be performed.

#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, 20);   for(iter = num.begin(); iter != num.end(); iter++)       cout << *iter << endl;   return 0;}

Running 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;}

Running result:

3 2 1

 

4. delete an element

(1) You can use the remove () method to delete an element in the linked list. All elements with 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;}

Running result:

1 2 3 1

2 3

 

(2) Use the pop_front () method to delete the first element of the linked list, and use the pop_back () method to delete the last element of the linked 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.pop_front();   num.pop_back();   for(iter = num.begin(); iter != num.end(); iter++)       cout << *iter << " ";   return 0;}

Running result:

1 2 3 1

2 3

 

(3) Use the erase () method to delete the element at the 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 elements (counted from 0) iter = num. begin (); iter ++; num. erase (iter); for (iter = num. begin (); iter! = Num. end (); iter ++) cout <* iter <""; return 0 ;}

Running result:

1 2 3 1

1 2 1

 

(4) clear the linked list using the clear () method

#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;}

Running result:

1 2 3 1

0

 

5. Search for elements

You can use the find () Search Algorithm to search for an element in the linked list. If the element is found, the return value is the iterator position of the element. If the element is not found, the return value is the end () iterator position.

The find () algorithm must 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;}

Running result:

1 2 3 1

Find it

Not find it

 

6. Sort Elements

The sort () method can be used to sort linked 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;}

Running result:

1 2 3 1

1 1 2 3

 

7. Remove repeated Elements

The unique () method can be used to remove duplicate elements and retain only one element.

# 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; // generally, it is sorted first and then duplicated num. sort (); num. unique (); for (iter = num. begin (); iter! = Num. end (); iter ++) cout <* iter <""; return 0 ;}

Running result:

1 2 3 1

1 2 3



Refer:

C ++ primer version 4

ACM Program Design


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.