0% found this document useful (0 votes)
4 views9 pages

Circular Queue _Practice

The document provides C code implementations for managing circular queues in various scenarios, including a general queue, an online food order queue, and a patient management system in a hospital emergency room. Each implementation includes functions for enqueueing, dequeueing, displaying contents, and checking if the queue is full or empty. The circular queue structure allows for efficient reuse of space as elements are added and removed.

Uploaded by

kuchbhirakho45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Circular Queue _Practice

The document provides C code implementations for managing circular queues in various scenarios, including a general queue, an online food order queue, and a patient management system in a hospital emergency room. Each implementation includes functions for enqueueing, dequeueing, displaying contents, and checking if the queue is full or empty. The circular queue structure allows for efficient reuse of space as elements are added and removed.

Uploaded by

kuchbhirakho45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Circular Queue Basic code

#include <stdio.h>
#include <stdlib.h>

#define MAX 5

typedef struct queue {


int f;
int r;
int data[MAX];
} *que;

// 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;

int choice, value, x;

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;
}

// Function to insert an element into the circular queue


void enqueue(que q, int value) {
if (isfull(q))
printf("Queue Overflow\n");
else
{
if (isempty(q))
{
q->f = q->r = 0;
}
else
{
q->r = (q->r + 1) % MAX;
}
q->data[q->r] = value;
}
}

// Function to remove an element from the circular queue


int dequeue(que q)
{
int x = -1;
if (isempty(q))
{
printf("Queue Underflow\n");
}
else
{
x = q->data[q->f];
if (q->f == q->r)
{
q->f = q->r = -1;
}
else
{
q->f = (q->f + 1) % MAX;
}
}
return x;
}

// Function to display the circular queue


void display(que q)
{
int i;
if (isempty(q))
{
printf("Queue is empty\n");
}
else {
printf("Queue elements are:\n");
i = q->f;
while (1) {
printf("%d\t", q->data[i]);
if (i == q->r)
break;
i = (i + 1) % MAX;
}
printf("\n");
}
}

| Feature | Linear Queue | Circular Queue |


| --------------- | ----------------------------------- | --------------------------------- |
| Full Condition | `rear == MAX - 1` | `(front == (rear + 1) % MAX)` |
| Empty Condition | `front == rear` (adjusted manually) | `front == -1` |
| Rear Movement | `rear++` | `rear = (rear + 1) % MAX` |
| Front Movement | `front++` | `front = (front + 1) % MAX` |
| Space Usage | Poor (no reuse of space) | Efficient (reuses freed-up space)|
2 At a cloud kitchen, online orders are handled in a round-robin fashion using a circular queue. You
must:
 Add an order (by order number)
 Serve the next order
 Display all pending orders
 Show the front and rear orders (for kitchen and delivery updates)

#include <stdio.h>
#include <stdlib.h>

#define MAX 5

typedef struct queue {


int f, r;
int data[MAX];
} *que;

int isfull(que q) {
return (q->f == (q->r + 1) % MAX);
}

int isempty(que q) {
return (q->f == -1);
}

void enqueue(que q, int value) {


if (isfull(q))
printf("Order queue is full. Please wait...\n");
else {
if (isempty(q))
q->f = q->r = 0;
else
q->r = (q->r + 1) % MAX;
q->data[q->r] = value;
printf("Order number %d added to the queue.\n", value);
}
}

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;

int choice, orderNum;

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:

1. Add a new patient to the queue if space is available.


2. Remove a patient from the front of the queue once they have been treated.
3. Display the list of all currently waiting patients with their details.

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;
}

void enqueuePatient(que q, Patient p) {


if (isempty(q)) {
q->f = q->r = 0;
} else {
q->r = (q->r + 1) % MAX;
}
q->data[q->r] = p;
}

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;
}

printf("\nCurrent Patients in Queue:\n");


printf("ID\tName\tAge\tSeverity\n");
int i = q->f;
while (1) {
printf("%d\t%s\t%d\t%d\n", q->data[i].id, q->data[i].name, q->data[i].age, q->data[i].severity);
if (i == q->r) break;
i = (i + 1) % MAX;
}
}

void showFrontRearPatient(que q) {
if (isempty(q)) {
printf("Queue is empty.\n");
return;
}

printf("\nNext Patient (Front): %d - %s\n", q->data[q->f].id, q->data[q->f].name);


printf("Last Patient (Rear): %d - %s\n", q->data[q->r].id, q->data[q->r].name);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy