
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement List in STL
A List in C++ STL library is hardcoded implementation of a doubly linked list data structure. In this article, we will learn how to implement and use a list in C++ .
What is List?
List is a doubly linked container provided in the C++ STL library, in which each element points to both its previous and next elements. Meaning, in this list we can quickly traverse in both forward and backward direction using pointers just like a doubly linked list. Compared to vectors or arrays, these lists allow fast insertions and deletions from the middle of the sequence. Hence, these lists are useful in scenarios where data needs to be added or removed frequently at various positions.
Functions of list Library
The list library in C++ STL provides several functions to manipulate the list. 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.
- merge(): Merges two sorted lists into one.
- swap(): Exchanges the contents of two forward_lists.
Steps to Implement List in C++ STL
Following are steps/algorithm to implement a list using C++ STL:
- Create a list using std::list.
- Insert elements using push_back() and push_front().
- Delete elements using pop_back(), pop_front(), or remove().
- Use begin() and end() to iterate through the list.
- Access and modify elements using iterators.
C++ Program to Implement List using STL
The below code is implementation of the above algorithm in C++ language.
#include <iostream> #include <list> using namespace std; void displayList(list<int>& lst) { for (int val : lst) cout << val << " "; cout << endl; } int main() { list<int> lst; // Insert elements lst.push_back(10); lst.push_back(20); lst.push_front(5); lst.push_front(2); cout << "List after insertions: "; displayList(lst); // Remove elements lst.pop_front(); lst.pop_back(); cout << "List after deletions: "; displayList(lst); // Insert at specific position using iterator auto it = lst.begin(); advance(it, 1); lst.insert(it, 15); cout << "List after inserting 15: "; displayList(lst); // Remove specific value lst.remove(15); cout << "List after removing 15: "; displayList(lst); return 0; }
The output of the above code will be:
List after insertions: 2 5 10 20 List after deletions: 5 10 List after inserting 15: 5 15 10 List after removing 15: 5 10
Time and Space Complexity
- Time Complexity:
- Insertion and deletion from front or back: O(1)
- Insertion or deletion using iterator: O(1)
- Searching and accessing elements: O(n)
- Space Complexity: O(n), where n is the number of elements in the list