Stack Implementation using Double Ended Queue: CSU1051P - Shoolini U

Implementation of Stack Using Double Ended Queue

To understand the implementation of a stack using a double ended queue using C++

Objective

To understand the implementation of a stack using a double ended queue using C++

#include <iostream>
#include <deque>
using namespace std;

int main() {
    deque<int> Stack;

    // Push elements
    Stack.push_back(1);
    Stack.push_back(2);
    cout << "Stack after pushes: ";
    for(auto i : Stack) {
        cout << i << " ";
    }
    cout << endl;

    // Pop element
    Stack.pop_back();
    cout << "Stack after one pop: ";
    for(auto i : Stack) {
        cout << i << " ";
    }
    cout << endl;

    // Check if stack is empty
    bool isEmpty = Stack.empty();
    cout << "Stack is empty: " << (isEmpty ? "true" : "false") << endl;

    return 0;
}

Discussion of Algorithms

  1. Start
  2. Declare a double ended queue
  3. Push an element to the top of the stack
  4. Pop an element from the top of the stack
  5. Check if the stack is empty
  6. End

Representations


Flow Diagram

   +----------------------------------+
   |                                  |
   |           Start                  |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |    Declare deque Stack           |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |        Push operations           |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |      Print stack after pushes     |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |        Pop operation             |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |     Print stack after pop         |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |    Check if stack is empty        |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |      Print stack emptiness        |
   |             |                    |
   +----------------------------------+
                  |
                  V
   +----------------------------------+
   |                                  |
   |             Exit                 |
   |             |                    |
   +----------------------------------+

Tabular Dry Run

Push | Pop | Empty | Elements
  1  |     |  No   | 1
  2  |     |  No   | 1 2
     |  2  |  No   | 1

Results/Output

Stack after pushes: 1 2
Stack after one pop: 1
Stack is empty: false