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


In this article we will be discussing the working, syntax and examples of list::push_front() and list::push_back() functions in C++ STL.

What is a List in STL?

List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List performs better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.

What is list::push_front()?

list::push_front() is an inbuilt function in C++ STL which is declared in header file. push_front() is used to push/insert the element in the list container at the front i.e. at the beginning. By pushing a new element at front the already existing first element becomes the second element by making the inserted one the first and the size of the list also increased by 1.

Syntax

list_container1.push_front (type_t& value);

Parameters

This function accepts one parameter which is the value which we want to insert at the beginning of the list.

Return Value

This function returns nothing.

Example

Input: list<int> List_container= {10, 11, 13, 15};
      List_container.push_front(9);
Output:
      List = 9 10 11 13 15

Example

 Live Demo

#include <iostream>
#include <list>
using namespace std;
int main(){
   list<int> myList{};
   myList.push_front(10);
   myList.push_front(20);
   myList.push_front(30);
   myList.push_front(40);
   myList.push_front(50);
   myList.sort();
      cout<<"Elements in the list are : ";
   for (auto i = myList.begin(); i!= myList.end(); ++i)
      cout << ' ' << *i;
}

Output

If we run the above code it will generate the following output −

Elements in the list are : 10 20 30 40 50

What is list::push_back()?

list::push_back() is an inbuilt function in C++ STL which is declared in header file. push_back() is used to push/insert the element in the list container at the front i.e. at the ending. By pushing a new element at end the already existing last element becomes the second last element by making the inserted element as first and the size of the list also increased by 1.

Syntax

list_container1.push_front (type_t& value);

Parameters

This function accepts one parameter which is the value which we want to insert at the beginning of the list.

Return Value

This function returns nothing.

Example

Input: list<int> List_container= {10, 11, 13, 15};
      List_container.push_back(9);
Output:
      List = 10 11 13 15 9

Example

 Live Demo

#include <iostream>
#include <list>
using namespace std;
int main(){
   list<int> myList{};
   myList.push_back(10);
   myList.push_back(20);
   myList.push_back(30);
   myList.push_back(40);
   myList.push_back(50);
   myList.sort();
      cout<<"Elements in the list are : ";
   for (auto i = myList.begin(); i!= myList.end(); ++i)
      cout << ' ' << *i;
}

Example

If we run the above code it will generate the following output −

Elements in the list are :10 20 30 40 50

Updated on: 06-Mar-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements