Listpush Front And Listpush Back In C++ STL

We know that the C++ programming language is blessed with the pre-defined implementation of Data Structures and Algorithms in its Standard Template Library (STL); if not present, we need to write the whole code, which takes a lot of compilation time for the compiler and a time-consuming process for the programmer sitting opposite to the PC.

The list is one such important Data Structure which is treated as a Container helping us to store the data, which can be of any data type like int, char, string, long, short etc. The pushfront operation helps us add the elements to the List Data Structure we have created, and the pushback function allows us to add up the features or the data from the back of the List Data Structure.

Syntax

Example

listname.push_front(value)

C++ code

Example

// Here we are writing down the C++ programming language code to demonstrate 
// the concept of list::push_front() and list::push_back() in C++ STL 
#include <iostream>
#include <list>
using namespace std;
// The main driver code functionality starts from here 
int main()
{
    // the below code snippet helps us with creating a List Data structure
    // in C++ programming language STL (Standard Template Library)
    list<int> mylist{ 1, 2, 3, 4, 5 };
    //the below code snippet helps us with pushing the data to the list
    mylist.push_front(6);
 
    // Now list becomes 6, 1, 2, 3, 4, 5
    // Now the list contains the elements 6, 1, 2, 3, 4, 5
    
    // now we are using the auto function available for us in the C++
    // programming language Standard Template Library helping us to print data
 
    for (auto it = mylist.begin(); it != mylist.end(); ++it)
        cout << ' ' << *it;
        // end of the C++ programming language code
}

Output:

Output

/ t m  p  / d J J Y Z f g 6J u.o
// Output of the List created
6 1 2 3 4 5

C++ code

Example

// Here we are writing down the C++ programming language code to demonstrate
// the concept of list::push_front() and list::push_back() in C++ STL 
#include <iostream>
#include <list>
using namespace std;
// The main driver code functionality starts from here 
int main()
{
    // the below code snippet helps us with creating a List Data structure
    // in C++ programming language STL (Standard Template Library)

	list<int> mylist{};
	//the below code snippet helps us push the data to the list
	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);

	// Now list becomes 6 7 24 43 45 58 89
	// Now, we shall implement the sorting function

	mylist.sort();
	
	// now we are using the auto function available for us in the C++
    // programming language Standard Template Library helping us to print 
    //data

	for (auto it = mylist.begin(); it != mylist.end(); ++it)
		cout << ' ' << *it;
		        // end of the C++ programming language code
}

Output:

Output

/ t m  p  / d J J Y Z f g 6J u.o
6 7 24 43 45 58 89

C++ code

Example

// Here we are writing down the C++ programming language code to demonstrate
// the concept of list::push_front() and list::push_back() in C++ STL 
#include <iostream>
#include <list>
using namespace std;
// The main driver code functionality starts from here 
int main()
{
    // the below code snippet helps us with creating a List Data structure
    // in C++ programming language STL (Standard Template Library)

	list<int> mylist{ 1, 2, 3, 4, 5 };
	//The code snippet below helps us push the data to the list
	mylist.push_back(6);

	// Now the list becomes 1, 2, 3, 4, 5, 6
	
	
	
    // now we are using the auto function available for us in the C++
    // programming language Standard Template Library helping us to print 
    //data

	for (auto it = mylist.begin(); it != mylist.end(); ++it)
		cout << ' ' << *it;
		// end of the C++ programming language code
}

Output:

Output

/ t m  p  / d J J Y Z f g 6J u.o
1 2 3 4 5 6

C++ code

Example

// Here we are writing down the C++ programming language code to demonstrate
// the concept of list::push_front() and list::push_back() in C++ STL 
#include <iostream>
#include <list>
using namespace std;
// The main driver code functionality starts from here 
int main()
{
    // the below code snippet helps us with creating a List Data structure
    // in C++ programming language STL (Standard Template Library)
	list<int> mylist{};
	//The code snippet below helps us push the data to the list
	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);

	// Now the list becomes 6 7 24 43 45 58 89
	// Now, we shall implement the sorting function

	mylist.sort();
	
	// now we are using the auto function available for us in the C++
    // programming language Standard Template Library helping us to print 
    //data
	for (auto it = mylist.begin(); it != mylist.end(); ++it)
		cout << ' ' << *it;
		// end of the C++ programming language code
}

Output:

Output

6 7 24 43 45 58 89

Input Required

This code uses input(). Please provide values below: