Doubly Ended Queue Code

//Doubly Ended Queue

#include<stdio.h>
#include<process.h>
int front=0,rear=-1,q[5];
int main()
{
 void insert(int);int deletion();void display();
 int x,data,ch;
 printf("\n\n***MENU***\n\n1.Insertion\n\n2.Deletion\n\n3.Display\n\n4.Exit\n\n");
 do
 {
  printf("\n\nEnter your choice\n\n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    printf("\n\nEnter data to insert in Queue\n\n");
                scanf("%d",&data);
    insert(data);
    break;
    case 2:
     x=deletion();
     printf("\n\nDeleted data is %d\n\n",x);
    /*here if you remove break; statememt from program then after deletion
    it will move to case 3 and call dispaly function*/
     case 3:
      display();
      break;
      case 4:
       exit(1);
       default:
        printf("\n\nWrong Choice Try again\n\n");
       
  }
 }while(ch!=4);
 
}
void insert(int x)
{
 int p;
 if(front==0&&rear==4)
 {
 printf("\n\nDQueue is Overflow\n");
 }
 else
 {
  printf("\nPress 1/2 to insert at Rear_end/Front_end\n\n ");
  scanf("%d",&p);
  if(p==1)
  {
   if(rear<4)
   {
     rear++;
                q[rear]=x;
   }
   else
   printf("\n\nCan not insert at Rear_end\n\n");
     }
       else if(p==2)
      {
         if(front>0)
       {
     front--;
     q[front]=x;
      }
       else
       printf("\n\nCan not insert at Front_end\n\n");
      }
    }
}

int deletion()
{
 int x,p;
 if(rear==front-1)
 {
  printf("\n\nDQueue is Underflow\n\n");
 }
 else
 {
  printf("Press 1/2 to Delete at Rear_end/Front_end\n\n");
  scanf("%d",&p);
  if(p==1)
  {
  x=q[rear];
  rear--;
  return(x); 
  }
  /* here no need to write else part because if rear==front-1 is false then it means there is atlest one element 
  in the queue to delete*/
  else if(p==2)
  {
   x=q[front];
   front++;
   return(x);
  }
 }
}


void display()
{
 
 int i;
 
  if(rear==front-1)
  printf("\n\nDQueue is Underflow\n\n");
       printf("DQueue is  ");
 for(i=front;i<=rear;i++)
 {
  printf("%d  ",q[i]);
 }
}

No comments:

Post a Comment