Double Ended Queue

 

Double Ended Queue


Double ended queue is a more generalized form of queue data structure which allows insertion and removal of elements from both the ends, i.e , front and back.

Double Ended Queue

Implementation of Double ended Queue

Here we will implement a double ended queue using a circular array. It will have the following methods:

  • push_back : inserts element at back
  • push_front : inserts element at front
  • pop_back : removes last element
  • pop_front : removes first element
  • get_back : returns last element
  • get_front : returns first element
  • empty : returns true if queue is empty
  • full : returns true if queue is full
// Maximum size of array or Dequeue

#define SIZE 5

class Dequeue
{
    //front and rear to store the head and tail pointers
    int  *arr;
    int front, rear;  
    
    public :

    Dequeue()
    {
    	//Create the array
        arr = new int[SIZE];

        //Initialize front and rear with -1
        front = -1;
        rear = -1;
    }
 
    // Operations on Deque
    void  push_front(int );
    void  push_back(int );
    void  pop_front();
    void  pop_back();
    int  get_front();
    int  get_back();
    bool  full();
    bool  empty();   
};

No comments:

Post a Comment