Double Ended Queue (Deque)

In a standard queue, insertion can only be done from the back and deletion only from the front. A double-ended queue allows for insertion and deletion from both ends of the queue.

It has four functions to manipulate the data insertion and deleteion.

Insert at the front

void insert_front()
{	int added_item;
	if((left == 0 && right == MAX - 1) || (left == right + 1))
	{	printf("Queue Overflow \n");
		return;	 }
	if (left == -1)/*If queue is initially empty*/
	{	left = 0;
		right = 0;	 
	}
	else if(left== 0)
		left = MAX - 1;
	else
		left = left - 1;
	printf("Input the element for adding in queue : ");
	scanf("%d", &added_item);
	deque_arr[left] = added_item ;	 
}

Insert at the rear

void insert_rear()
{
	int added_item;
	if ((left == 0 && right == MAX - 1) || (left == right + 1))
	{	printf("Queue Overflow\n");
		return;}
	if (left == -1)  /* if queue is initially empty */
	{	left = 0;
		right = 0;
	}
	else if(right == MAX - 1)  /*right is at last position of queue */
		right = 0;
	else
		right = right+1;
	printf("Input the element for adding in queue : ");
	scanf("%d", &added_item);
	deque_arr[right] = added_item ;
}

Delete from the front

void delete_front()
{	if (left == -1)
	{	printf("Queue Underflow\n");
		return ;	}
	printf("Element deleted from queue is : %d\n",deque_arr[left]);
	if(left == right) /*Queue has only one element */
	{	
		left = -1;
		right = -1;
	}
	else if(left == MAX-1)
		left = 0;
	else
		left = left+1;
}

Delete from the rear

void delete_rear()
{
if (left == -1)
	{
	printf("Queue Underflow\n");
		return ;
	}
	printf("Element deleted from queue is : %d\n", deque_arr[right]);
	if(left == right) /*queue has only one element*/
	{	
		left = -1;
		right=-1;	 
	}
	else if(right == 0)
		right = MAX - 1;
	else
		right = right - 1;	
}

A dequeue also has 2 specific types of queues:

Input Restricted Queues

In Input Restricted Queues, insertion takes place only from the rear end, deletion can take place from both ends.

Output Restricted Queues

In Output Restricted Queues, the deletion takes place from only the front, however, insertion can take place from both ends.

Output Restricted Queue
Vegpuff/Wikipedia [CC BY-SA 3.0] from Wikimedia Commons

The practical applications of a deque are insignificant in programming.

Scroll to top

By using this website you agree to accept our Privacy Policy and Terms and Conditions