Page

How to implement Enqueue & Dequeue using switch case

#include<stdio.h>
#include<conio.h>
#define max 5
void enqueue();
void dequeue();
void traverse();
int queue[max];
int item;
int front=-1;
int rear=-1;
void main()
{
int choice;
clrscr();
do
{
printf("\n1 : Enqueue\n2 : Dequeue\n3: Traverse\n");
printf("Enter Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
traverse();
break;
case 4:
exit(0);
}
}
while(choice<4);
getch();
}
void enqueue()
{
if((front==0&&rear==max-1)||front==max)
{
printf("\nQueue is Overflow\n");
}
else
{
if(front==-1)
{
front=front+1;
}
else
{
printf("Enter the data in Queue : ");
scanf("%d",&item);
rear=rear+1;
queue[rear]=item;
}
}
}
void dequeue()
{
if(front==-1||front==rear+1)
{
printf("Queue is Under flow\n");
}
else
{
front=front+1;
}
};
void traverse()
{
int temp;
if(front==-1||front==rear+1)
{
printf("Queue is Empty\n");
}
else
{
temp=front;
while(temp!=rear+1)
{
printf("\n%d",queue[temp]);
temp=temp+1;
}
}
}


No comments:

Post a Comment