How to Add an Element at the Front of a List in C++? Last Updated : 04 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the STL has a doubly linked list container defined as the std::list class template inside the <list> header. It stores the sequential data in non-contiguous memory locations. In this article, we will learn how to add an element at the beginning of a list in C++ STL. Example: Input: myList = {2, 3, 4, 5} Output: Original List Elements: 2 3 4 5 Updated List Elements: 1 2 3 4 5Add an Element at the Beginning of the List in C++To add an element at the beginning of a std::list in C++, we can use the std::list::push_front() method that inserts the element at the beginning of the list, hence increasing its size by one. We just need to pass the value to be added as an argument. Syntax to std::list::push_front()list_name.push_front(value)Here, list_name denotes the name of the list where the element will be added.value is the element that will be added to the beginning of the list.C++ Program to Add an Element at the Beginning of a ListThe below example demonstrates how we can use the push_front() function to add an element at the beginning of a list in C++. C++ // C++ program to demonstrate how we can use // the push_front() function to add an element at the // beginning of a list #include <iostream> #include <list> using namespace std; int main() { // Create a list with some elements list<int> myList = { 2, 3, 4, 5 }; // Print the original list elements cout << "Original List Elements:"; for (const auto& elem : myList) { cout << " " << elem; } cout << endl; // Add an element at the beginning of the list myList.push_front(1); // Print the updated list elements cout << "Updated List Elements:"; for (const auto& elem : myList) { cout << " " << elem; } cout << endl; return 0; } OutputOriginal List Elements: 2 3 4 5 Updated List Elements: 1 2 3 4 5 Time Complexity: O(1)Auxilary Space: O(1) Comment More infoAdvertise with us Next Article How to Add an Element at the Front of a List in C++? G gauravggeeksforgeeks Follow Improve Article Tags : C++ Programs C++ STL cpp-list CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Add an Element at the End of List in C++? In C++, the Standard Template Library (STL) has a doubly-linked list container, known as std::list that offers various functions to manipulate elements. In this article, we will learn how to add an element at the end of a list in C++ STL. Example: Input: myList = {1, 2, 3}; Output: List after Elemen 2 min read How to Add an Element at the End of a Deque in C++? In C++, a deque is a flexible data structure that supports the insertion and deletion of elements from both ends efficiently. In this article, we will learn how to add an element at the end of a deque in C++. Example: Input: myDeque ={10,20,30,40} Output: // added element 50 at the end myDeque ={10, 2 min read How to Add Element at Front of a Deque in C++? In C++, a deque is a vector like data structure that allows users to add and remove elements from both the front and the back ends. In this article, we will learn how to add an element at the beginning of a deque in C++. Example: Input: myDeque: {1, 2, 3} Output: myDeque = {11, 1, 2, 3}Add an Elemen 2 min read How to Remove an Element from a List in C++? In C++, the STL provides a std::list container that represents a doubly linked list to store the sequential data in non-contiguous memory locations. In this article, we will learn how to remove an element from a list in C++. Example: Input: myList = {1, 2, 3, 4, 5, 6, 7, 8} Target = 5 Output: // rem 2 min read How to Add an Element to a Set in C++? In C++ STL, a set is a container that stores unique elements in a sorted order. In this article, we will learn how to add an element to a set in C++ STL. Example: Input: mySet = {1, 2, 4, 5, 8} Element to add: 3 Output: mySet = {1, 2, 3, 4, 5, 8}Add an Element to a Set in C++To add a specific elemen 2 min read Like