MIT-Curricular/DS/C/Lab/Week4/circq.c

68 lines
1.5 KiB
C
Raw Normal View History

2024-08-12 22:43:32 +05:30
#include <stdio.h>
2024-08-13 11:27:43 +05:30
#define MAX_SIZE 4
2024-08-12 22:43:32 +05:30
int queue[MAX_SIZE];
int front = 0;
int rear = 0;
void enqueue(int data) {
if ((rear + 1) % MAX_SIZE == front) {
2024-08-13 11:27:43 +05:30
printf("\n\nQueue is full\n\n\n");
2024-08-12 22:43:32 +05:30
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) {
2024-08-13 11:27:43 +05:30
printf("Choose the Circular Queue option you would like to perform:\n");
2024-08-12 22:43:32 +05:30
printf("1. Enqueue\n2. Dequeue\n3. Print Queue\n4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
2024-08-13 11:27:43 +05:30
printf("Enter the value for the latest element: ");
2024-08-12 22:43:32 +05:30
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;
2024-08-13 11:27:43 +05:30
default:
printf("Invalid choice.");
return -1;
2024-08-12 22:43:32 +05:30
}
}
}