2/23/2009

C Program To Perform Insert, Delete And Display The Elements Using Circular Queue


/* C Program To Perform Insert, Delete And Display The Elements Using Circular Queue */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define queue_size 4
int item,q[20],f=0,r=-1,count=0;
void insertion();
void deletion();
void display();
void main()
{
 int choice;
 clrscr();
 for(;;)
 {
  printf("1:Insert 2:Delete 3:display 4:exit\n");
  printf("Enter your choice\n");
   scanf("%d",&choice);
  switch(choice)
  {
   case 1:
    printf("Enter the item to be inserted\n");
     scanf("%d",&item);
    insertion();
    break;
   case 2:
    deletion();
    break;
   case 3:
    display();
    break;
   default:
    exit(0);
  }
 getch();
 }
}

void insertion()
{
 if(count==queue_size)
  {
  printf("Queue Overflow\n");
  return;
  }
 r=(r+1)%queue_size;
 q[r]=item;
 count++;
}

void deletion()
{
 if(count==0)
  {
  printf("Queue is underflow\n");
  return;
  }
 printf("Deleted item=%d\n",q[f]);
 f=(f+1)%queue_size;
 count--;
}

void display()
{
 int i,j;
 if(count==0)
  {
  printf("Queue is Empty\n");
  return;
  }
 else
 {
 i=f;
 printf("Content of the circular queue\n");
 for(j=1;j<=count;j++)
  {
  printf("%d\n",q[i]);
  i=(i+1)%queue_size;
  }
 printf("%d\n");
 }
}
Out Put:
C Program To Perform Insert, Delete And Display The Elements Using Circular Queue

No comments:

Post a Comment