Thursday 9 August 2012

implementation of circular queue using arrays in c language

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX 5
int queue[MAX],front=0,rear=0;
void insertion()
{
if(front==(rear+1)%MAX)
printf("\n queue is full..cannot insert ");
else
{
rear=(rear+1)%MAX;
printf("\n Enter the element to insert in the queue : ");
scanf("%d",&queue[rear]);
}
}
void deletion()
{
if(front==0&&rear==0)
printf("\n queue is empty...nothing to delete ");
else
{
printf("\n deleted element is : %d ",queue[front]);
front=(front+1)%MAX;
}
if(front==rear)
{
front=0;
rear=0;
}
}
void display()
{
int i;
if(front==0&&rear==0)
printf("\n queue is empty...nothing to display ");
else
{
if(front==0)
front++;
for(i=front;i!=rear;i=(i+1)%MAX)
{
printf("%d ",queue[i]);
}
printf("%d ",queue[i]);
}
}
void length()
{
int length=0,i;
if(front==0&&rear==0)
length=0;
else
{
for(i=front;i!=rear;i=(i+1)%MAX)
length++;
}
printf("\n length of the queue is : %d",length);
}
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n 1.insertion\n 2.deletion\n 3.display\n 4.length\n 5.exit");
printf("\n Enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
length();
break;
case 5:
exit(0);
default:
printf("\n invalid choice...try again");
}
}
}

2 comments:

  1. dispaly function have a bug!!........insert an element then delete it then display the queue it wont give right output

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete