Double Ended Queue

DQueue or Double Ended Queue is a generalized version of a queue data structure that allows insert and delete at both ends.

Depends on requirement or operation it’s classified into 2 types 

  1. input restricted dqueue

  2. output restricted dqueue

Input restricted dqueue

An inputrestricted dqueue is one where deletion can be made from both ends, but insertion can be made at one end.

Output restricted dqueue

An output-restricted dqueue is one where insertion can be made from both ends, but deletion can be made at one end only.

Double ended queue in c using an array

#include<stdio.h>
#include <process.h>

#define MAX 5
int dqueue[MAX];
int left=-1;
int right=-1;

void insert_right();
void insert_left();
void delete_left();
void delete_right();
void display_que();

void input_que()
{
  int choice;
  while(1)
  {
    printf("\n1.Insert at right: ");
    printf("\n2.Delete from left: ");
    printf("\n3.Delete from right: ");
    printf("\n4.Display: ");
    printf("\n5.Return to main ");
    printf("\n6.Quit ");
    printf("\nEnter your choice:");
    scanf("%d",&choice);
    switch(choice)
    {
      case 1:   insert_right();
    break;
      case 2:	delete_left();
    break;
      case 3:	delete_right();
    break;
      case 4:	display_que();
    break;
      case 5:   return;
      case 6:	exit(1);
      default:printf("\Wrong choice:");
    }
  }//while
}  

void output_que()
{
    int choice;
    while(1)
    {
       printf("\n1.Insert at right: ");
       printf("\n2.Insert at left : ");
       printf("\n3.Delete from left: ");
       printf("\n4.Display: ");
       printf("\n5.Return to main ");
       printf("\n6.Quit ");
       printf("\nEnter your choice:");
       scanf("%d",&choice);
       switch(choice)
       {
     case 1:insert_right();
      break;
     case 2:insert_left();
      break;
     case 3:delete_left();
      break;
     case 4:display_que();
      break;
     case 5:return;
     case 6:exit(1);
     default:printf("\Wrong choice:");
       }
    }
  }
int main()
{
   int option;
   while(1)
   {
     printf("\n1.input restricted dequeue  :");
     printf("\n2.Output restricted dequeue :");
     printf("\n3.Exit......................:");
     printf("\nEnter your choice:");
     scanf("%d",&option);
     switch(option)
     {
  case 1:	input_que();
    break;
  case 2:	output_que();
    break;
        case 3: return 0;
  
        default:
 		printf("\nWrong choice");
     }//switch
   }//while
} //main

void insert_right()
{
   int data;
   if( (left==0&&right==MAX-1)||(left==right+1) )
   {
  printf("\nQueue Overflow:");
  return;  
   }
   if(left==-1)
   {
  left=0;
  right=0;
   }
   else if(right==MAX-1)
     return;
   else
     right=right+1;
   printf("\nEnter Data:");
   scanf("%d",&data);
   dqueue[right]=data;
}
void insert_left()
{
   int data;
   if( (left==0&&right==MAX-1)||(left==right+1) )
   {
      printf("\nQueue Overflow");
      return;
   }
   if(left==-1)
   {
      left=0;
      right=0;
   }
   else if(left==0)
    left=MAX-1;
   else
    left=left-1;
   
   printf("\nEnter Data: ");
   scanf("%d",&data);
   dqueue[left]=data;
}
void delete_left()
{
  if(left==-1)
  {
     printf("Queue Underflow");
     return;
  }
  printf("\nDeleted Data is:%d",dqueue[left]);
  if(left==right)
  {
      left=-1;
      right=-1;
  }
  else if(left==MAX-1)
  left=0;
  else
   left=left+1;

}
void delete_right()
{
   if(left==-1)
   {
     printf("Queue Underflow ");
     return;
   }
   printf("\nDeleted data is:%d",dqueue[right]);
   if(left==right)
   {
     left=-1;
     right=-1;
   }
  else if(right==0)
  right=MAX-1;
  else
   right=right-1;
}
void display_que()
{
   int fposition=left,rposition=right;
   if(left==-1)
   {
      printf("Queue is empty " );
      return;
   }
   printf("\nQueue elements " );
   if(fposition<=rposition)
   {
       while(fposition<=rposition)
       {
     printf(" %d",dqueue[fposition]);
     fposition++;
       }
   }
   else
   {
  while(fposition<=MAX-1)
  {
     printf("%d",dqueue[fposition]);
     fposition++;
  }
  fposition=0;
  while(fposition<=rposition)
  {
     printf("%d",dqueue[fposition]);
     fposition++;
  }
   }
}










Leave a Reply

Your email address will not be published. Required fields are marked *