List in C++: Basic Functions

In this tutorial, we will learn about the list container of C++ and some basic functions related to it.

Lists in C++, are sequence containers that allocate memory in a non-contiguous fashion. Lists normally refer to a Doubly Linked List so If you don’t know what is a Doubly Linked List, please check it out:

Doubly Linked Lists in C++

Key points: List in C++

  • Lists in C++, refer to Doubly Linked Lists. For Singly Linked Lists, use forward_list.
  • Include <list> header in your code to use lists.
  • Traversal in lists is slower than traversal in vector but once we have traversed to the required position, insertion and deletion become easier.
  • In lists, a bi-directional iterator is used hence traversal can be done in both directions sequentially.

Functions used with list in C++

Here are some important functions of list in C++

push_front()

This function adds an element at the beginning of the list.

li.push_front(e);

Here e is added to the front of the list li.

push_back()

This function adds an element at the end of the list.

li.push_back(e);

Here e is added to the end of the list li.

Learn: push_back() and pop_back() function in C++ STL

pop_front()

This function deletes the element which is at the start of the list and reduces the size of the list by 1.

li.pop_front();

Here an element is popped out from the beginning of the list li.

pop_back()

This function deletes the element which is at the end of the list and reduces the size of the list by 1.

li.pop_back();

Here an element is popped out from the end of the list li and the size is reduced by 1.

front()

This function returns the first element (the element which is at the beginning) of the list.

li.front();

back()

This function returns the last element in the list.

li.back();

size()

This function returns the size of the list.

li.size();

empty()

This function returns 1 if the given list is empty. If not, it returns 0.

li.empty();

insert()

This function inserts an element given by a user at a position given by the user. Using this function we can also specify the number of occurrences of the element.

li.insert(it,e);
li.insert(it,n,e);

Here the element e is inserted at a position which is pointed by the iterator it. n is the number of occurrences of e in the list li. If n is not given then the compiler assumes it to be 1, which is its default value.

erase()

This function removes an element or a range of elements specified by the user.

li.erase(it);
li.erase(start_it,last_it);

In the first statement, the element which is being pointed by iterator it is removed from the list. In the second statement, two parameters are provided specifying the range of elements that need to be deleted.

Learn: Vector erase() and clear() in C++

assign()

This function is used to assign new values to the list by replacing the existing values and resize it.

li.assign(n,e);

Here element e is assigned to the list li n times.

remove()

This function takes an element as a parameter and removes all the occurrences of that element from the list.

li.remove(e);

reverse()

This is used to reverse a given list.

li.reverse();

sort()

This function sorts the elements of the list in increasing order.

li.sort();

begin()

This function returns an iterator pointing to the start of the list.

li.begin();

end()

This function returns an iterator pointing to the theoretical last element which follows the last element.

li.end();

clear()

This function deletes all the elements of the list container and sets its size to zero.

li.clear();

Implementation of all list functions in C++

#include <iostream>
#include <list>
#include <iterator>
using namespace std;

void printlist(list<int> li)
{
  list<int>::iterator it;
  cout<<"list->";
  for(it=li.begin();it!=li.end();it++)
  {
    cout<<" "<<*it;
  }
  cout<<"\n";
}

int main()
{
  list<int> li1;
    cout<<"A list has been created with no elements."<<"\n";
    //inserting an element using push_front() and push_back() functions
    cout<<"list after inserting element 1 and 2 using push_front() and push_back() functions:\n";
    li1.push_front(1);
    li1.push_front(2);
    li1.push_front(3);
    li1.push_back(4);
    li1.push_back(5);
    li1.push_back(6);
    printlist(li1);
    
    //using front() nad back() functions to print first and last element of the list
    cout<<"First element:"<<li1.front()<<endl;
    cout<<"Last element:"<<li1.back()<<endl;
    
    //using pop_front() and pop_back() functions
    li1.pop_back();
    li1.pop_front();
    cout<<"After popping first and last element, the list is:\n";
    printlist(li1);
    
    //using size() function
    cout<<"size of the list is:"<<li1.size()<<endl;
    
    //using sort() function
    cout<<"After sorting the list becomes:\n";
    li1.sort();
    printlist(li1);
    
    //using reverse() function
    cout<<"After reversing the list becomes:\n";
    li1.reverse();
    printlist(li1);
    
    //checking whether list is empty or not
    if(li1.empty())
    {
    	cout<<"List is empty.\n";
  }
  else
      cout<<"list is not empty.\n";
      
  //inserting elements using insert() function
  cout<<"Inserting more elements using insert():\n";
  list<int>::iterator it=li1.begin();                   //it points to the beginning of the  list
  advance(it,1);                                        //it points to 2nd position  
  li1.insert(it,7);                                     //inserting 7 to second position
  li1.insert(it,2,8);                                   //inserting 8 twice to third position
  printlist(li1);
  
  //using erase() function
  cout<<"Erasing first to 3rd element using erase():\n";
  list<int>::iterator s_it=li1.begin();                   // s_it and l_it both pointing to the beginning of the list
    list<int>::iterator l_it=li1.begin();
  advance(l_it,3);                                        // l_it pointing to 4th element 
    li1.erase(s_it,l_it);
    printlist(li1);
  
  //using remove() function
  cout<<"Deleting elements with value 8:\n";
    li1.remove(8);
    printlist(li1);
    
    //using assign() function
    cout<<"Assigning new values to the list:\n";
  li1.assign(2,9);
  printlist(li1);
  
  //Clearing the list
  cout<<"Now clear the list"<<endl;
  li1.clear();
  printlist(li1);
  
  
  return 0;
}

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

 

Output:

A list has been created with no elements.
list after inserting element 1 and 2 using push_front() and push_back() functions:
list-> 3 2 1 4 5 6
First element:3
Last element:6
After popping first and last element, the list is:
list-> 2 1 4 5
size of the list is:4
After sorting the list becomes:
list-> 1 2 4 5
After reversing the list becomes:
list-> 5 4 2 1
list is not empty.
Inserting more elements using insert():
list-> 5 7 8 8 4 2 1
Erasing first to 3rd element using erase():
list-> 8 4 2 1
Deleting elements with value 8:
list-> 4 2 1
Assigning new values to the list:
list-> 9 9
Now clear the list
list->

Leave a Reply

Your email address will not be published. Required fields are marked *