Using the list container in STL to implement single-linked list operations

Source: Internet
Author: User

#include <iostream>#include <list>#include <algorithm>using namespace STD;voidPrint (int&item) {cout<<item<<" ";}intMain () { list<int>Listintegers; list<int>:: Iterator Listiter;//Introducing iterators  //------------inserting elements into the head-------------Listintegers.push_front (5); Listintegers.push_front (3); Listintegers.push_front (1); Listintegers.push_front (2); Listintegers.push_front (4);//----------Insert element----------Listintegers.push_back (6); Listintegers.push_back (8); Listintegers.push_back (7);//--------inserting elements into a linked list using the member function of the list insert ()Listintegers.insert (Listintegers.end (),Ten); Listintegers.insert (Listintegers.end (),9);//----------Use the iterator to output the list-----------  /* We use one or more iterator in each algorithm.      We use them to access the objects in the container. To access a given object, we point a iterator to it and then indirectly refer to this iterator * /  cout<<"Linked list is:"; for(Listiter=listintegers.begin (); Listiter!=listintegers.end (); listiter++) {cout<<*listiter<<" "; }cout<<endl;//-------Use the STL generic algorithm for_each () output chain list---------------  / * Print is a function that implements the output function of the object * /  cout<<"Linked list is:";STD:: For_each (Listintegers.begin (), Listintegers.end (), Print);cout<<endl;//------Use the STL General algorithm find () to determine if an element exists in a linked list----------Listiter=find (Listintegers.begin (), Listintegers.end (),6);if(Listiter==listintegers.end ()) {cout<<"6 is not in list"<<endl; }Else{cout<<"6 is in list"<<endl; }//-------Use the STL General Algorithm search () to determine if a sequence exists in the list-------   list<int>TargetList; Targetlist.push_front (2); Targetlist.push_front (1);//define the sequence asListiter=search (Listintegers.begin (), Listintegers.end (), Targetlist.begin (), Targetlist.end ());if(Listiter==listintegers.end ()) {cout<<"Sequence is not in list"<<endl; }Else{cout<<"sequence" in List "<<endl; }//Use the list's member function sort () to order the linked list   cout<<"After sorting, the list is:"; Listintegers.sort (); for(Listiter=listintegers.begin (); Listiter!=listintegers.end (); listiter++) {cout<<*listiter<<" "; }cout<<endl;//Use the member function of List remove () to delete a list elementListintegers.remove (8);cout<<"After deleting 8, the list is:"; for(Listiter=listintegers.begin (); Listiter!=listintegers.end (); listiter++) {cout<<*listiter<<" "; }cout<<endl;//----------Delete the first element of the chain using the list member function Pop_front----------Listintegers.pop_front ();cout<<"After deleting the first element of the chain, the list is:"; for(Listiter=listintegers.begin (); Listiter!=listintegers.end (); listiter++) {cout<<*listiter<<" "; }cout<<endl;//----------Delete a chain-tailed element using the list member function Pop_back----------Listintegers.pop_back ();cout<<"After deleting the chain tail element, the list is:"; for(Listiter=listintegers.begin (); Listiter!=listintegers.end (); listiter++) {cout<<*listiter<<" "; }cout<<endl; System"Pause");return 0;}

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

Using the list container in STL to implement single-linked list operations

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.