C++ Program to Implement Dequeue



A deque is a double-ended queue linear data structure where elements can be added or removed from both the front and rear ends. Unlike a standard queue which is FIFO. A dequeue can function in both FIFO and LIFO modes.

Application of Dequeue

Deques are useful in a situation where you need to add or remove an element from both ends, such as in buffer implementation managing undo/redo functions or implementing certain algorithms.

Deque Operations

Deque supports the following operations:

  • insert_at_beg(): inserts an item at the front of Dequeue.
  • insert_at_end(): inserts an item at the rear of Dequeue.
  • delete_fr_beg(): Deletes an item from front of Dequeue.
  • delete_fr_rear(): Deletes an item from rear of Dequeue.

C++ Program to Implement Dequeue

Here, we are implementing the dequeue in C++:

Open Compiler
#include <iostream> using namespace std; #define SIZE 10 class dequeue { int a[SIZE], f, r; public: dequeue(); void insert_at_beg(int); void insert_at_end(int); void delete_fr_front(); void delete_fr_rear(); void show(); }; dequeue::dequeue() { f = -1; r = -1; } void dequeue::insert_at_end(int i) { if (r >= SIZE - 1) { cout << "\nInsertion not possible, overflow!" << endl; } else { if (f == -1) { f = 0; } r++; a[r] = i; cout << "\nInserted at end: " << a[r] << endl; } } void dequeue::insert_at_beg(int i) { if (f == -1) { f = 0; r = 0; a[f] = i; cout << "\nInserted at beginning: " << i << endl; } else if (f > 0) { f--; a[f] = i; cout << "\nInserted at beginning: " << i << endl; } else { cout << "\nInsertion at beginning not possible, overflow!" << endl; } } void dequeue::delete_fr_front() { if (f == -1) { cout << "Deletion not possible: dequeue is empty" << endl; } else { cout << "Deleted from front: " << a[f] << endl; if (f == r) { f = r = -1; } else { f++; } } } void dequeue::delete_fr_rear() { if (f == -1) { cout << "Deletion not possible: dequeue is empty" << endl; } else { cout << "Deleted from rear: " << a[r] << endl; if (f == r) { f = r = -1; } else { r--; } } } void dequeue::show() { if (f == -1) { cout << "Dequeue is empty" << endl; } else { cout << "Dequeue elements: "; for (int i = f; i <= r; i++) { cout << a[i] << " "; } cout << endl; } } int main() { dequeue d; d.insert_at_end(10); d.insert_at_end(20); d.insert_at_beg(5); d.insert_at_beg(2); d.show(); d.delete_fr_front(); d.delete_fr_rear(); d.show(); d.insert_at_end(30); d.insert_at_beg(1); d.show(); d.delete_fr_front(); d.delete_fr_rear(); d.delete_fr_front(); d.delete_fr_rear(); d.show(); return 0; }

The above code generates the following output:

Inserted at end: 10

Inserted at end: 20
    
Insertion at beginning not possible, overflow!
    
Insertion at beginning not possible, overflow!
Dequeue elements: 10 20 
Deleted from front: 10
Deleted from rear: 20
Dequeue is empty
    
Inserted at end: 30
    
Insertion at beginning not possible, overflow!
Dequeue elements: 30 
Deleted from front: 30
Deletion not possible: dequeue is empty
Deletion not possible: dequeue is empty
Deletion not possible: dequeue is empty
Dequeue is empty
Updated on: 2025-05-22T18:14:56+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements