Double Ended Queue (Deque) Implementation: CSU1051P - Shoolini U

Implementation of Queue Using Double Ended Queue

To understand the implementation of a queue using a deque using C++

Objective

To understand the implementation of a queue using a deque using C++

#include <iostream>
#include <deque>
using namespace std;
int main() {
    // Declare a deque
    deque<int> dq;

    // Insert elements at the rear
    for(int i = 1; i <= 5; i++) {
        dq.push_back(i);
    }

    // Print the queue
    cout << "The queue is: ";
    for(auto i = dq.begin(); i != dq.end(); i++) {
        cout << *i << " ";
    }
    cout << endl;

    // Pop element from the front
    dq.pop_front();

    // Print the queue after popping
    cout << "After popping front, the queue is: ";
    for(auto i = dq.begin(); i != dq.end(); i++) {
        cout << *i << " ";
    }
    cout << endl;

    return 0;
}

Algorithm

  1. Start
  2. Declare and initialize a double ended queue
  3. Add elements at the rear end
  4. Remove elements from the front
  5. End

Representations

Flow Diagram

   +----------------------------------+
   |                                  |
   |           Start                  |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |       Declare deque dq           |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |    Insert elements at the rear   |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |     Print the queue              |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |   Pop element from the front     |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   | Print the queue after popping    |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |             Exit                 |
   |             |                    |
   +----------------------------------+

Tabular Dry Run

push | pop
 1   | 
 2   | 
 3   | 
 4   | 
 5   | 
     |  1

Results/Output

The queue is: 1 2 3 4 5
After popping front, the queue is: 2 3 4 5