IMPLEMENTATION OF QUEUE


SOURCE CODE:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struct queue
{
  int que[size];
  int front,rear;
}Q;

Qfull()
{
  if(Q.rear>=size-1)
   return 1;
 else
   return 0;
}

int Insert(int data)
{
  if(Q.front==-1)
   Q.front ++;
   Q.que[++Q.rear]=data;
   return Q.rear;
}

int Qempty()
{
  if((Q.front==-1)||(Q.front>Q.rear))
    return 1;
  else
    return 0;
}

int Delete()
{
  int data;
  data=Q.que[Q.front];
  Q.front++;
  printf("\n deleted data is %d",data);
  return Q.rear;
}

void display()
{
  int i;
  for(i=Q.front;i<=Q.rear;i++)
  printf("%d\n",Q.que[i]);
}

void main()
{
 int choice,data;
 clrscr();
 Q.front=-1;
 Q.rear=-1;
 X:
 printf("\n Main Menu");
 printf("\n1.insert\n2.delete\n3.display\n4.exit\n");
 printf("\n Enter your choice:\t");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:
     if(Qfull())
      printf("\n cannot insert the element");
     else
     {
      printf("\n Enter the element to be inserted");
      scanf("%d",&data);
      Insert(data);
     }
     break;
  case 2:
     if(Qempty())
      printf("\n Queue Underflow");
     else
      Delete();
     break;
  case 3:
     if(Qempty())
      printf("\n Queue empty");
     else
      display();
     break;
  case 4:
     exit(0);
  default:
     printf("\n Wrong Choice");
     break;
 }
goto X;
}

OUTPUT:

 Main Menu
1.insert
2.delete
3.display
4.exit

 Enter your choice:     1
 Enter the element to be inserted    10

 Main Menu
1.insert
2.delete
3.display
4.exit

 Enter your choice:     1
 Enter the element to be inserted    20

Main Menu
1.insert
2.delete
3.display
4.exit

 Enter your choice:     1
 Enter the element to be inserted    30

Main Menu
1.insert
2.delete
3.display
4.exit

 Enter your choice:     3
10
20
30

Main Menu
1.insert
2.delete
3.display
4.exit

 Enter your choice:     3
 Deleted data is 10


Main Menu
1.insert
2.delete
3.display
4.exit

 Enter your choice:     3
20
30

Main Menu
1.insert
2.delete
3.display
4.exit

 Enter your choice:     4
Previous
Next Post »

Still not found what you are looking for? Try again here.