Tutorialspoint.dev

forward_list::push_front() and forward_list::pop_front() in C++ STL

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 behaviour.

// 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

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 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 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

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 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


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter