The Wayback Machine - https://web.archive.org/web/20211031113750/https://www.geeksforgeeks.org/listfront-and-listback-in-cpp-stl/
Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

list::front() and list::back() in C++ STL

  • Difficulty Level : Basic
  • Last Updated : 24 Sep, 2021

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::front()

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

This function is used to reference the first element of the list container. This function can be used to fetch the first element of a list.

Syntax :  



listname.front()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the first element of the list container.

Examples: 

Input  : list list{1, 2, 3, 4, 5};
         list.front();
Output : 1

Input  : list list{0, 1, 2, 3, 4, 5};
         list.front();
Output : 0

Errors and Exceptions 

  1. If the list container is empty, it causes undefined behavior
  2. It has a no exception throw guarantee if the list is not empty

C++




// CPP program to illustrate
// Implementation of front() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    list<int> mylist{ 1, 2, 3, 4, 5 };
    cout << mylist.front();
    return 0;
}

Output: 

1
list::back()

This function is used to reference the last element of the list container. This function can be used to fetch the first element from the end of a list.

Syntax : 

listname.back()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the last element of the list container.

Examples:  

Input  : list list{1, 2, 3, 4, 5};
         list.back();
Output : 5

Input  : list list{1, 2, 3, 4, 5, 6};
         list.back();
Output : 6

Errors and Exceptions  

  1. If the list container is empty, it causes undefined behavior
  2. It has a no exception throw guarantee if the list is not empty

C++




// CPP program to illustrate
// Implementation of back() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    list<int> mylist{ 1, 2, 3, 4, 5 };
    cout << mylist.back();
    return 0;
}

Output: 

5

Application 
Given an empty list of integers, add numbers to the list, then print the difference between the first and the last element.

Input: 1, 2, 3, 4, 5, 6, 7, 8
Output:7
Explanation: Last element = 8, First element = 1, Difference = 7

Algorithm 
1. Add numbers to the list using push_front() or push_back() function 
2. Compare the first and the last element. 
3. If first element is larger, subtract last element from it and print it. 
4. Else subtract first element from the last element and print it.

C++




// CPP program to illustrate
// application Of front() and back() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
    list<int> mylist{};
    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
 
    if (mylist.front() > mylist.back()) {
        cout << mylist.front() - mylist.back();
    }
    else if (mylist.front() < mylist.back()) {
        cout << mylist.back() - mylist.front();
    }
    else
        cout << "0";
}

Output: 

7

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :