Circular Queue _Practice
Circular Queue _Practice
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
// Function declarations
void enqueue(que q, int value);
int dequeue(que q);
void display(que q);
int isfull(que q) {
return (q->f == (q->r + 1) % MAX);
}
int isempty(que q) {
return (q->f == -1);
}
int main() {
que q = (que)malloc(sizeof(struct queue));
q->f = -1;
q->r = -1;
while (1) {
printf("\nCircular Queue Operations Menu:\n");
printf("1. Enqueue (Insert)\n");
printf("2. Dequeue (Remove)\n");
printf("3. Display Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(q, value);
break;
case 2:
x = dequeue(q);
if (x != -1)
printf("Data deleted is %d\n", x);
break;
case 3:
display(q);
break;
case 4:
free(q);
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int isfull(que q) {
return (q->f == (q->r + 1) % MAX);
}
int isempty(que q) {
return (q->f == -1);
}
int dequeue(que q) {
if (isempty(q)) {
printf("No orders to serve.\n");
return -1;
} else {
int x = q->data[q->f];
if (q->f == q->r)
q->f = q->r = -1;
else
q->f = (q->f + 1) % MAX;
return x;
}
}
void display(que q) {
if (isempty(q))
printf("No pending orders.\n");
else {
printf("Pending Orders: ");
int i = q->f;
while (1) {
printf("%d ", q->data[i]);
if (i == q->r)
break;
i = (i + 1) % MAX;
}
printf("\n");
}
}
void showFrontRear(que q) {
if (isempty(q))
printf("No orders pending.\n");
else
printf("Next order to prepare: %d, Last order added: %d\n", q->data[q->f], q->data[q->r]);
}
int main() {
que q = (que)malloc(sizeof(struct queue));
q->f = -1; q->r = -1;
while (1) {
printf("\nOnline Food Order Queue Menu:\n");
printf("1. Add Order\n");
printf("2. Serve Order\n");
printf("3. Display Orders\n");
printf("4. Show Next and Last Order\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter Order Number to add: ");
scanf("%d", &orderNum);
enqueue(q, orderNum);
break;
case 2:
orderNum = dequeue(q);
if (orderNum != -1)
printf("Serving order number: %d\n", orderNum);
break;
case 3:
display(q);
break;
case 4:
showFrontRear(q);
break;
case 5:
free(q);
printf("Exiting the order system.\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
3 Patient Queue Management at a Hospital Emergency Room
A hospital emergency room needs an efficient system to manage the flow of patients during
peak hours. Each patient arriving at the emergency room must be registered with their
details including a unique Patient ID, Name, Age, and Severity level of their condition (on a
scale of 1 to 5, with 5 being the most severe). The hospital uses a circular queue to manage
this flow of patients so that once a slot becomes available (due to a patient being treated and
removed from the queue), it can be reused efficiently. The queue has a fixed capacity, and
the hospital staff should be able to perform the following operations:
Design and implement a C program using structures and circular queue logic to manage this
system. Use a structure to represent each patient and simulate the queue operations with a
menu-driven interface.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
typedef struct Patient {
int id;
char name[30];
int age;
int severity; // 1 to 5, where 5 is most critical
} Patient;
typedef struct {
int f;
int r;
Patient data[MAX];
} *que;
// Function declarations
void enqueuePatient(que q, Patient p);
Patient dequeuePatient(que q);
void displayPatients(que q);
void showFrontRearPatient(que q);
int isfull(que q);
int isempty(que q);
int isfull(que q) {
return ((q->r + 1) % MAX == q->f);
}
int isempty(que q) {
return (q->f == -1);
}
int main() {
que q = (que)malloc(sizeof(struct queue));
q->f = -1;
q->r = -1;
int choice;
Patient temp;
while (1) {
printf("\nHospital Emergency Room - Patient Queue Menu:\n");
printf("1. Add Patient\n");
printf("2. Attend Patient\n");
printf("3. Display All Patients\n");
printf("4. Show Next and Last Patient\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (isfull(q)) {
printf("Queue is full. Cannot add new patient.\n");
} else {
printf("Enter Patient ID: ");
scanf("%d", &temp.id);
printf("Enter Name: ");
scanf("%s", temp.name);
printf("Enter Age: ");
scanf("%d", &temp.age);
printf("Enter Severity Level (1 to 5): ");
scanf("%d", &temp.severity);
enqueuePatient(q, temp);
}
break;
case 2:
if (isempty(q)) {
printf("No patient to attend. Queue is empty.\n");
} else {
temp = dequeuePatient(q);
printf("Attending Patient: %d, %s\n", temp.id, temp.name);
}
break;
case 3:
displayPatients(q);
break;
case 4:
showFrontRearPatient(q);
break;
case 5:
free(q);
printf("Exiting the system.\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
Patient dequeuePatient(que q) {
Patient temp = {0, "", 0, 0};
if (!isempty(q)) {
temp = q->data[q->f];
if (q->f == q->r) {
q->f = q->r = -1;
} else {
q->f = (q->f + 1) % MAX;
}
}
return temp;
}
void displayPatients(que q) {
if (isempty(q)) {
printf("Queue is empty.\n");
return;
}
void showFrontRearPatient(que q) {
if (isempty(q)) {
printf("Queue is empty.\n");
return;
}