Code For Double-Ended Queue (DQUEUE)


#include<stdio.h>
#include<conio.h>

void insert_left(int q[],int *f,int  x)
{
 if(*f<=0)
 {
printf("insertion is not possible\n");
 }
 else
 {
  *f=*f-1;
  q[*f]=x;
 }
}
int delete_left(int  q[],int *f,int *r)
{
int a;
 if(*f==-1)
  return 0;
 else
 {
  if(*f==*r)
  {
   *f=-1;
   *r=-1;
  }
  else
  {
   a=q[*f];
   *f=*f+1;
  }
  return a;
 }
}
void insert_right(int q[],int n,int *f,int *r,int x)
{
 if(*r>=n-1)
printf("queue is overflow\n");
 else
 {
  *r=*r+1;
  q[*r]=x;
 }
 if(*f==-1)
  *f=0;
}

int delete_right(int q[],int  *f,int  *r)
{
int  a;
  if(*r==-1)
   return 0;
  else
  {
   if(*f==*r)
   {
    *f=-1;
    *r=-1;
   }
   else
   {
    a=q[*r];
    *r=*r-1;
   }
    return a;
  }
}

void display(int q[],int n,int *f,int *r)
{
int i;
 if(*f==-1)
printf("queue is empty");
 else
 {
printf("\nqueue contains:\n");
  if(*f<=*r)
  {
   for(i=*f;i<=*r;i++)
   {
printf("%d\t",q[i]);
   }
  }
  else
  {
   for(i=0;i<=*r;i++)
   {
printf("%d\t",q[i]);
   }
   for(i=*f;i<=n-1;i++)
   {
printf("%d\t",q[i]);
   }
  }
 }
}

void main()
{
int q[5],n,*f,*r,j,k;
 n=5;
 *f=-1;
 *r=-1;
clrscr();
 again:
printf("\n1. insert left\n2. delete left\n3. insert right\n4.delete right\n5. display\n6.exit\n");
printf("enter your choice:\n");
scanf("%d",&k);
 switch(k)
 {

  case 1:
printf("enter value to be inserted\n");
scanf("%d",&j);
insert_left(q,f,j);
  display(q,n,f,r);
goto again;

  case 2:
  j=delete_left(q,f,r);
  if(j==0)
printf("queue is underflow");
  else
  display(q,n,f,r);
goto again;

  case 3:
printf("enter value to be inserted\n");
scanf("%d",&j);
insert_right(q,n,f,r,j);
  display(q,n,f,r);
goto again;

  case 4:
  j=delete_right(q,f,r);
  if(j==0)
printf("queue is empty");
  else
  display(q,n,f,r);
goto again;

  case 5:
  display(q,n,f,r);
goto again;

case 6:
 goto exit;
 }
exit:
}

Comments

Post a Comment

Popular posts from this blog

Code for BUBBLE Sort

Code for Circular Linked List c-programing

Code for SELECTION Sort in C-Programming