Forward_list container provides the implementation of the singly linked list data structure. It stores data in non-contiguous memory where each element points to the next element in the sequence. This makes insertion and deletion faster once the position of the element is known.
Syntax
The forward list is defined as the std::forward_list class template inside the <forward_list> header file.
forward_list<T> fl;
where,
- T: Data type of elements in the forward list.
- fl: Name assigned to the forward list.
Declaration and Initialization
A forward_list can be declared and initialized in several ways, as shown in the example:
#include <bits/stdc++.h>
using namespace std;
void printFL(forward_list<int>& fl) {
for (auto i : fl)
cout << i << " ";
cout << '\n';
}
int main() {
forward_list<int> fl1;
forward_list<int> fl2(3, 4);
forward_list<int> fl3 = {1, 5, 3, 4};
printFL(fl2);
printFL(fl3);
return 0;
}
Output
4 4 4 1 5 3 4
Explanation: In the above program, we are simple initialized forward list in three ways:
- "forward_list<int> fl1" creates an empty forward list of integers.
- "forward_list<int> fl2(3,4)" creates a forward list of size 3 and with each element being 4.
- "forward_list<int> fl3 = {1, 5, 3, 4}" creates a forward list and initializes with the elements form the initializer list.
Basic Operations
Here are the basic operations we can perform on a forward list:
1. Accessing Elements
forward_list does not support index access like arrays or vectors. Elements must be accessed sequentially using iterators (with next() or advance()), except the first element which can be accessed directly using front().
#include <bits/stdc++.h>
using namespace std;
int main() {
forward_list<int> fl = {1, 5, 3, 4};
// Access the first element
cout << fl.front() << endl;
// Access third element
auto it = next(fl.begin(), 2);
cout << *it;
return 0;
}
Output
1 3
Explanation: The first element is printed by using "front()" method. To access the third element, "next()" is used to move the iterator two positions from the beginning, and "*it" is used to dereference the iterator.
2. Inserting Elements
Elements can be inserted in the forward list using insert_after() function. It requires the iterator after which the element is to be inserted. However, fast insertion at the front is supported by push_front() method.
#include <bits/stdc++.h>
using namespace std;
int main() {
forward_list<int> fl = {5, 4};
// Inserting Element at front
fl.push_front(1);
// Insert 3 after the second element
auto it = fl.begin();
advance(it, 1);
fl.insert_after(it, 3);
for (auto x: fl) cout << x << " ";
return 0;
}
Output
1 5 3 4
Explanation: In this program, the first element of the forward_list is inserted at the front using the push_front() function. Then, an iterator is created and moved one position forward using the advance() function. After that, the element 5 is inserted after the second element using the insert_after() function.
3. Updating Elements
The value of existing elements can be changed simply by accessing them and using assignment operator to assign the new value.
#include <bits/stdc++.h>
using namespace std;
int main() {
forward_list<int> fl = {1, 5, 3, 4};
// Updating first element
fl.front() = 111;
cout << fl.front() << endl;
// Updating third element
auto it = next(fl.begin(), 2);
*it = 333;
cout << *it;
return 0;
}
Output
111 333
4. Finding Element
The forward list does not provide any member function to search for an element, but we can use the find() algorithm to find any given value.
#include <bits/stdc++.h>
using namespace std;
int main() {
forward_list<int> fl = {1, 5, 3, 4};
// Finding 3
auto it = find(fl.begin(), fl.end(), 3);
if (it != fl.end()) cout << *it;
else cout << "Element not Found";
return 0;
}
Output
3
Explanation:
- Create a forward_list fl with values {1, 5, 3, 4}.
- Use find() to search for the value 3 in the list.
- find() returns an iterator to the element if found, otherwise fl.end().
- Check the iterator and print the value if it exists.
5. Traversing
A forward list can be traversed using begin() and end() iterators with a loop, but we can only move forward and not backward.
#include <bits/stdc++.h>
using namespace std;
int main() {
forward_list<int> fl = {1, 5, 3, 4};
// Traversing using range-based for loop
for(auto i : fl)
cout << i << " ";
cout << endl;
return 0;
}
Output
1 5 3 4
6. Deleting Elements
In forward list, we can delete the element at the given position using erase_after() method. This method takes the iterator to one position before the target element. Fast deletion from the front is possible using pop_front() method.
#include <bits/stdc++.h>
using namespace std;
int main() {
forward_list<int> fl = {1, 5, 3, 4};
// Delete first element
fl.pop_front();
// Delete third element
auto it = fl.begin();
advance(it, 1);
fl.erase_after(it);
for (auto x: fl) cout << x << " ";
return 0;
}
Output
5 3
Explanation:
- Create a forward_list fl with values {1, 5, 3, 4}.
- Remove the first element using pop_front().
- Get an iterator to the list and move it one step using advance().
- Delete the element after that position using erase_after().
- Traverse the list and print remaining elements.
7. Size of Forward List
forward_list does not have a built-in size() function. To find its size, we need to manually count the elements by traversing it with a loop or using std:: distance.
#include <iostream>
#include <forward_list>
#include <iterator>
using namespace std;
int main() {
forward_list<int> flist={10,20,30,40};
int size = distance(flist.begin(),flist.end());
cout<<"Size of forward_list: "<<size<<endl;
return 0;
}
Output
Size of forward_list: 4
8. empty()
It is used to check if the forward_list is empty. It returns true if the list is empty and false otherwise, allowing to quickly verifying if the container has no data.
#include <iostream>
#include <forward_list>
using namespace std;
int main() {
forward_list<int> flist;
if (flist.empty()) {
cout << "The forward_list is empty." << endl;
}
flist.push_front(10);
if (!flist.empty()) {
cout << "The forward_list is not empty." << endl;
}
return 0;
}
Output
The forward_list is empty. The forward_list is not empty.