Open In App

How to Create Deque of List in C++?

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, deque (double-ended queue) is a linear data structure that allows insertion and deletion at both ends. A list is a sequential container that implements a doubly linked list to store the data in non-contiguous storage. In this article, we will learn how to create a deque of a list in C++.

Example:

Input:
list1 = {1,2,3}
list2 = {4,5,6}
list3 = {7,8,9}

Output: 
Deque of Lists:
list1 :  1 2 3 
list2 : 4 5 6 
list2 : 7 8 9 

Creating Deque of List in C++

To create a deque of lists in C++, we can pass the type of the deque as the std::list at the time of deque declaration. This allows us to store multiple lists within a deque as its elements.

Syntax to Create Deque of List in C++

deque<list<dataType>> dequeList;

here,

  • dataType: It is the type of elements stored in the lists.
  • dequeList: Name of the deque variable used to hold the lists.

C++ Program to Create Deque of List

The below program demonstrates how we can create deque of list in C++:

C++
// C++ Program to demonstrates how we can create deque of
// list
#include <deque>
#include <iostream>
#include <list>
using namespace std;

int main()
{

    // Initializing a deque of lists
    deque<list<int> > dequeList;

    // Initializing lists with few elements
    list<int> list1 = { 1, 2, 3 };
    list<int> list2 = { 4, 5, 6 };
    list<int> list3 = { 7, 8, 9 };

    // Adding lists to the deque
    dequeList.push_back(list1);
    dequeList.push_back(list2);
    dequeList.push_back(list3);

    // Accessing and printing elements from the deque of
    // lists
    cout << "Deque of Lists:" << endl;
    int i = 1;
    for (auto& list : dequeList) {
        cout << "list" << i << ": ";
        for (int num : list) {
            cout << num << " ";
        }
        cout << endl;
        i++;
    }
    return 0;
}

Output
Deque of Lists:
list1: 1 2 3 
list2: 4 5 6 
list3: 7 8 9 

Time Complexity: O(N*M), where N is the number of lists and M is the average number of elements in each list.
Space Complexity: O(N*M)




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads