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.
list::push_front()
push_front() function is used to push elements into a list from the front. The new value is inserted into the list at the beginning, before the current first element and the container size is increased by 1.
Syntax :
listname.push_front(value) Parameters : The value to be added in the front is passed as the parameter Result : Adds the value mentioned as the parameter to the front of the list named as listname
Examples:
Input : list list{1, 2, 3, 4, 5};
list.push_front(6);
Output : 6, 1, 2, 3, 4, 5
Input : list list{5, 4, 3, 2, 1};
list.push_front(6);
Output :6, 5, 4, 3, 2, 1
Errors and Exceptions
- Strong exception guarantee - if an exception is thrown, there are no changes in the container.
- If the value passed as argument is not supported by the list, it shows undefined behavior.
// CPP program to illustrate
// push_front() function
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> mylist{ 1, 2, 3, 4, 5 };
mylist.push_front(6);
// list becomes 6, 1, 2, 3, 4, 5
for (auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
}
Output:
6 1 2 3 4 5
Application: Input an empty list with the following numbers and order using push_front() function and sort the given list.
Input : 7, 89, 45, 6, 24, 58, 43 Output : 6, 7, 24, 43, 45, 58, 89
// CPP program to illustrate
// application Of push_front() function
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> mylist{};
mylist.push_front(43);
mylist.push_front(58);
mylist.push_front(24);
mylist.push_front(6);
mylist.push_front(45);
mylist.push_front(89);
mylist.push_front(7);
// list becomes 7, 89, 45, 6, 24, 58, 43
// Sorting function
mylist.sort();
for (auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
}
Output:
6 7 24 43 45 58 89
list::push_back()
push_back() function is used to push elements into a list from the back. The new value is inserted into the list at the end, after the current last element and the container size is increased by 1.
Syntax :
listname.push_back(value) Parameters : The value to be added in the back is passed as the parameter Result : Adds the value mentioned as the parameter to the back of the list named as listname
Examples:
Input : list list{1, 2, 3, 4, 5};
list.push_back(6);
Output :1, 2, 3, 4, 5, 6
Input : list list{5, 4, 3, 2, 1};
list.push_back(0);
Output :5, 4, 3, 2, 1, 0
Errors and Exceptions
- Strong exception guarantee - if an exception is thrown, there are no changes in the container.
- If the value passed as argument is not supported by the list, it shows undefined behavior.
// CPP program to illustrate
// push_back() function
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> mylist{ 1, 2, 3, 4, 5 };
mylist.push_back(6);
// list becomes 1, 2, 3, 4, 5, 6
for (auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
}
Output:
1 2 3 4 5 6
Application: Input an empty list with the following numbers and order using push_back() function and sort the given list.
Input : 7, 89, 45, 6, 24, 58, 43 Output : 6, 7, 24, 43, 45, 58, 89
// CPP program to illustrate
// application Of push_back() function
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> mylist{};
mylist.push_back(7);
mylist.push_back(89);
mylist.push_back(45);
mylist.push_back(6);
mylist.push_back(24);
mylist.push_back(58);
mylist.push_back(43);
// list becomes 7, 89, 45, 6, 24, 58, 43
// Sorting function
mylist.sort();
for (auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
}
Output:
6 7 24 43 45 58 89
Let us see the differences in a tabular form -:
| list::push_front() | list::push_back() | |
| 1. | It is used to insert a new element at the beginning of the list. | It is used to add a new element at the end of the list container |
| 2. | Its syntax is -: push_front (const value_type& val); |
Its syntax is -: push_back (const value_type& val); |
| 3. | Its takes one parameter that is the value to be inserted. | Its takes one parameter that is the value to be inserted. |
| 4. | Its complexity is constant. | Its complexity is constant. |
| 5. | Its iterator validity does not changes. | Its iterator validity does not changes. |