
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
#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
- Related Articles
- list::front() and list::back() in C++ STL
- stack push() and pop() in C++ STL
- list front() function in C++ STL
- queue::push() and queue::pop() in C++ STL
- list back() function in C++ STL
- Program to implement a queue that can push or pop from the front, middle, and back in Python
- queue::front() and queue::back() in C++ STL
- deque front( ) and deque back( ) in C++ in STL
- Program to convert linked list by alternating nodes from front and back in Python
- list begin( ) and list end( ) in C++ STL
- list::emplace_front() and list::emplace_back() in C++ STL
- list::pop_front() and list::pop_back() in C++ STL
- Push and slice multiple times in MongoDB?
- Reversing the alphabet from back to front and front to back in JavaScript
- MongoDB $push in nested array?
