Lists are sequence containers available in the C++ Standard Library. They allow non-contiguous memory allocation. List containers are implemented as doubly-linked lists. Compared to other sequence containers (eg: arrays, vectors etc.), lists have a better performance when it comes to inserting, moving or deleting elements. The list::push_front()
is a method available in the STL which adds a new element at the beginning of the list container, before its current first element. It effectively increases the container size by one.
Syntax:
list_name.push_front(value);
Parameters: The list::push_front()
method accepts a single parameter:
•Element to be inserted to the list
Return value: none
Example of list::push_front() method
#include <iostream> #include <list> using namespace std; int main() { list<int> L; L.push_front(10); L.push_front(20); L.push_front(30); L.push_front(40); cout<<"List contains: \n"; for( list<int>::iterator it = L.begin(); it != L.end(); ++it) { cout<<*it<<" <--> "; } cout<<"NULL"; }
Output:
List contains: 40 <--> 30 <--> 20 <--> 10 <--> NULL