Forward List in C++ | Set 2 (Manipulating Functions)
Forward List in C++ | Set 1 (Introduction and Important Functions) More functions are discussed in this article Some of the operations other than insertions and deletions that can be used in forward lists are as follows :
1. merge() :- This function is used to merge one forward list with other. If both the lists are sorted then the resultant list returned is also sorted.
2. operator “=” :- This operator copies one forward list into other. The copy made in this case is deep copy.
CPP
#include<iostream>
#include<forward_list>
using namespace std;
int main()
{
forward_list< int > flist1 = {1, 2, 3};
forward_list< int > flist2;
flist2 = flist1;
cout << "The contents of 2nd forward list"
" after copy are : " ;
for ( int &x : flist2)
cout << x << " " ;
cout << endl;
flist1.merge(flist2);
cout << "The contents of forward list "
"after merge are : " ;
for ( int &x : flist1)
cout << x << " " ;
cout << endl;
return 0;
}
|
Output:
The contents of 2nd forward list after copy are : 1 2 3
The contents of forward list after merge are : 1 1 2 2 3 3
Time Complexity: O(1)
Auxiliary Space: O(1)
3. sort() :- This function is used to sort the forward list.
4. unique() :- This function deletes the multiple occurrences of a number and returns a forward list with unique elements. The forward list should be sorted for this function to execute successfully.
CPP
#include<iostream>
#include<forward_list> // for sort() and unique()
using namespace std;
int main()
{
forward_list< int > flist1 = {1, 2, 3, 2, 3, 3, 1};
flist1.sort();
cout << "The contents of forward list after "
"sorting are : " ;
for ( int &x : flist1)
cout << x << " " ;
cout << endl;
flist1.unique();
cout << "The contents of forward list after "
"unique operation are : " ;
for ( int &x : flist1)
cout << x << " " ;
cout << endl;
return 0;
}
|
Output:
The contents of forward list after sorting are : 1 1 2 2 3 3 3
The contents of forward list after unique operation are : 1 2 3
Time Complexity: O(1)
Auxiliary Space: O(1)
5. reverse() :- This function is used to reverse the forward list.
6. swap() :- This function swaps the content of one forward list with other.
CPP
#include<iostream>
#include<forward_list> // for reverse() and swap()
using namespace std;
int main()
{
forward_list< int > flist1 = {1, 2, 3,};
forward_list< int > flist2 = {4, 5, 6};
flist1.reverse();
cout << "The contents of forward list after"
" reversing are : " ;
for ( int &x : flist1)
cout << x << " " ;
cout << endl << endl;
cout << "The contents of 1st forward list "
"before swapping are : " ;
for ( int &x : flist1)
cout << x << " " ;
cout << endl;
cout << "The contents of 2nd forward list "
"before swapping are : " ;
for ( int &x : flist2)
cout << x << " " ;
cout << endl;
flist1.swap(flist2);
cout << "The contents of 1st forward list "
"after swapping are : " ;
for ( int &x : flist1)
cout << x << " " ;
cout << endl;
cout << "The contents of 2nd forward list "
"after swapping are : " ;
for ( int &x : flist2)
cout << x << " " ;
cout << endl;
return 0;
}
|
Output:
The contents of forward list after reversing are : 3 2 1
The contents of 1st forward list before swapping are : 3 2 1
The contents of 2nd forward list before swapping are : 4 5 6
The contents of 1st forward list after swapping are : 4 5 6
The contents of 2nd forward list after swapping are : 3 2 1
Time Complexity: O(1)
Auxiliary Space: O(1)
7. clear() :- This function clears the contents of forward list. After this function, the forward list becomes empty.
8. empty() :- This function returns true if the list is empty otherwise false.
CPP
#include<iostream>
#include<forward_list> // for clear() and empty()
using namespace std;
int main()
{
forward_list< int > flist1 = {1, 2, 3,};
cout << "The contents of forward list are : " ;
for ( int &x : flist1)
cout << x << " " ;
cout << endl;
flist1.clear();
cout << "The contents of forward list after "
<< "clearing are : " ;
for ( int &x : flist1)
cout << x << " " ;
cout << endl;
flist1.empty() ? cout << "Forward list is empty" :
cout << "Forward list is not empty" ;
return 0;
}
|
Output:
The contents of forward list are : 1 2 3
The contents of forward list after clearing are :
Forward list is empty
Time Complexity: O(1)
Auxiliary Space: O(1)
Recent articles on forward_list
Last Updated :
21 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...