Skip to content
Related Articles

Related Articles

Improve Article

Difference between forward list and list in C++

  • Difficulty Level : Easy
  • Last Updated : 08 Jul, 2021

Forward list 
Forward list is a sequence container that allows unidirectional sequential access to its data. It contains data of the same type. In STL, it has been implemented using Singly Linked List, requires constant time for insertion and deletion. Elements of the forward list are scattered in the memory and the ordering is maintained by associating every element of the list by the next element of the list via a link. Thus, it makes efficient use of memory. It has been introduced from the C++11 version.
Implementation of Forward list: 
 

C++




#include <bits/stdc++.h>
using namespace std;
int main()
{
    // Declaring forward list
    forward_list<int> flist1;
 
    // Assigning values using assign()
    flist1.assign({ 10, 12, 13, 15 });
 
    // Displaying forward list
    cout << "The elements of forward list are : ";
    for (auto a : flist1)
        cout << a << " ";
    return 0;
}
Output: 
The elements of forward list are : 10 12 13 15

 

List 
List is also a sequence container that allows bidirectional sequential access to its data. It contains data of the same type. In STL, it has been implemented using Doubly Linked List and it requires constant time for insertion and deletion. It allows non-contiguous memory allocation. Each element of the list is associated with a link to the element following it and preceding it. It is extensively used in sorting algorithm because of its constant insertion and deletion time and bidirectional sequential access.
Implementation of list: 
 

C++




#include <bits/stdc++.h>
using namespace std;
int main()
{
    // Declaring a list
    list<int> list1;
 
    // Assigning values using assign()
    list1.assign({ 1, 2, 10, 15 });
 
    // Displaying list
    cout << "The elements of list are : ";
    for (auto a : list1)
        cout << a << " ";
    return 0;
}
Output: 
The elements of list are : 1 2 10 15

 

Tabular difference between forward list and list:



 

Forward listList
Implemented using Singly Linked ListImplemented using Doubly Linked List
Consumes relatively less memoryConsumes relatively more memory
Less overhead in insertion and removal elements due to less pointer per node, thus it gives better performance.More overhead in insertion and removal elements due to more pointer per node, thus it gives poor performance.
Sequential access in forward directionSequential access in both forward and reverse direction
Generally used when bidirectional sequential access is needed like for implementing chaining in hashing, adjacency list representation of graph, etc.Generally used when unidirectional sequential access is needed like for implementing binary tree, hash table, stack, etc.

 

 

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.



My Personal Notes arrow_drop_up
Recommended Articles
Page :