Open In App

C++ Remove Elements From a List While Iterating

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: List in C++

Removing elements is a necessary task for any container. Removing elements from a list is an easy task as we have predefined pop_back and pop_front functions but as the list is a container following the properties of a doubly linked list there should be a method to remove elements while iterating. 

You cannot directly delete the element from the list while iterating because of doing you get Runtime Error because the iterator is now pointing to NULL and the address of the next element of the list gets lost and you cannot access the list anymore.

Removing Element using Single Iterator

Iterators are the components of STL used for iteration over a container. So, we can use an iterator to iterate over the list, and while traversing we can remove the elements we want to remove. Removing elements with respect to value or index can be done using this method.

Syntax:

iterator = list_name.erase(iterator);

That will update the iterator to point to the location after the iterator you removed from the list.

Remember to not do it++ in the loop because you already increment the iterator by 1 after each iteration. If you do so, then you have to do it– in each iteration of the loop for maintaining continuity.

Time Complexity: O(N)
Auxiliary Space: O(1)

Below is the implementation of the above approach.

C++




// C++ Program to implement Removing
// element from the list while iteration
#include <iostream>
#include <list>
using namespace std;
  
// Function to print elements From
// the list While iterating
void print(list<int>& li)
{
    auto it = li.begin();
    if (it == li.end()) {
        cout << "List is Empty" << endl;
    }
  
    for (it; it != li.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
  
// Function to delete elements From
// the list While iterating
  
void solve(list<int>& li)
{
    list<int>::iterator it = li.begin();
  
    // Before Deletion
    cout << "Before Deletion: " << endl;
    cout << "Size of List: " << li.size() << endl;
    print(li);
  
    // Deleting element from list while iterating
    while (it != li.end()) {
        if (*it % 3 == 0) {
            it = li.erase(it);
            continue;
        }
        it++;
    }
  
    // After Deletion
    cout << "\nAfter Deletion: " << endl;
    cout << "Size of List: " << li.size() << endl;
    print(li);
}
  
// Driver Code
int main()
{
    list<int> li = { 1, 2, 3, 5, 4, 6 };
  
    solve(li);
  
    return 0;
}


Output

Before Deletion: 
Size of List: 6
1 2 3 5 4 6 

After Deletion: 
Size of List: 4
1 2 5 4 

Iterating in for loop:

There is no such change in the while and for loop, just the way to increment is changed which can be quite confusing.

Below is the implementation of the above topic

C++




// C++ Program for removing elements
// in a list using single iterator
// in for loop
#include <iostream>
#include <list>
using namespace std;
  
// Function to print elements From
// the list While iterating
void print(list<int>& li)
{
    auto it = li.begin();
    if (it == li.end()) {
        cout << "List is Empty" << endl;
    }
  
    for (it; it != li.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
  
// Function to delete elements From
// the list While iterating
void solve(list<int>& li)
{
  
    list<int>::iterator it;
  
    // Before Deletion
    cout << "Before Deletion: " << endl;
    cout << "Size of List: " << li.size() << endl;
    print(li);
  
    // Deleting element from list while iterating
    // through it--
    for (auto it = li.begin(); it != li.end(); it++) {
        if (*it % 3 == 0) {
            it = li.erase(it);
            it--;
        }
    }
  
    // After Deletion
    cout << "\nAfter Deletion: " << endl;
    cout << "Size of List: " << li.size() << endl;
    print(li);
}
  
// Driver Code
  
int main()
{
  
    list<int> li = { 1, 2, 3, 5, 4, 6 };
  
    solve(li);
  
    return 0;
}


Output

Before Deletion: 
Size of List: 6
1 2 3 5 4 6 

After Deletion: 
Size of List: 4
1 2 5 4 

Removing Element using two iterators

Using 2 iterators is just the same just rather than operating over the iterator we operate over the dummy iterator so that when the iterator is incremented it comes to its right position.

Example:

C++




// C++ Program to implement Removing of
// element while iterating using
// 2 iterators
#include <iostream>
#include <list>
using namespace std;
  
// Function to print elements From
// the list While iterating
void print(list<int>& li)
{
    auto it = li.begin();
    if (it == li.end()) {
        cout << "List is Empty" << endl;
    }
  
    for (it; it != li.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
  
// Function to delete elements From
// the list While iterating
void solve(list<int>& li)
{
    list<int>::iterator it = li.begin();
  
    // Before Deletion
    cout << "Before Deletion: " << endl;
    cout << "Size of List: " << li.size() << endl;
    print(li);
  
    // Deleting element from list while iterating
    // through iterator
    while (it != li.end()) {
        list<int>::iterator temp = it++;
        if (*temp % 3 == 0) {
            temp = li.erase(temp);
        }
    }
  
    // After Deletion
    cout << "\nAfter Deletion: " << endl;
    cout << "Size of List: " << li.size() << endl;
    print(li);
}
  
// Driver Code
int main()
{
    list<int> li = { 1, 2, 3, 5, 4, 6 };
    solve(li);
  
    return 0;
}


Output

Before Deletion: 
Size of List: 6
1 2 3 5 4 6 

After Deletion: 
Size of List: 4
1 2 5 4 


Last Updated : 18 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads