DS – Double Ended Queue

Deque:

  • It is a linear data structure.
  • Using linear queue, we can insert element from one end and remove the elements from another end.
  • In double ended queue, we can perform insert and delete operations from both the ends.
  • It is also called Deque.

Operations of DEQUE:

  1. create(): Define and initialize the queue with default values
  2. isEmpty(): Returns a boolean true value if the queue is empty
  3. isFull(): Determines whether the queue is full or not with boolean value
  4. insertFront(): Inserting element at front of Deque
  5. insertRear(): Insert an element at the rear end of the queue
  6. deleteRear(): Delete the rear element
  7. deleteFront(): Delete the front element
  8. traverse(): Display elements of Queue

Declaration:

            #define size 7
            int deque[size];
            int front=-1 , rear=-1 ;

Notes:

  • Operating queue from front means – we use ‘front’ variable for insertions and deletions.
  • Operating queue from rear means – we use ‘rear’ variable.

Check the Queue is Empty or Not:

Inserting First element and Deleting the Last element:

  • We use front and rear variables to represent insertions from Front or Rear locations.
  • When the Queue has only one last element then front and read pointing to the same location only.

Queue is Full condition:

If the rear is pointing to last location of queue and we try to insert, then starts inserting elements from the front location.

Function to check the deque is full or not?

int  isFull()
{
            if((f==0 && r==si ze-1) || (f == r + 1))
                        return 1;
            else
                        return 0;
}
int isFull()
{
            return ((rear+1)%size == front)  ;
}

Deleting elements:

Way of inserting and deleting elements from both the ends as follows:

Insert Rear:

Delete Rear:

Insert Front:

Scroll to Top