List in C++ – Some Useful Functions
Last Updated :
23 Mar, 2023
Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector, the List has slow traversal, but once a position has been found, insertion and deletion is quick.
List Useful Functions:
1. emplace(position, value): This function is used to insert an element at the specified position.
2. emplace_back(value) :- This function adds value at end of list. It is different from push_back() by the fact that it directly creates elements at position, whereas push_back() first makes a temporary copy and copies from there. emplace_back() is faster in implementation than push_back() in most situations.
3. emplace_front(value): This function adds value at beginning of the List. It is different from push_front() by the fact that it directly creates elements at position, whereas push_front() first makes a temporary copy and copies from there. emplace_front() is faster in implementation than push_front() in most situations.
CPP
#include <iostream>
#include <list> // for list functions
using namespace std;
int main()
{
list< int > gqlist;
list< int >::iterator it = gqlist.begin();
for ( int i = 1; i <= 5; i++)
gqlist.emplace_back(i);
cout << "List after emplace_back operation is : " ;
for ( int & x : gqlist)
cout << x << " " ;
cout << endl;
for ( int i = 10; i <= 50; i += 10)
gqlist.emplace_front(i);
cout << "List after emplace_front operation is : " ;
for ( int & x : gqlist)
cout << x << " " ;
cout << endl;
advance(it, 2);
gqlist.emplace(it, 100);
cout << "List after emplace operation is : " ;
for ( int & x : gqlist)
cout << x << " " ;
cout << endl;
return 0;
}
|
Output
List after emplace_back operation is : 1 2 3 4 5
List after emplace_front operation is : 50 40 30 20 10 1 2 3 4 5
List after emplace operation is : 50 100 40 30 20 10 1 2 3 4 5
Time Complexity: O(1)
Auxiliary Space: O(1)
4. merge(list2): This function is used to merge list2 with list1. If both the lists are in sorted order, then the resulting list is also sorted.
5. remove_if(condition): This function removes the element from the List on the basis of the condition given in its argument.
CPP
#include <iostream>
#include <list> // for list functions
using namespace std;
int main()
{
list< int > gqlist1 = { 1, 2, 3 };
list< int > gqlist2 = { 2, 4, 6 };
gqlist1.merge(gqlist2);
cout << "list1 after merge operation is : " ;
for ( int & x : gqlist1)
cout << x << " " ;
cout << endl;
gqlist1.remove_if([]( int x) { return x % 2 != 0; });
cout << "list1 after remove_if operation is : " ;
for ( int & x : gqlist1)
cout << x << " " ;
cout << endl;
return 0;
}
|
Output
list1 after merge operation is : 1 2 2 3 4 6
list1 after remove_if operation is : 2 2 4 6
Time Complexity: O(1)
Auxiliary Space: O(1)
6. unique(): This function is used to delete the repeated occurrences of the number. List has to be sorted for this function to get executed.
7. splice(position, list2): This function is used to transfer elements from one list into another.
CPP
#include <iostream>
#include <list> // for list functions
using namespace std;
int main()
{
list< int > gqlist1 = { 1, 1, 1, 2, 2, 3, 3, 4 };
list< int > gqlist2 = { 2, 4, 6 };
list< int >::iterator it = gqlist1.begin();
advance(it, 3);
cout << "list1 before unique operation is : " ;
for ( int & x : gqlist1)
cout << x << " " ;
cout << endl;
gqlist1.unique();
cout << "list1 after unique operation is : " ;
for ( int & x : gqlist1)
cout << x << " " ;
cout << endl << endl;
gqlist1.splice(it, gqlist2);
cout << "list1 after splice operation is : " ;
for ( int & x : gqlist1)
cout << x << " " ;
cout << endl;
return 0;
}
|
Output
list1 before unique operation is : 1 1 1 2 2 3 3 4
list1 after unique operation is : 1 2 3 4
list1 after splice operation is : 1 2 4 6 2 3 4
Time Complexity: O(1)
Auxiliary Space: O(1)
8. swap(list2): This function is used to swap one list element with other.
CPP
#include <iostream>
#include <list> // for list functions
using namespace std;
int main()
{
list< int > gqlist1 = { 1, 2, 3, 4 };
list< int > gqlist2 = { 2, 4, 6 };
cout << "The contents of 1st list "
"before swapping are : " ;
for ( int & x : gqlist1)
cout << x << " " ;
cout << endl;
cout << "The contents of 2nd list "
"before swapping are : " ;
for ( int & x : gqlist2)
cout << x << " " ;
cout << endl;
gqlist1.swap(gqlist2);
cout << "The contents of 1st list "
"after swapping are : " ;
for ( int & x : gqlist1)
cout << x << " " ;
cout << endl;
cout << "The contents of 2nd list "
"after swapping are : " ;
for ( int & x : gqlist2)
cout << x << " " ;
cout << endl;
return 0;
}
|
Output
The contents of 1st list before swapping are : 1 2 3 4
The contents of 2nd list before swapping are : 2 4 6
The contents of 1st list after swapping are : 2 4 6
The contents of 2nd list after swapping are : 1 2 3 4
Time Complexity: O(1)
Auxiliary Space: O(1)
9. reverse(): This function is used to reverse the order of elements in a list.
C++
#include <iostream>
#include <list>
int main()
{
std::list< int > mylist{ 1, 2, 3, 4, 5 };
std::cout << "Original list: " ;
for ( auto it = mylist.begin(); it != mylist.end();
it++) {
std::cout << *it << " " ;
}
std::cout << std::endl;
mylist.reverse();
std::cout << "Reversed list: " ;
for ( auto it = mylist.begin(); it != mylist.end();
it++) {
std::cout << *it << " " ;
}
std::cout << std::endl;
return 0;
}
|
Output
Original list: 1 2 3 4 5
Reversed list: 5 4 3 2 1
Time Complexity: O(1) where n is the number of elements in the list. This is because the function simply iterates over the list once and swaps the pointers for each element.
Auxiliary Space: O(1) where it does not use any extra memory beyond a few temporary variables used for swapping pointers.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...