Program to implement Circular queue operations using Structure


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

struct queue
{
 int front;
 int rear;
 int queue[max];
} q;

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

 main()
{
 int c1,c2;
 clrscr();
 q.front=q.rear=-1;

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(q.rear==max-1 &&q.front==0)
 printf("\nQueue is full");

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

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

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

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

 else
 q.rear=q.rear+1;

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

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

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

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

  else
  q.front=q.front+1;

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

void display()
{
 int i;

 printf("Displaying the items\n");

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

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

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

 }
}

0 comments:

Post a Comment