#include <stdio.h>
#include <stdlib.h>
#define QUEUE_LENGTH 10
int enqueue(int *queue, int *front, int *rear)
{
if (*rear == QUEUE_LENGTH - 1)
{
printf("Queue is full.\n");
}
else if (*rear == -1 && *front == -1)
{
*rear = 0;
*front = 0;
printf("Give a number: ");
int num;
scanf("%i", &num);
queue[*rear] = num;
}
else
{
printf("Give a number: ");
int num;
scanf("%i", &num);
queue[++(*rear)] = num;
}
}
int dequeue(int *queue, int *front, int *rear)
{
if (*front == -1 && *rear == -1)
{
printf("Queue is empty.\n");
}
else if (*front == *rear)
{
printf("Removing %i...\n", queue[*front]);
queue[*front] = 0;
*front = -1;
*rear = -1;
}
else
{
printf("Removing %i...\n", queue[*front]);
queue[(*front)++] = 0;
}
}
int print_queue(int *queue)
{
printf("[ ");
for (int i = 0; i < QUEUE_LENGTH; i++)
{
printf("%i ", queue[i]);
}
printf("]\n");
}
int main()
{
int queue[QUEUE_LENGTH] = {0};
int front = -1;
int rear = -1;
while (1)
{
int choice;
do
{
printf("\tENQUEUE (0)\n");
printf("\tDEQUEUE (1)\n");
printf("\tPRINT (2)\n");
printf("\tEXIT (3)\n");
printf("\tGive choice: ");
scanf("%i", &choice);
} while (choice < 0 || choice > 3);
switch (choice)
{
case 0:
enqueue(queue, &front, &rear);
break;
case 1:
dequeue(queue, &front, &rear);
break;
case 2:
print_queue(queue);
break;
default:
exit(0);
}
}
}