Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Making Linked Lists More User-Friendly-2 : Page 2


advertisement
Step 1: Creating a List Object
The header <list> contains the declaration of std::list and its specialized algorithms. Our first step is to create an empty list of integers called li:

#include <list> using std::list; int main() { list <int> li; }

To insert a single element, use the push_back() member function:



li.push_back(1);

To remove the last element of a list, use the pop_back() member function

li.pop_back();

The push_front() member function inserts an element before the list's beginning:

li.push_front(0); // the first element is now 0, not 1

Similarly, pop_front() removes the first element of a list:

li.pop_front();

To remove a certain value from the list, use remove(). remove(n) removes all the elements that equal to n from the list:

li.remove(0); // li doesn't contain any zeros anymore



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date