Queue(Data structure) code


//QUEUE CODE

#include<stdio.h>
#include<process.h>
int front=0,rear=-1;
int x,data,ch,q[5];
int main()
{
 void insert(int);int deletion();void display();
 do
 {
  printf("\n\n***MENU***\n\n1.Insertion\n\n2.Deletion\n\n3.Display\n\n4.Exit\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)
{
 
 if(rear==4)
 {
 printf("\n\nQueue is Overflow\n");
 }
 else
 {
  rear++;
 q[rear]=x;

 }
}
int deletion()
{
 int x;
 if(rear==front-1)
 {
  printf("\n\nQueue is Underflow\n\n");
 }
 else
 {
  x=q[front];
  front++;
  return(x); 
 }
}
void display()
{
 
 int i;
 
  if(rear==front-1)
  printf("\n\nQueue is Underflow\n\n");
       printf("Queue is  ");
 for(i=front;i<=rear;i++)
 {
  printf("%d  ",q[i]);
 }
}

No comments:

Post a Comment