diff --git a/spring-2025/sen-107/00010/a.out b/spring-2025/sen-107/00010/a.out new file mode 100755 index 0000000..f5f3d6a Binary files /dev/null and b/spring-2025/sen-107/00010/a.out differ diff --git a/spring-2025/sen-107/00010/vipTaxi b/spring-2025/sen-107/00010/vipTaxi new file mode 100755 index 0000000..778511b Binary files /dev/null and b/spring-2025/sen-107/00010/vipTaxi differ diff --git a/spring-2025/sen-107/00010/vipTaxi.c b/spring-2025/sen-107/00010/vipTaxi.c index 9c8bdd4..7ffb839 100644 --- a/spring-2025/sen-107/00010/vipTaxi.c +++ b/spring-2025/sen-107/00010/vipTaxi.c @@ -1,7 +1,10 @@ -/* VIP Taxi Queue +/* + * 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 #include @@ -29,43 +32,77 @@ typedef struct Queue // Queue variable Queue queue = { NULL, NULL }; -// Qeue-related functions +// Queue-related functions void enqueue(int rank, const char* name, int partyCount) { Party* newParty = (Party*)malloc(sizeof(Party)); - if (!newParty) - { - printf("Memory allocation failed!"); - return; - } - strncpy(newParty->name, name, MAX_NAME_LEN - 1); newParty->name[MAX_NAME_LEN - 1] = '\0'; newParty->partyCount = partyCount; newParty->next = NULL; newParty->rank = rank; - print("Create node"); + // If No queue, just insert front if (queue.front == NULL) { - printf("No queue, insert front"); queue.front = newParty; queue.rear = newParty; } - else (newParty->rank > queue.front->rank); + // If new party is higher rank, insert in front of the queue + else if (newParty->rank > queue.front->rank) { - printf("High rank, insert front"); - newParty->next + 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() { - // ************** Please change the code below to your code ************** - printf("Please add your code to handle taxi arrival.\n"); + 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. @@ -74,17 +111,31 @@ void taxiArrival() * count - the number of people in the party * name - the name of the party */ -void partyArrival(int rank, int count, char name[]) +void partyArrival(int rank, int partyCount, char name[]) { - // ************** Please change the code below to your code ************** - printf("Please add your code to handle party arrival.\n"); + 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() { - // ************** Please change the code below to your code ************** - printf("Please add your code to free all datastructures.\n"); + 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,