How to Create a Stack of Lists in C++? Last Updated : 18 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a list is a sequence container that allows dynamic insertion and deletion operations, whereas a stack is a data structure that follows last-in, first-out (LIFO). In this article, we will learn how to create a stack of lists in C++. Example: Input: list1 = { 1, 2, 3, 4 }list2 = { 5, 6, 7 }Output: Stack of Lists: [ { 1, 2, 3, 4 }, { 5, 6, 7 } ]Stack of Lists in C++We can create a stack of lists in C++ by passing the std::list type as the template argument during the declaration of the stack. Then we can push the lists in the stack of lists using the std::stack::push() function. Syntax to Declare Stack of Lists in C++stack<list<datatype>> stack_name; Here, datatype denotes the type of data stored in the list.stack_name is the name of the stack of lists.C++ Program to Create Stack of ListThe below program demonstrates how we can create a stack of list in C++ STL. C++ // C++ Program to illustrate how to create a stack of lists #include <iostream> #include <list> #include <stack> using namespace std; int main() { // Define the type of list typedef list<int> ListType; // Initialize two lists ListType list1 = { 1, 2, 3 }; ListType list2 = { 4, 5, 6 }; // Create a stack of lists stack<ListType> myStack; myStack.push(list1); myStack.push(list2); // Print the stack of lists while (!myStack.empty()) { ListType& topList = myStack.top(); for (auto& element : topList) { cout << element << ", "; } cout << endl; myStack.pop(); } return 0; } Output4, 5, 6, 1, 2, 3, Time Complexity: O(N), here N is the number of lists.Auxilliary Space: O(N * M), here Comment More infoAdvertise with us Next Article How to Create a Stack of Lists in C++? H heysaiyad Follow Improve Article Tags : C++ Programs C++ STL cpp-stack cpp-list CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Create a Stack of Multisets in C++? In C++, a stack is a container adapter that operates in a LIFO (Last In First Out) element order and allows insertion and deletion from the end only. A multiset is a container that stores elements in an ordered manner and allows multiple instances of an element. In this article, we will learn how to 2 min read How to Create a Stack of Map in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::map is an associative container that stores key-value pairs. In this article, we will learn how to create a stack of a map in C++. Example:Input: myMap = { {1: âaâ}, {2: âbâ}, {3: âcâ} }; myMap = { {4 2 min read How to Create a Stack of Multimap in C++? In C++, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. A multimap is a container that stores key-value pairs in an ordered manner. In this article, we will learn how to c 2 min read How to Create a Stack of Set in C++? In C++ STL, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. Sets are a type of associative container in which each element is unique and in some sorted order. In this arti 2 min read How to Create a Stack of Stack in C++? In C++, the stack is a container that follows the LIFO (Last In, First Out) order in which the elements are inserted and removed from it. In this article, we will learn how to create a stack of a stack in C++. Example:Input:Elements in stack1= 1, 2, 3, 4Elements in stack2= 5, 6, 7Output:Elements in 2 min read How to Create a Stack of Queue in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::queue is a container that follows the FIFO (First In, First Out) rule. In this article, we will learn how to create a stack of a queue in C++. Example: Input:myQueue = { a, b, c };myQueue = { d, e };O 2 min read How to Create a Stack of Pairs in C++? In C++, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. A pair is a simple container that stores data in a key and value format. In this article, we will learn how to crea 2 min read How to Create a Stack of Deque in C++? In C++, the stack is a container in which new elements are added from one end (top) and removed from that end only whereas a deque (double-ended queue) are sequence container with the feature of expansion and contraction on both ends. In this article, we will learn how to create a stack of deque in 2 min read How to Create a Stack of Arrays in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar 2 min read How to Create a Stack of Unordered_Multiset in C++? In C++, the stack is a container in which new elements are added from one end (top) and removed from that end only. In this article, we will learn how to create a stack of unordered_multiset in C++. Example: Input: mySet1 = { âappleâ, âbananaâ, âappleâ } mySet2 = { âorangeâ, âmangoâ, âorangeâ } Outp 2 min read Like