list::pop_front() and list::pop_back() in C++ STL

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of list::pop_front() and list::pop_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 forward_list::pop_front()?

list::pop_front() is an inbuilt function in C++ STL which is declared in header f ile. pop_front() is used to pop/remove the element which is in the beginning of the list. When we use this function the first element which is already in the container is removed and the next element to the first element becomes the first element of the list container and the size of the container is decreased by 1.

Syntax

list_container1.pop_front ();

Parameters

This function accepts no parameters.

Return Value

This function returns nothing.

Example

Input: list<int> List_container= {10, 11, 13, 15};
      List_container.pop_front();
Output:
      List = 11 13 15

Example

 Live Demo

#include <iostream>
#include <list>
using namespace std;
int main(){
   list<int> myList_1 = {}, myList_2 = {};
   myList_1.push_front(10);
   myList_1.push_front(20);
   myList_1.push_front(30);
   myList_1.push_front(40);
   myList_1.push_front(50);
   while (!myList_1.empty()){
      myList_2.push_front(myList_1.front());
      myList_1.pop_front();
   }
   cout<<"Elements in the list are : ";
   for (auto i = myList_2.begin(); i!= myList_2.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::pop_back()?

list::pop_back() is an inbuilt function in C++ STL which is declared in header file. pop_back() is used to remove/pop the element from the back or the last of the list container. When we use pop_back then it removes/pops the last element and the element before the last element becomes the last element and the size of the list container is reduced by 1.

Syntax

list_container.pop_back();

Parameter

This function accepts no parameters.

Return Value

This function returns nothing.

Example

Input: list<int> List_container= {10, 11, 13, 15};
      List_container.pop_back();
Output:
      List = 10 11 13

Example

 Live Demo

#include <iostream>
#include <list>
using namespace std;
int main(){
   list<int> myList_1 = {}, myList_2 = {};
   myList_1.push_front(10);
   myList_1.push_front(20);
   myList_1.push_front(30);
   myList_1.push_front(40);
   myList_1.push_front(50);
   while (!myList_1.empty()){
      myList_2.push_front(myList_1.back());
      myList_1.pop_back();
   }
   cout<<"Elements in the list are : ";
   for (auto i = myList_2.begin(); i!= myList_2.end(); ++i)
      cout << ' ' << *i;
}

Output

If we run the above code it will generate the following output −

Elements in the list are : 50 40 30 20 10
raja
Published on 06-Mar-2020 12:01:57
Advertisements