MIT-Curricular/DS/C/Lab/Week4/circq.c
2024-08-13 11:27:43 +05:30

68 lines
1.5 KiB
C

#include <stdio.h>
#define MAX_SIZE 4
int queue[MAX_SIZE];
int front = 0;
int rear = 0;
void enqueue(int data) {
if ((rear + 1) % MAX_SIZE == front) {
printf("\n\nQueue is full\n\n\n");
return;
}
queue[rear] = data;
rear = (rear + 1) % MAX_SIZE;
}
int dequeue() {
if (front == rear) {
printf("Queue is empty\n");
return -1;
}
int data = queue[front];
front = (front + 1) % MAX_SIZE;
return data;
}
void printQueue() {
if (front == rear) {
printf("Queue is empty\n");
return;
}
int i = front;
while (i != rear) {
printf("%d ", queue[i]);
i = (i + 1) % MAX_SIZE;
}
printf("\n");
}
int main() {
int choice, data;
while (1) {
printf("Choose the Circular Queue option you would like to perform:\n");
printf("1. Enqueue\n2. Dequeue\n3. Print Queue\n4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value for the latest element: ");
scanf("%d", &data);
enqueue(data);
break;
case 2:
data = dequeue();
if (data != -1) printf("Dequeued: %d\n", data);
break;
case 3:
printQueue();
break;
case 4:
return 0;
default:
printf("Invalid choice.");
return -1;
}
}
}