Push C changes

This commit is contained in:
Win 2025-05-02 20:45:54 +07:00
parent 4535c360d4
commit ff7991c82b
3 changed files with 71 additions and 20 deletions

BIN
spring-2025/sen-107/00010/a.out Executable file

Binary file not shown.

BIN
spring-2025/sen-107/00010/vipTaxi Executable file

Binary file not shown.

View File

@ -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. * Template for Fundamental Data Structures, skill 00010.
* *
* Created by Pasin Manurangsi, 2025-01-08 * Created by Pasin Manurangsi, 2025-01-08
* Modified by Thanawin Pattanaphol, 2025-03-02
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -29,43 +32,77 @@ typedef struct Queue
// Queue variable // Queue variable
Queue queue = { NULL, NULL }; Queue queue = { NULL, NULL };
// Qeue-related functions // Queue-related functions
void enqueue(int rank, const char* name, int partyCount) void enqueue(int rank, const char* name, int partyCount)
{ {
Party* newParty = (Party*)malloc(sizeof(Party)); Party* newParty = (Party*)malloc(sizeof(Party));
if (!newParty)
{
printf("Memory allocation failed!");
return;
}
strncpy(newParty->name, name, MAX_NAME_LEN - 1); strncpy(newParty->name, name, MAX_NAME_LEN - 1);
newParty->name[MAX_NAME_LEN - 1] = '\0'; newParty->name[MAX_NAME_LEN - 1] = '\0';
newParty->partyCount = partyCount; newParty->partyCount = partyCount;
newParty->next = NULL; newParty->next = NULL;
newParty->rank = rank; newParty->rank = rank;
print("Create node");
// If No queue, just insert front
if (queue.front == NULL) if (queue.front == NULL)
{ {
printf("No queue, insert front");
queue.front = newParty; queue.front = newParty;
queue.rear = 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 = queue.front;
newParty->next 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. /* Handle an arrival of a taxi.
*/ */
void taxiArrival() void taxiArrival()
{ {
// ************** Please change the code below to your code ************** if (queue.front == NULL)
printf("Please add your code to handle taxi arrival.\n"); {
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. /* Handles an arrival of a new party.
@ -74,17 +111,31 @@ void taxiArrival()
* count - the number of people in the party * count - the number of people in the party
* name - the name of 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 ************** if (queue.front != NULL && rank < queue.front->rank)
printf("Please add your code to handle party arrival.\n"); {
printf("Rejected\n");
}
else
{
printf("Accepted\n");
enqueue(rank, name, partyCount);
}
} }
/* Free the contents of the datastructures used */ /* Free the contents of the datastructures used */
void freeAll() void freeAll()
{ {
// ************** Please change the code below to your code ************** Party* current = queue.front;
printf("Please add your code to free all datastructures.\n"); Party* temp;
while(current != NULL)
{
temp = current->next;
free(current);
current = temp;
}
} }
/* Main function loops for however many timesteps is specified, /* Main function loops for however many timesteps is specified,