#include<stdio.h>
#define SIZE 5
typedef struct
{
int front,rear,arr[SIZE];
}queue;
queue q;
void init()
{
q.front=q.rear=-1;
}
int isEmpty()
{
if(q.front==-1)
return 1;
return 0;
}
int isFull()
{
if(q.rear==(SIZE-1))
return 1;
return 0;
}
void enqueue(int element)
{
if(isFull())
{
printf("\nQueue is full");
return;
}
if(isEmpty())
q.front++;
q.arr[++q.rear] = element;
}
int dequeue()
{
int element = q.arr[q.front];
if(isEmpty())
{
printf("\nQueue underflow");
return -1;
}
else
{
if(q.front == q.rear)
init();
else
q.front++;
return element;
}
}
void display()
{
int i;
if(isEmpty())
{
printf("\nQueue is Empty");
return;
}
printf("\n");
for(i=q.front;i<=q.rear;i++)
printf("%d ",q.arr[i]);
}
int main()
{
int ch,element;
init();
do
{
printf("\n1.Enqueue 2.Dequeue 3.Display \nEnter choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter element to be enqueued ");
scanf("%d",&element);
enqueue(element);
break;
case 2:
element = dequeue();
if(element!=-1)printf("\nElement dequeued = %d",element);
break;
case 3:
display();
break;
}
printf("\nContinue ? ");
fflush(stdin);
}while(getchar()=='y');
return 0;
}
No comments:
Post a Comment