cmkl/spring-2025/sen-107/00010/vipTaxi.c

167 lines
3.3 KiB
C

/*
* SEN-107:00010 Taxi + Rank Queue Assessment
* Based on VIP Taxi Queue
* Template for Fundamental Data Structures, skill 00010.
*
* Created by Pasin Manurangsi, 2025-01-08
* Modified by Thanawin Pattanaphol, 2025-03-02
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Variable declaration
#define MAX_NAME_LEN 25
#define MAX_QUEUE_SIZE 30
// Structs
typedef struct Party
{
char name[MAX_NAME_LEN];
int rank;
int partyCount;
struct Party* next;
} Party;
typedef struct Queue
{
Party* front;
Party* rear;
} Queue;
// Queue variable
Queue queue = { NULL, NULL };
// Queue-related functions
void enqueue(int rank, const char* name, int partyCount)
{
Party* newParty = (Party*)malloc(sizeof(Party));
strncpy(newParty->name, name, MAX_NAME_LEN - 1);
newParty->name[MAX_NAME_LEN - 1] = '\0';
newParty->partyCount = partyCount;
newParty->next = NULL;
newParty->rank = rank;
// If No queue, just insert front
if (queue.front == NULL)
{
queue.front = newParty;
queue.rear = newParty;
}
// If new party is higher rank, insert in front of the queue
else if (newParty->rank > queue.front->rank)
{
newParty->next = queue.front;
queue.front = newParty;
}
else
{
Party* currentParty = queue.front;
while(currentParty->next != NULL)
{
if (newParty->rank > currentParty->next->rank)
{
newParty->next = currentParty->next;
currentParty->next = newParty;
}
}
}
}
void dequeue()
{
if (queue.front == NULL)
{
return;
}
// Store first node and update the front pointer
Party* prevParty = queue.front;
queue.front = queue.front->next;
// Keep data of front party, and free it.
Party* data = prevParty;
free(prevParty);
prevParty = NULL;
}
/* Handle an arrival of a taxi.
*/
void taxiArrival()
{
if (queue.front == NULL)
{
printf("Empty Queue\n");
return;
}
Party* party = queue.front;
printf("Pick up party %s with %d people.\n", party->name, party->partyCount);
dequeue();
}
/* Handles an arrival of a new party.
* Arguments:
* rank - the status of the party assumed to be one of 1, 2, 3, 4, 5
* count - the number of people in the party
* name - the name of the party
*/
void partyArrival(int rank, int partyCount, char name[])
{
if (queue.front != NULL && rank < queue.front->rank)
{
printf("Rejected\n");
}
else
{
printf("Accepted\n");
enqueue(rank, name, partyCount);
}
}
/* Free the contents of the datastructures used */
void freeAll()
{
Party* current = queue.front;
Party* temp;
while(current != NULL)
{
temp = current->next;
free(current);
current = temp;
}
}
/* Main function loops for however many timesteps is specified,
* reading from the standard input and handling taxi / party arrivals
*/
int main(int argc, char* argv[])
{
int timesteps; // The total number of timesteps
char input[10]; // For the input prefix "Taxi" or "Party"
char name[25]; // The party name
int rank; // The party's status
int count; // The number of people in the party
scanf("%d", &timesteps);
for (int i = 0 ; i < timesteps ; i ++)
{
scanf("\n%s", input);
if (strcmp(input, "Taxi") == 0)
{
taxiArrival();
}
else
{
scanf(" %d %d %s", &rank, &count, name);
partyArrival(rank, count, name);
}
}
freeAll();
}