/*Practical No.
5 :Write a C Program to simulate the working of a
circular queue of integers using an array. provide the following
operations.
1.Insert 2.Delete 3.Display */
/*Roll No. : NLT19/BCA/ */
#include<stdio.h>
#include<stdlib.h>
#define QUE_SIZE 5
void addq(int *arr, int item, int *count, int *rear);
void delq(int *arr, int *front, int *count);
void display (int * arr, int *front, int *count);
int main(void){
int arr[QUE_SIZE];
int item, choice, count, front, rear;
front = 0;
rear = -1;
count = 0;
for(;;){
printf("\n\nEnter Your Choice\n 1. Add\t\t 2. Delete\n 3.
Display\t 4. Exit\n-> ");
scanf("%d", &choice);
switch(choice){
case 1:
printf("Enter item to add: ");
scanf("%d", &item);
addq(arr, item, &count, &rear);
break;
case 2:
delq(arr, &front, &count);
break;
case 3:
display(arr, &front, &count);
break;
case 4:
exit(0);
default:
printf("Enter correct choice\n");
}
}
return 0;
}
/* adds an element to the queue */
void addq(int *arr, int item, int *count, int *rear){
if(*count == QUE_SIZE){
printf("\nThe Queue is full\n");
return;
}
*rear = (*rear+1) % QUE_SIZE;
arr[*rear] = item;
(*count)++;
return;
}
/* removes an element from the queue */
void delq(int *arr, int *front, int *count){
int data;
if(*count == 0){
printf("\nQueue is empty.");
return;
}
data = arr[*front];
printf("Item deleted: %d", data);
*front = (*front + 1) % QUE_SIZE;
(*count)--;
return;
}
/* displays element in a queue */
void display (int *arr, int *front, int *count){
int i, f;
if(*count == 0){
printf("\nQueue is empty.");
return;
}
printf("\nElements in the circular queue:\n");
for(i = 1, f = *front; i <= *count; i++){
printf("%d\t", arr[f]);
f = (f+1) % QUE_SIZE;
}
printf("\n");
}