List Push Front Function - C++ Programming Tutorial
C++ Course / Dynamic Programming / List Push Front Function

List Push Front Function

BLUF: Mastering List Push Front Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: List Push Front Function

C++ is renowned for its efficiency. Learn how List Push Front Function enables low-level control and high-performance computing in the tutorial below.

C++ List push_front

The push_front method in C++ List inserts a new element at the front of the list, thereby expanding the size of the list by one element.

The push_front(0) method inserts a new element with a value of 0 at the start of the collection.

Syntax

Suppose an element is 'x':

Example

void push_front(const value_type& x);

Parameter

The variable x represents the value that will be added to the start 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 instance, the push_front method adds the value '10' at the start of the linked list.

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience