Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of location of only next element while list keeps track to both next and previous elements.
forward_list::push_front
push_front() function is used to push elements into a Forward list from the front. The new value is inserted into the Forward list at the beginning, before the current first element and the container size is increased by 1.
Syntax :
forwardlistname.push_front(value)
Parameters :
The value to be added in the front is
passed as the parameter
Result :
Adds the value mentioned as the parameter to the
front of the forward list named as forwardlistname
Examples:
Input : forward_list forwardlist{1, 2, 3, 4, 5};
forwardlist.push_front(6);
Output : 6, 1, 2, 3, 4, 5
Input : forward_list forwardlist{5, 4, 3, 2, 1};
forwardlist.push_front(6);
Output :6, 5, 4, 3, 2, 1
Errors and Exceptions
1. Strong exception guarantee – if an exception is thrown, there are no changes in the container.
2. If the value passed as argument is not supported by the forward list, it shows undefined behavior.
CPP
#include <forward_list>
#include <iostream>
using namespace std;
int main()
{
forward_list< int > myforwardlist{ 1, 2, 3, 4, 5 };
myforwardlist.push_front(6);
for ( auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it)
cout << ' ' << *it;
}
|
Output:
6 1 2 3 4 5
Time Complexity: O(1)
Auxiliary Space: O(1)
Application : Input an empty forward list with the following numbers and order using push_front() function and sort the given forward list.
Input : 7, 89, 45, 6, 24, 58, 43
Output : 6, 7, 24, 43, 45, 58, 89
CPP
#include <forward_list>
#include <iostream>
using namespace std;
int main()
{
forward_list< int > myforwardlist{};
myforwardlist.push_front(43);
myforwardlist.push_front(58);
myforwardlist.push_front(24);
myforwardlist.push_front(6);
myforwardlist.push_front(45);
myforwardlist.push_front(89);
myforwardlist.push_front(7);
myforwardlist.sort();
for ( auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it)
cout << ' ' << *it;
}
|
Output
6 7 24 43 45 58 89
forward_list::pop_front
pop_front() function is used to pop or remove elements from a forward list from the front. The value is removed from the list from the beginning, and the container size is decreased by 1.
Syntax :
forwardlistname.pop_front()
Parameters :
No parameter is passed as the parameter.
Result :
Removes the value present at the front
of the given forward list named as forwardlistname
Examples:
Input : forward_list forwardlist{1, 2, 3, 4, 5};
forwardlist.pop_front();
Output :2, 3, 4, 5
Input : forward_list forwardlist{5, 4, 3, 2, 1};
forwardlist.pop_front();
Output :4, 3, 2, 1
Errors and Exceptions
1. No-Throw-Guarantee – if an exception is thrown, there are no changes in the container.
2. If the list is empty, it shows undefined behaviour.
CPP
#include <forward_list>
#include <iostream>
using namespace std;
int main()
{
forward_list< int > myforwardlist{ 1, 2, 3, 4, 5 };
myforwardlist.pop_front();
for ( auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it)
cout << ' ' << *it;
}
|
Output:
2 3 4 5
Time Complexity: O(1)
Auxiliary Space: O(1)
Application : Input an empty forward list with the following numbers and order using push_front() function and print the reverse of the list.
Input : 1, 2, 3, 4, 5, 6, 7, 8
Output: 8, 7, 6, 5, 4, 3, 2, 1
CPP
#include <forward_list>
#include <iostream>
using namespace std;
int main()
{
forward_list< int > myforwardlist{}, newforwardlist{};
myforwardlist.push_front(8);
myforwardlist.push_front(7);
myforwardlist.push_front(6);
myforwardlist.push_front(5);
myforwardlist.push_front(4);
myforwardlist.push_front(3);
myforwardlist.push_front(2);
myforwardlist.push_front(1);
while (!myforwardlist.empty()) {
newforwardlist.push_front(myforwardlist.front());
myforwardlist.pop_front();
}
for ( auto it = newforwardlist.begin(); it != newforwardlist.end(); ++it)
cout << ' ' << *it;
}
|
Output
8 7 6 5 4 3 2 1
Time Complexity: O(n)
Auxiliary Space: O(1)
Let us see the differences in a tabular form -:
|
forward_list::push_front() |
forward_list::pop_front() |
1. |
It is used to insert a new element at the beginning of the forward_list. |
It is used to remove the first element in the forward_list container |
2. |
Its syntax is -: push_front (const value_type& val); |
Its syntax is -:
pop_front();
|
3. |
It takes only one parameter that is the value to be copied to the inserted element. |
It does not take any parameters. |
4. |
Its complexity is constant. |
It does not have any return value. |
5. |
Its iterator validity does not change. |
Its complexity is constant. |
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Jun, 2022
Like Article
Save Article