// crab_fight.c // // Written by YOUR-NAME (YOUR-ZID) // on TODAYS-DATE // // Simulates a crab fighting tournament using a linked list sorted by strength. #include #include // HINT: You may want to change this struct struct crab { int strength; struct crab *next; }; // Function prototypes struct crab *insert_crab(struct crab *fighters, int strength); struct crab *filter_crabs(struct crab *fighters); void print_fighters(struct crab *fighters); // ----------------------------------------------------------------------------- //////////////// DO NOT CHANGE THE MAIN FUNCTION /////////////////////////////// int main(void) { struct crab *fighters = NULL; int strength; printf("Enter the strength of the next crab: "); while (scanf("%d", &strength) == 1) { fighters = insert_crab(fighters, strength); fighters = filter_crabs(fighters); print_fighters(fighters); printf("Enter the strength of the next crab: "); } printf("\nFinal results:\n"); print_fighters(fighters); return 0; } // ----------------------------------------------------------------------------- // YOUR FUNCTIONS // ----------------------------------------------------------------------------- // Insert a new crab of the given strength into the list of fighters. // The list remains sorted in increasing order of strength. struct crab *insert_crab(struct crab *fighters, int strength) { // TODO: Complete this function return fighters; } // Remove all crabs from the list that have been surpassed in strength // 3 or more times (i.e. knocked out of the tournament). struct crab *filter_crabs(struct crab *fighters) { // TODO: Complete this function return fighters; } // ----------------------------------------------------------------------------- // PROVIDED FUNCTIONS // ----------------------------------------------------------------------------- // DO NOT CHANGE ANY OF THE CODE BELOW HERE // Print the list of surviving fighters, from weakest to strongest. // Print the list of surviving fighters, from weakest to strongest. void print_fighters(struct crab *fighters) { if (fighters == NULL) { printf("No fighters left standing!\n"); return; } struct crab *curr = fighters; while (curr != NULL) { printf("%d", curr->strength); if (curr->next != NULL) { printf(" -> "); } curr = curr->next; } printf("\n"); }