forward_list::push_front() and forward_list::pop_front() in C++ STL
Last Updated :
23 Jun, 2022
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
// CPP program to illustrate
// push_front() function
#include <forward_list>
#include <iostream>
using namespace std;
int main()
{
forward_list<int> myforwardlist{ 1, 2, 3, 4, 5 };
myforwardlist.push_front(6);
// Forward list becomes 6, 1, 2, 3, 4, 5
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
// CPP program to illustrate
// application Of push_front() function
#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);
// Forward list becomes 7, 89, 45, 6, 24, 58, 43
// Sorting function
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
// CPP program to illustrate
// pop_front() function
#include <forward_list>
#include <iostream>
using namespace std;
int main()
{
forward_list<int> myforwardlist{ 1, 2, 3, 4, 5 };
myforwardlist.pop_front();
// forward list becomes 2, 3, 4, 5
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
// CPP program to illustrate
// application Of pop_front() function
#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);
// Forward list becomes 1, 2, 3, 4, 5, 6, 7, 8
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. |
Similar Reads
Forward List in C++ STL In C++, forward_list container provides the implementation of 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.Example:C++#inclu
7 min read
Commonly Used Methods
forward_list::begin() and forward_list::end() in C++ STLForward list in STL implements singly linked list. Introduced from C++11, the forward list is more 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 the forward list
3 min read
forward_list::push_front() and forward_list::pop_front() in C++ STLForward 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
4 min read
forward_list assign() function in C++ STLThe forward_list::assign() is a function in C++ STL which assigns new content to a forward list, replacing its current content and adjusting its size as required.Syntax: Version 1:forward_list_name.assign(iterator it1, iterator it2) Version 2:forward_list_name.assign(int n, val) Version 3:forward_li
2 min read
forward_list::front() and forward_list::empty() in C++ STLForward 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
3 min read
forward_list::remove() and forward_list::remove_if() in C++ STLForward list in STL implements singly linked list. The forward list was introduced in C++11, and is useful than other containers in insertion, removal, and moving operations (like sort) and allows time constant insertion and removal of elements. It differs from the list by the fact that the forward
4 min read
forward_list::clear() and forward_list::erase_after() in C++ STLForward 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
4 min read
forward_list::reverse() in C++ STLstd::forward_list::reverse() is an inbuilt function in CPP STL which reverses the order of the elements present in the forward_list. Syntax: forwardlist_name.reverse()Parameter: The function does not accept any parameter. Return value: The function has no return value. It reverses the forward list.
1 min read
forward_list::swap() in C++ STLThe forward_list::swap() is a built-in function in CPP STL which exchanges the contents of the first given forward_list with another forward_list. Syntax: swap(forward_list first, forward_list second) or forward_list1.swap(forward_list second) Parameters: The function accepts two parameters which ar
3 min read
std::forward_list::sort() in C++ STLForward 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
3 min read
Other Member Methods
Forward List and List of Pairs in C++ with Examples Forward List Forward list in STL implements singly linked list. Introduced from C++11, forward lists are more useful than other containers in insertion, removal, and moving operations (like sort) and allow time constant insertion and removal of elements. It differs from the list by the fact that the
8 min read
Forward List and List of Tuples in C++ with Examples What is Forward List? Forward list in STL is used to implement a singly linked list. It was introduced from C++11 onwards, forward lists are more useful than other containers in insertion, removal, and moving operations (like sort) and allow time constant insertion and removal of elements. It differ
9 min read
Difference Between Forward List and List in C++ Forward List is a sequence container that allows unidirectional sequential access to its data. It contains data of the same type. In STL, it has been implemented using Singly Linked List, which requires constant time for insertion and deletion. Elements of the forward list are scattered in the memor
3 min read
Common Forward List Programs