Updated SEN-107
This commit is contained in:
parent
7f7fd7c39c
commit
4535c360d4
|
|
@ -0,0 +1,115 @@
|
|||
/* VIP Taxi Queue
|
||||
* Template for Fundamental Data Structures, skill 00010.
|
||||
*
|
||||
* Created by Pasin Manurangsi, 2025-01-08
|
||||
*/
|
||||
#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 };
|
||||
|
||||
// Qeue-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 (queue.front == NULL)
|
||||
{
|
||||
printf("No queue, insert front");
|
||||
queue.front = newParty;
|
||||
queue.rear = newParty;
|
||||
}
|
||||
else (newParty->rank > queue.front->rank);
|
||||
{
|
||||
printf("High rank, insert front");
|
||||
newParty->next
|
||||
}
|
||||
}
|
||||
|
||||
/* 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");
|
||||
}
|
||||
|
||||
/* 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 count, char name[])
|
||||
{
|
||||
// ************** Please change the code below to your code **************
|
||||
printf("Please add your code to handle party arrival.\n");
|
||||
}
|
||||
|
||||
/* 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");
|
||||
}
|
||||
|
||||
/* 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", ×teps);
|
||||
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();
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
7
|
||||
Taxi
|
||||
Party 3 2 Elon
|
||||
Party 3 1 Bill
|
||||
Party 5 3 Donald
|
||||
Party 4 2 JD
|
||||
Taxi
|
||||
Taxi
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
Empty Queue
|
||||
Accepted
|
||||
Accepted
|
||||
Accepted
|
||||
Rejected
|
||||
Pick up party Donald with 3 people
|
||||
Pick up party Elon with 2 people
|
||||
Loading…
Reference in New Issue