Wednesday, 8 August 2012

queue in C programming

#include<stdio.h>
#include<conio.h>
int arr[20],front=-1,rear=-1,i;
void enqueue();
void dequeue();
void display();
void main()
{
int ch;
clrscr();
do
{
printf(" [1] enqueue \n [2] dequeue \n[3] display \n[4] exit\n ");
printf("\n enter your choice ");
scanf("%d",&ch);
switch (ch)
{
case 1:
enqueue ();
break;
case 2:
dequeue ();
break;
case 3:
display ();
case 4:
break;
default:
printf("\n invalid option ");
getch();
}
}
while (ch!=4);
getch();
}
void enqueue()
{
if(rear==20-1)
{
printf("\n queue is full");
getch();
return;
}
rear++;
printf("\n enter element to enqueue ");
scanf("%d",&arr[rear]);
if(front==-1)
front++;
}
void dequeue()
{
if (front==-1)
{
printf("\nqueue is empty");
getch();
return;
}
printf("\n the deque element is %d",arr[front]);
getch();
if (front==rear)
front=rear=-1;
else
front++;
}
void display()
{
if (front==-1)
{
printf("\n the queue is empty");
getch();
return;
}
printf("\n the elements in queue are front");
for(i=front;i<=rear;i++)
printf("\n the elements in queue are %d",arr[i]);
getch();
}

No comments:

Post a Comment

There was an error in this gadget