![]() |
VOOZH | about |
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 } ]
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.
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.The below program demonstrates how we can create a stack of list in C++ STL.
4, 5, 6, 1, 2, 3,
Time Complexity: O(N), here N is the number of lists.
Auxilliary Space: O(N * M), here