C Program For Circular Queue

 /*Circular Queue*/

Press Alt+R To Execute

#include<stdio.h>
#include<conio.h>

#define size 5

int front=-1;
int rear=0;

void insert(int[]);
void Delete(int[]);
void display(int[]);
int isempty(void);
int isfull(void);

void main(void)
{
int a[size],ch;
clrscr();
do
{
printf("\n............MENU............\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Isempty\n");
printf("5.Isfull\n");
printf("6.Exit\n");
printf("Enter the choice");
scanf("%d",&ch);
 switch(ch)
 {
 case 1:insert(a);
  break;
 case 2:Delete(a);
  break;
 case 3:display(a);
  break;
 case 4:isempty();
  break;
 case 5:isfull();
  break;
 default:
 printf("Enter the correct Option\n");
 }

}while(ch!=6);
}


void insert(int x[size])
{
 if (isfull()==1)
 {
 printf("\nCircular Q is full\n");
 }
 else
 {
  if (front==-1)
  {
  front=rear=0;
  }
  else
  {
  rear=(rear+1)%size;
  }
 printf("Enter the value\n");
 scanf("%d",&x[rear]);
 }

}


void Delete(int x[size])
{
 if (isempty()==1)
 {
 printf("Circular Q is empty\n");
 }
 else
 {
 printf("Element to be deleted is %d\n",x[front]);
  if (front==rear)
  {
  front=rear=-1;
  }
  else
  {
  front=(front+1)%size;
  }

 }
}

void display(int x[size])
{
int i;
 if (isempty()==1)
 {
 printf("There is no element to be displayed\n");
 }
 else
 {
 i=front;
  while(i!=rear)
  {
  printf("%d\n",x[i]);
  i=(i+1)%size;
  }
 printf("%d\n",x[i]);
 }

}

int  isempty(void)
{
 if (front==-1)
 {
 printf("Circular Q is empty\n");
 return 1;
 }
 else
 {
 return 0;
 }

}


int  isfull()
{
 if (front==(rear+1)%size)
 {
 printf("Circular Q is full\n");
 return 1;
 }
 else
 {
 return 0;
 }
}

Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment