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
- Start
- Declare a double ended queue
- Push an element to the top of the stack
- Pop an element from the top of the stack
- Check if the stack is empty
- 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