// route_setter.c // // Written by YOUR-NAME (YOUR-ZID) // on TODAYS-DATE // // A linked list representation of a bouldering route. #include #include // Struct representing a hold in a climbing route struct hold { int hold_id; int difficulty; struct hold *next; }; // Your function prototypes struct hold *append_hold(struct hold *route, int new_hold_id, int difficulty); struct hold *insert_after_hold(struct hold *route, int hold_id, int new_hold_id, int difficulty); struct hold *remove_hold(struct hold *route, int hold_id); struct hold *find_crux(struct hold *route); int climber_progress(struct hold *route, int skill); // Provided function prototypes struct hold *create_hold(int hold_id, int difficulty); void print_route(struct hold *route); void free_route(struct hold *route); void print_commands(); void command_loop(); // ----------------------------------------------------------------------------- //////////////// DO NOT CHANGE THE MAIN FUNCTION /////////////////////////////// int main(void) { // Create a sample route with 4 holds printf("Welcome to Route Setter!\n"); command_loop(); return 0; } // ----------------------------------------------------------------------------- // YOUR FUNCTIONS // ----------------------------------------------------------------------------- // Append a new hold to the end of the route. struct hold *append_hold(struct hold *route, int new_hold_id, int difficulty) { // TODO: Implement this function return NULL; } // Insert a new hold after the hold with hold_id. struct hold *insert_after_hold(struct hold *route, int hold_id, int new_hold_id, int difficulty) { // TODO: Implement this function return NULL; } // Remove the hold with hold_id from the route. struct hold *remove_hold(struct hold *route, int hold_id) { // TODO: Implement this function return NULL; } // Return a pointer to the hardest hold in the route. struct hold *find_crux(struct hold *route) { // TODO: Implement this function return NULL; } // Return how many holds a climber can complete. int climber_progress(struct hold *route, int skill) { // TODO: Implement this function return 0; } // ----------------------------------------------------------------------------- // PROVIDED FUNCTIONS // ----------------------------------------------------------------------------- // DO NOT CHANGE ANY OF THE CODE BELOW HERE // Creates a new hold and returns a pointer to it. struct hold *create_hold(int hold_id, int difficulty) { struct hold *new_hold = malloc(sizeof(struct hold)); new_hold->hold_id = hold_id; new_hold->difficulty = difficulty; new_hold->next = NULL; return new_hold; } // Prints the entire route, one hold per line. void print_route(struct hold *route) { printf("==== Current Route ====\n"); if (route == NULL) { printf("Route is empty.\n"); return; } while (route != NULL) { printf("Hold %d (difficulty %d)\n", route->hold_id, route->difficulty); route = route->next; } } // Frees every hold in the route. void free_route(struct hold *route) { while (route != NULL) { struct hold *next = route->next; free(route); route = next; } } // Print the available commands for the user. void print_commands() { printf("Commands:\n"); printf(" a - append hold to the route\n"); printf(" i - insert hold\n"); printf(" r - remove hold\n"); printf(" p - print route\n"); printf(" c - climber progress\n"); printf(" x - find crux\n"); printf(" q - quit\n\n"); } // Command loop for user input. void command_loop() { char command; struct hold *route = NULL; while (scanf(" %c", &command) == 1 && command != 'q') { if (command == 'a') { int new_id; int difficulty; scanf("%d %d", &new_id, &difficulty); route = append_hold(route, new_id, difficulty); } else if (command == 'i') { int after_id; int new_id; int difficulty; scanf("%d %d %d", &after_id, &new_id, &difficulty); route = insert_after_hold(route, after_id, new_id, difficulty); } else if (command == 'p') { print_route(route); } else if (command == 'r') { int hold_id; scanf("%d", &hold_id); route = remove_hold(&route, hold_id); } else if (command == 'c') { int skill; scanf("%d", &skill); printf( "A climber with skill %d can climb %d holds before failing.\n", skill, climber_progress(route, skill) ); } else if (command == 'x') { struct hold *crux = find_crux(route); if (crux != NULL) { printf( "Crux hold is %d (difficulty %d)\n", crux->hold_id, crux->difficulty ); } else { printf("No crux hold found.\n"); } } else if (command == '?') { print_commands(); } } free_route(route); }