Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

C++ Program to Implement Forward List in STL



Forward List is a type of singly linked list available in the C++ STL (Standard Template Library). In this artcile, we will learn how to use forward_list from C++ STL library.

What is Forward List?

Forward list is a linear data structure which allows traversal only in one direction. It is similar to a singly linked list where each element points to the next element and moves in forward direction. It supports operations like insertion, deletion, and traversal from the beginning to end but not in reverse direction. The insertion and deletion operations are comparatively very fast in forward_list as there is linking involved.

Functions of forward_list Library

Below is a list of commonly used functions for a forward list with their descriptions:

  • push_front(): Inserts an element at the beginning of the list.
  • pop_front(): Removes the first element from the list.
  • insert_after(): Inserts elements after a specified position.
  • erase_after(): Removes elements after a specified position.
  • begin(): Returns an iterator pointing to the first element.
  • end(): Returns an iterator pointing past the last element.
  • clear(): Removes all the elements from the list.
  • empty(): Checks whether the list is empty or not.
  • sort(): Sorts the elements in ascending order.
  • reverse(): Reverses the order of elements in the list.
  • unique(): Removes consecutive duplicate elements.
  • swap(): Exchanges the contents of two forward_lists.

Steps to Implement Forward List in C++ STL

Following are steps/algorithm to implement a forward list using C++ STL:

  • Create a forward list using std::forward_list.
  • Insert elements using push_front() or insert_after().
  • Remove elements using pop_front() or erase_after().
  • Access and display elements using a loop with iterators.

C++ Program to Implement Forward List using STL

The below code is implemention of the above algorithm in C++ language.

#include <iostream>
#include <forward_list>
using namespace std;

void displayList(forward_list<int>& fl) {
    for (int x : fl)
        cout << x << " ";
    cout << endl;
}

int main() {
    forward_list<int> fl;

    // Insert elements at front
    fl.push_front(30);
    fl.push_front(20);
    fl.push_front(10);

    cout << "Forward List after push_front: ";
    displayList(fl);

    // Insert after first element
    auto it = fl.begin();
    fl.insert_after(it, 15);

    cout << "After insert_after first element: ";
    displayList(fl);

    // Remove element after first
    fl.erase_after(it);

    cout << "After erase_after first element: ";
    displayList(fl);

    // Remove first element
    fl.pop_front();

    cout << "After pop_front: ";
    displayList(fl);

    return 0;
}

Output

Forward List after push_front: 10 20 30 
After insert_after first element: 10 15 20 30 
After erase_after first element: 10 20 30 
After pop_front: 20 30 

Time and Space Complexity

  • Time Complexity:
  • Insertion and deletion from front: O(1)
  • Insertion and deletion after a given position: O(1)
  • Traversal: O(n)
  • Space Complexity: O(n), where n is the number of elements in the forward list
Updated on: 2025-05-06T18:20:36+05:30

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements