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

64 lines
1.3 KiB
C

#include <stdio.h>
#define MAX_SIZE 5
int queue[MAX_SIZE];
int front = 0;
int rear = 0;
void enqueue(int data) {
if ((rear + 1) % MAX_SIZE == front) {
printf("Queue is full\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("1. Enqueue\n2. Dequeue\n3. Print Queue\n4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
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;
}
}
}