list::push_front() and list::push_back() in C++ STL

Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists.

list::push_front()

push_front() function is used to push elements into a list from the front. The new value is inserted into the list at the beginning, before the current first element and the container size is increased by 1.

Syntax :

listname.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 list named as listname

Examples:



Input : list list{1, 2, 3, 4, 5};
        list.push_front(6);
Output : 6, 1, 2, 3, 4, 5

Input : list list{5, 4, 3, 2, 1};
        list.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 list, it shows undefined behaviour.
// CPP program to illustrate
// push_front() function
#include <iostream>
#include <list>
using namespace std;

int main()
{
    list<int> mylist{ 1, 2, 3, 4, 5 };
    mylist.push_front(6);

    // list becomes 6, 1, 2, 3, 4, 5

    for (auto it = mylist.begin(); it != mylist.end(); ++it)
        cout << ' ' << *it;
}

Output:

6 1 2 3 4 5

Application : Input an empty list with the following numbers and order using push_front() function and sort the given 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 <iostream>
#include <list>
using namespace std;

int main()
{
    list<int> mylist{};
    mylist.push_front(43);
    mylist.push_front(58);
    mylist.push_front(24);
    mylist.push_front(6);
    mylist.push_front(45);
    mylist.push_front(89);
    mylist.push_front(7);

    // list becomes 7, 89, 45, 6, 24, 58, 43
    // Sorting function

    mylist.sort();

    for (auto it = mylist.begin(); it != mylist.end(); ++it)
        cout << ' ' << *it;
}

Output:

 6 7 24 43 45 58 89

list::push_back()

push_back() function is used to push elements into a list from the back. The new value is inserted into the list at the end, after the current last element and the container size is increased by 1.

Syntax :

listname.push_back(value)
Parameters :
The value to be added in the back is 
passed as the parameter
Result :
Adds the value mentioned as the parameter 
to the back of the list named as listname

Examples:

Input : list list{1, 2, 3, 4, 5};
        list.push_back(6);
Output :1, 2, 3, 4, 5, 6

Input : list list{5, 4, 3, 2, 1};
        list.push_front(0);
Output :5, 4, 3, 2, 1, 0

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 list, it shows undefined behaviour.
// CPP program to illustrate
// push_back() function
#include <iostream>
#include <list>
using namespace std;

int main()
{
    list<int> mylist{ 1, 2, 3, 4, 5 };
    mylist.push_back(6);

    // list becomes 1, 2, 3, 4, 5, 6

    for (auto it = mylist.begin(); it != mylist.end(); ++it)
        cout << ' ' << *it;
}

Output:

1 2 3 4 5 6

Application : Input an empty list with the following numbers and order using push_back() function and sort the given list.

Input :  7, 89, 45, 6, 24, 58, 43
Output : 6, 7, 24, 43, 45, 58, 89
// CPP program to illustrate
// application Of push_back() function
#include <iostream>
#include <list>
using namespace std;

int main()
{
    list<int> mylist{};
    mylist.push_back(7);
    mylist.push_back(89);
    mylist.push_back(45);
    mylist.push_back(6);
    mylist.push_back(24);
    mylist.push_back(58);
    mylist.push_back(43);

    // list becomes 7, 89, 45, 6, 24, 58, 43
    // Sorting function

    mylist.sort();

    for (auto it = mylist.begin(); it != mylist.end(); ++it)
        cout << ' ' << *it;
}

Output:

 6 7 24 43 45 58 89




Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1 Average Difficulty : 1/5.0
Based on 1 vote(s)