Friday, April 17, 2009

QUEUE USING ARRAYS

/* queue using arrays*/
#include
#include
#define max 5
int front=-1,rear=-1;
int q[max];
void main()
{
int rpt=1;
int ch;
clrscr();
while(rpt)
{
printf("1.qinsert\n");
printf("2.qdelete\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:qinsert();
break;
case 2:qdelete();
break;
case 3:display();
break;
case 4:return;
}
printf("\nDo you want to continue press 1 for yes and 0 for no\n");
scanf("%d",&rpt);
}
}

qinsert()
{
int num;
if(rear==(max-1))
{
printf("queue is full\n");
return;
}
else
{
printf("enter the element\n");
scanf("%d",&num);
rear=rear+1;
q[rear]=num;
if(front==-1)
front=0;
}
}

qdelete()
{
int num;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
num=q[front];
printf("Deleted number is %d",num);
if(front==rear)
front=rear=-1;
else
front++;
}
}

display()
{
int i;
if(front==-1)
{
printf("queue is empty\n");
return;
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d\n",q[i]);
}
}
}

No comments: