Double Ended Queue

#include<stdio.h>
#include<conio.h>
#define max 30

char queue[max];
int f=-1,r=-1;

void insert_rear();
void insert_front();
void del_rear();
void del_front();
void display();

void main()
{
   int i,n,x,y;
   printf("\nEnter no. of elements: ");
   scanf("%d",&n);
   r=n-1;
   f=0;
   printf("\nEnter %d elements: ",n);
   for(i=0;i<n;i++)
   {
      scanf("%d",&queue[i]);
   }
   do
   {
   printf("\n1.Input restriction.\n2.Output restriction.\n3.Exit\n");
   scanf("%d",&x);
   switch(x)
   {
      case 1:
         do
         {
           printf("\n1.Insert rear.\n2.Delete front.\n3.Delete rear.\n4.Display.\n5.Exit.\n");
           scanf("%d",&y);
           switch(y)
           {
              case 1:insert_rear();    break;
              case 2:del_front();      break;
              case 3:del_rear();       break;
              case 4:display();        break;
           }
         }while(y!=5);
         break;
      case 2:
         do
         {
           printf("\n1.Insert rear.\n2.Insert front.\n3.Delete front.\n4.Display.\n5.Exit.\n");
           scanf("%d",&y);
           switch(y)
           {
              case 1:insert_rear();    break;
              case 2:insert_front();   break;
              case 3:del_front();      break;
              case 4:display();        break;
           }
         }while(y!=5);
         break;
   }
   }while(x!=3);
}

void insert_rear()
{
  int m;
  if(r==max-1)
  {
   printf("\nQueue is full");
  }
  else
  {
   if(f==-1 && r==-1)
   {
      f=0;
      r=0;
   }
   else
   {
      r=r+1;
   }
   printf("\nEnter number to be inserted: ");
   scanf("%d",&m);
   queue[r]=m;
  }
}

void del_front()
{
   if(f==-1 && r==-1)
   {
      printf("\nQueue is empty.");
   }
   else
   {
      if(f==r)
      {
        f=-1;
        r=-1;
      }
      else
     f=f+1;
   }
}

void insert_front()
{
   int m;
   if(r==max-1 && f==0)
   {
      printf("\nQueue is full.");
   }
   else
   {
      f=f-1;
   }
   printf("\nEnter element : ");
   scanf("%d",&m);
   queue[f]=m;
}

void del_rear()
{
   if(f==-1 && r==-1)
   {
      printf("\nQueue is empty.");
   }
   else
   {
      r=r-1;
   }
}

void display()
{
   int i;
   printf("\nQueue is : ");
   for(i=f;i<=r;i++)
   {
      printf("%d ",queue[i]);
   }
   printf("\n");
}

/*Output

Enter no. of elements: 5

Enter 5 elements: 1 2 3 4 5

1.Input restriction.
2.Output restriction.
3.Exit
1

1.Insert rear.
2.Delete front.
3.Delete rear.
4.Display.
5.Exit.
1

Enter number to be inserted: 6

1.Insert rear.
2.Delete front.
3.Delete rear.
4.Display.
5.Exit.
2

1.Insert rear.
2.Delete front.
3.Delete rear.
4.Display.
5.Exit.
4

Queue is : 2 3 4 5 6

1.Insert rear.
2.Delete front.
3.Delete rear.
4.Display.
5.Exit.
3

1.Insert rear.
2.Delete front.
3.Delete rear.
4.Display.
5.Exit.
4

Queue is : 2 3 4 5

1.Insert rear.
2.Delete front.
3.Delete rear.
4.Display.
5.Exit.
5

1.Input restriction.
2.Output restriction.
3.Exit
2

1.Insert rear.
2.Insert front.
3.Delete front.
4.Display.
5.Exit.
1

Enter number to be inserted: 7

1.Insert rear.
2.Insert front.
3.Delete front.
4.Display.
5.Exit.
2

Enter element : 1

1.Insert rear.
2.Insert front.
3.Delete front.
4.Display.
5.Exit.
4

Queue is : 1 2 3 4 5 7

1.Insert rear.
2.Insert front.
3.Delete front.
4.Display.
5.Exit.
3

1.Insert rear.
2.Insert front.
3.Delete front.
4.Display.
5.Exit.
4

Queue is : 2 3 4 5 7

1.Insert rear.
2.Insert front.
3.Delete front.
4.Display.
5.Exit.
5

1.Input restriction.
2.Output restriction.
3.Exit
3
*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s