MIT-Curricular/DS/C/endsem-omnibus/queues.c

80 lines
1.6 KiB
C

#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
void enqueue() {
int item;
if(rear == MAX-1) {
printf("\nQueue is Full");
}
else {
printf("\nEnter element to insert: ");
scanf("%d", &item);
if(front == -1 && rear == -1) {
front = rear = 0;
}
else {
rear++;
}
queue[rear] = item;
printf("\nItem inserted successfully");
}
}
void dequeue() {
if(front == -1 || front > rear) {
printf("\nQueue is Empty");
}
else {
printf("\nDeleted element is: %d", queue[front]);
if(front == rear) {
front = rear = -1;
}
else {
front++;
}
}
}
void display() {
int i;
if(front == -1 || front > rear) {
printf("\nQueue is Empty");
}
else {
printf("\nQueue elements are:\n");
for(i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
}
}
int main() {
int choice;
while(1) {
printf("\n\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nInvalid Choice");
}
}
return 0;
}