Is there anything wrong with this implementation of Queue with array?
I have started front = rear = -1.
Some references tend to start front = 0 like this:enter link description here
I think if front starts with 0, the source code becomes cumbersome.
#include <stdio.h>
#include<ctype.h>
#define SIZE 5
int queue[SIZE] = {0};
int front = -1;
int rear = -1;
void PrintQueue()
{
int i=0;
if(!IsEmpty())
{
for(i=front+1 ; i<=rear ; i++)
{
printf("%d, ", queue[i]);
}
printf("\n");
}
else
{
printf("Queue is empty.\n");
}
}
int IsEmpty()
{
if(front==rear)
{
front = rear = -1;
return 1;
}
else
{
return 0;
}
}
void Enque(int value)
{
if(rear<SIZE-1)
{
++rear;
queue[rear] = value;
}
else
{
printf("Queue overflow!\n");
}
}
int Dequeue()
{
int ret = 0;
if(!IsEmpty())
{
++front;
ret = queue[front];
}
else
{
printf("Queue underflow!\n");
ret = -99;
}
return ret;
}
main()
{
int value = 0;
int menu = 0;
while(1)
{
printf("Menu\n");
printf("--------\n");
printf("1. Enque\n");
printf("2. Deque\n");
printf("3. IsEmpty\n");
printf("4. Print\n");
printf("5. Clrscr\n");
printf("6. Exit\n");
scanf(" %d", &menu);
switch(menu)
{
case 1:
printf("Enter an element : ");
scanf(" %d", &value);
Enque(value);
break;
case 2:
printf("Dequed, %d.\n", Dequeue());
break;
case 3:
if(IsEmpty())printf("Queue is empty.\n");
else printf("Que has some values.\n");
break;
case 4:
PrintQueue();
break;
case 5:
system("cls");
break;
case 6:
exit(0);
break;
}
}
}