Program to implement Circular queue operations


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5

int front=-1;
int rear=-1;
int queue[max];

void insert();
void Delete();
void display();

 main()
{
 int c1,c2;
 clrscr();

do
{
 printf("\nEnter the Operation\n");
 printf("1.INSERT 2.DELETE 3.DISPLAY 4.EXIT ");
 scanf("%d",&c1);

 switch(c1)
 {
  case 1:
  insert();
  break;
  case 2:
  Delete();
  break;
  case 3:
  display();
  break;
  case 4:
  exit(0);
  default:
  printf("\nEnter valid choice ");
 }
 printf("   [Press 1 to continue] ");
 scanf("%d",&c2);
}
while(c2==1);
if(c2!=1)
exit(0);

getch();
}

void insert()
{
 int item;
 if(rear==max-1 &&front==0)
 printf("\nQueue is full");

 else if(front==rear+1)
 printf("\nQueue is full");

 else
 {
 printf("Enter item: ");
 scanf("%d",&item);

 if(rear==-1)
 {
 rear=0;
 front=0;
 }

 else if(rear==max-1)
 rear=0;

 else
 rear=rear+1;

 queue[rear]=item;
 printf("\nThe inserted item is %d",queue[rear]);
 }
}

void Delete()
{
 int item;
 if(front==-1)
 printf("\nQueue is empty");
 else
 {
  item=queue[front];

  if(front==rear)
  front=rear=-1;

  else if(front==max-1)
  front=0;

  else
  front=front+1;

  printf("The deleted item is %d",item);
 }
}

void display()
{
 int i;

 printf("Displaying the items\n");

 if(front>rear)
 {
  for(i=front;i<max;i++)
  printf("\t%d",queue[i]);

  printf("\t%d",queue[rear]);
 }
 else
 {

  for(i=front;i<=rear;i++)
  printf("\t%d",queue[i]);

 }
}

0 comments:

Post a Comment