List Push Front Function

C++ List push_front

C++ List push_front function adds a new element in the beginning of the list. Therefore, increasing the size of the list by one.

push_front(0) function adds 0 element in the beginning.

Syntax

Suppose an element is 'x':

Example

void push_front(const value_type& x);

Parameter

x : It is the value to be inserted in the beginning of the list.

Return value

It does not return any value.

Example

Let's see a simple example

Example

#include <iostream>
#include<list>
using namespace std;
int main()
{
    list<int> li={20,30,40,50};
    li.push_front(10);
    list<int>::iterator itr;
    for(itr=li.begin();itr!=li.end();++itr)
    cout<<*itr<<",";
    return 0;
}

Output:

Output

10,20,30,40,50

In this example, push_front function inserts the element '10' in the beginning of the list.

Input Required

This code uses input(). Please provide values below: