![]() |
VOOZH | about |
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.
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.
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.
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.
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