Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists.
pop_front() function is used to pop or remove elements from a list from the front. The value is removed from the list from the beginning, and the container size is decreased by 1. Syntax :
listname.pop_front() Parameters : No argument is passed as parameter. Result : Removes the value present at the front of the given list named as listname
Examples:
Input : list list{1, 2, 3, 4, 5}; list.pop_front(); Output : 2, 3, 4, 5 Input : list list{5, 4, 3, 2, 1}; list.pop_front(); Output : 4, 3, 2, 1
Errors and Exceptions
- No-Throw-Guarantee – if an exception is thrown, there are no changes in the container.
- If the list is empty, it shows undefined behaviour.
// CPP program to illustrate // pop_front() function #include <iostream> #include <list> using namespace std;
int main()
{ list< int > mylist{ 1, 2, 3, 4, 5 };
mylist.pop_front();
// list becomes 2, 3, 4, 5
for ( auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
} |
Output:
2, 3, 4, 5
Application : Input an empty list with the following numbers and order using push_front() function and print the reverse of the list.
Input : 1, 2, 3, 4, 5, 6, 7, 8 Output: 8, 7, 6, 5, 4, 3, 2, 1
// CPP program to illustrate // application Of pop_front() function #include <iostream> #include <list> using namespace std;
int main()
{ list< int > mylist{}, newlist{};
mylist.push_front(8);
mylist.push_front(7);
mylist.push_front(6);
mylist.push_front(5);
mylist.push_front(4);
mylist.push_front(3);
mylist.push_front(2);
mylist.push_front(1);
// list becomes 1, 2, 3, 4, 5, 6, 7, 8
while (!mylist.empty()) {
newlist.push_front(mylist.front());
mylist.pop_front();
}
for ( auto it = newlist.begin(); it != newlist.end(); ++it)
cout << ' ' << *it;
} |
Output:
8, 7, 6, 5, 4, 3, 2, 1
pop_back() function is used to pop or remove elements from a list from the back. The value is removed from the list from the end, and the container size is decreased by 1. Syntax :
listname.pop_back() Parameters : No argument is passed as parameter. Result : Removes the value present at the end or back of the given list named as listname
Examples:
Input : list list{1, 2, 3, 4, 5}; list.pop_back(); Output : 1, 2, 3, 4 Input : list list{5, 4, 3, 2, 1}; list.pop_back(); Output : 5, 4, 3, 2
Errors and Exceptions
- No-Throw-Guarantee – if an exception is thrown, there are no changes in the container.
- If the list is empty, it shows undefined behaviour.
// CPP program to illustrate // pop_back() function #include <iostream> #include <list> using namespace std;
int main()
{ list< int > mylist{ 1, 2, 3, 4, 5 };
mylist.pop_back();
// list becomes 1, 2, 3, 4
for ( auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
} |
Output:
1, 2, 3, 4
Application : Input an empty list with the following numbers and order using push_front() function and print the reverse of the list.
Input : 1, 20, 39, 43, 57, 64, 73, 82 Output: 82, 73, 64, 57, 43, 39, 20, 1
// CPP program to illustrate // application Of pop_back() function #include <iostream> #include <list> using namespace std;
int main()
{ list< int > mylist{}, newlist{};
mylist.push_front(82);
mylist.push_front(73);
mylist.push_front(64);
mylist.push_front(57);
mylist.push_front(43);
mylist.push_front(39);
mylist.push_front(20);
mylist.push_front(1);
// list becomes 1, 20, 39, 43, 57, 64, 73, 82
while (!mylist.empty()) {
newlist.push_back(mylist.back());
mylist.pop_back();
}
for ( auto it = newlist.begin(); it != newlist.end(); ++it)
cout << ' ' << *it;
} |
Output:
82, 73, 64, 57, 43, 39, 20, 1
Let us see the differences in a tabular form -:
list::pop_front() | list::pop_back() | |
1. | It is used to remove the first element in the list container | It is used to remove the last element in the list container |
2. |
Its syntax is -: pop_front(); |
Its syntax is -: pop_back(); |
3. | It does not take any parameters. | It does not take any parameters. |
4. | It does not have any return type. | It does not have any return type. |
5. | Its complexity is constant. | Its complexity is constant. |