Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Difficulty Level : Basic
  • Last Updated : 28 Sep, 2021

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

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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

C++




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

C++




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

C++




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

C++




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

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :