#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXZONES 5 // Define…
- User
#include <stdio.h> #include <stdlib.h> #include <string.h>
#define MAX_ZONES 5 // Define the maximum number of zones #define STACK_SIZE 50 // Define the maximum stack size for supplies
typedef struct { int id; // Zone ID int priority; // Priority of the zone (lower number means higher priority) int people; // Number of people to rescue int zoneSupplies; // Supplies specific to the zone } Zone;
typedef struct { int supplies[STACK_SIZE]; // Array to store the supplies int top; // Top index of the stack } Stack;
typedef struct Node { int time; // Time of the rescue char name[20]; // Name of the rescued person struct Node *left, *right; // Pointers for left and right children in BST } RescuedPerson;
// Function prototypes void initStack(Stack *s); int isStackEmpty(Stack *s); int canAddSupplies(Stack *s, int quantity); void addSupplies(Stack *s, int quantity); void useSupplies(Stack *s, int quantity); void addZone(Zone zones[], int *zoneCount); void heapifyDown(Zone zones[], int size); void rescue(Stack *s, Zone zones[], int *zoneCount, RescuedPerson *root); RescuedPerson insertBST(RescuedPerson *root, int time, char name[]); void displayRescued(RescuedPerson *root); void inOrder(RescuedPerson *root);
void initStack(Stack *s) { s->top = -1; } // Initialize the stack
int isStackEmpty(Stack *s) { return s->top == -1; } // Check if stack is empty
int canAddSupplies(Stack *s, int quantity) { return s->top + quantity < STACK_SIZE; } // Check if supplies can be added
// Add multiple supplies to the stack void addSupplies(Stack *s, int quantity) { if (canAddSupplies(s, quantity)) { s->top += quantity; // Directly adjust the top pointer to add supplies } else { printf("Cannot add %d more supplies, stack overflow.\n", quantity); } }
// Use (pop) multiple supplies from the stack void useSupplies(Stack *s, int quantity) { for (int i = 0; i < quantity; i++) { if (!isStackEmpty(s)) { s->top--; } else { printf("Ran out of supplies unexpectedly.\n"); return; } } }
// Add a zone with heap property maintenance void addZone(Zone zones[], int *zoneCount) { if (*zoneCount >= MAX_ZONES) { printf("Cannot add more zones.\n"); return; } int id, priority, people, zoneSupplies; printf("Enter Zone ID, Priority (1-5), People, and Zone Specific Supplies: "); scanf("%d %d %d %d", &id, &priority, &people, &zoneSupplies); Zone newZone = {id, priority, people, zoneSupplies}; int i = (*zoneCount)++; while (i > 0 && zones[(i-1)/2].priority > priority) { zones[i] = zones[(i-1)/2]; // Bubble up i = (i-1)/2; } zones[i] = newZone; // Insert new zone }
// Maintain heap by sinking down an element void heapifyDown(Zone zones[], int size) { int i = 0; while (2 * i + 1 < size) { int leftChild = 2 * i + 1; int rightChild = 2 * i + 2; int smallest = leftChild;
code - Assistant