Open In App

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

Last Updated : 23 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

Let us see the differences in a tabular form -:

  list::push_front() list::push_back()
1. It is used to insert a new element at the beginning of the list. It is used to add a new element at the end of the list container
2. Its syntax is -:
push_front (const value_type& val);

Its syntax is -:

push_back (const value_type& val);

3. Its takes one parameter that is the value to be inserted. Its takes one parameter that is the value to be inserted.
4. Its complexity is constant. Its complexity is constant.
5. Its iterator validity does not changes. Its iterator validity does not changes.

 



Similar Reads

list push_front() function in C++ STL
The list::push_front() is a built-in function in C++ STL which is used to insert an element at the front of a list container just before the current top element. This function also increases the size of the container by 1. Syntaxlist_name.push_front(dataType value)Parameters This function accepts a single parameter value. This parameter represents
2 min read
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 tr
4 min read
deque::push_front() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::push_front() push_front
2 min read
list push_back() function in C++ STL
The list:push_back() function in C++ STL is used to add a new element to an existing list container. It takes the element to be added as a parameter and adds it to the list container. Syntaxlist_name.push_back(value)ParametersThis function accepts a single parameter which is a mandatory value. This refers to the elements needed to be added to the l
1 min read
vector::push_back() and vector::pop_back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::push_back() push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the cur
4 min read
deque::push_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::push_back() push_back()
2 min read
push_back() vs emplace_back() in C++ STL Vectors
In C++, vectors are dynamic arrays, that can grow or shrink and their storage is handled by the container itself. There are two ways of inserting an element in a vector. They are push_back() and emplace_back(). In this article, we will discuss the difference between them. push_back(): This method is used to insert elements in a vector from the end
5 min read
std::string::push_back() in C++
The push_back() member function is provided to append characters. Appends character c to the end of the string, increasing its length by one. Syntax : void string:: push_back (char c) Parameters: Character which to be appended. Return value: None Time Complexity: O(1) as it appends a single character to the string. Auxiliary Space: O(1) Error: thro
1 min read
std::string::append vs std::string::push_back() vs Operator += in C++
To append characters, you can use operator +=, append(), and push_back(). All of them helps to append character but with a little difference in implementation and application. Operator += : appends single-argument values. Time complexity : O(n)append() : lets you specify the appended value by using multiple arguments. Time complexity: O(n)push_back
6 min read
list::pop_front() and list::pop_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::pop_front() pop_front() function is used to pop or remove elements from a list from the front
4 min read
Article Tags :
Practice Tags :