// It's a FIFA World Cup year (I wish, but not long to wait!) // To celebrate, I want to keep adding countries to the list, as they qualify // and will keep track of who has been knocked out of the competition and // who is still playing. // 1. I need to create a list, to which I can add all the // participating countries as they qualify // 2. I want to be able to print out this list // 3. The countries will start in random order #include #include #include #define MAX_LENGTH 20 // 1. TODO: Define team struct, which will hold the name of the country, the // number of goals scored during competition and the pointer to the next team // in the list struct team { int goals; char country[MAX_LENGTH]; struct team *next; }; //Helper functions: one to create a new team and one to print out the team //TODO: Write the inputs and outputs to these functions to complete the //definitions... what does this remind you of? struct team *create_team (char *country, int goals, struct team *new_next); void print_teams (struct team *head); struct team *insert_after(char country[MAX_LENGTH], int goals, struct team *head); struct team *insert_alphabetical(char country[MAX_LENGTH], int goals, struct team *head); struct team *delete_team(char country[MAX_LENGTH], struct team *head); int main (void) { //TODO: Create some teams here struct team *head = create_team("Uruguay", 2, NULL); head = create_team("France", 2, head); head = create_team("Brazil", 3, head); head = create_team("Australia", 8, head); head = create_team("Argentina", 12, head); //TODO: What will be the input to the print_team function? print_teams(head); // insert_after("Spain", 6, head); // print_teams(head); insert_alphabetical("Zimbabwe", 1, head); print_teams(head); head = delete_team("Argentina", head); print_teams(head); return 0; } //Function that creates a new team (node) // TODO: Fill in input and output to complete function struct team *create_team (char *country, int goals, struct team *new_next) { //TODO: create a pointer to a node of type struct team //by using malloc to allocate memory struct team *new_team = malloc(sizeof(struct team)); //TODO: Initialise the data by setting it to a name of a country, //and goal number strcpy(new_team->country, country); new_team->goals = goals; //TODO: Initialise next pointer to point to the next team given new_team->next = new_next; //TODO: What will I need to return? return new_team; } //strcmp for exact match returns 0.... struct team *insert_after(char country[MAX_LENGTH], int goals, struct team *head) { struct team *current = head; struct team *new_team = create_team(country, goals, NULL); while (current != NULL) { //find a node that has France as the country, and insert after it if(strcmp(current->country, "France") == 0) { new_team->next = current->next; current->next = new_team; } current = current->next; } return head; } //strcmp returns 0 if the two strings are identical //>0 if the first non-matching character is greater than that of // the second string //<0 if hte first non-matching character is less than that of the //second string struct team *insert_alphabetical(char country[MAX_LENGTH], int goals, struct team *head) { struct team *current = head; struct team *new_team = create_team(country, goals, NULL); //is it the first thing that I need to insert? while(current->next != NULL && strcmp(country, current->next->country) > 0) { current = current->next; } new_team->next = current->next; current->next = new_team; return head; } // Madagascar and Argentina M>A (>0) // Madagascar and Australia M>A (>0) // Madagascar and Brazil M>B (>0) // Madagascar and France M>F (>0) // Madagascar and Uruguay Mcountry == country) { struct team *new_head = current->next; free(current); return new_head; } while ((strcmp(current->next->country, country) != 0) && current->next != NULL) { current = current->next; } if (strcmp(current->next->country, country) == 0) { struct team *new_next = current->next->next; free(current->next); current->next = new_next; } return head; } //Function that prints the list of teams and goals of each // TODO: Fill in input and output to complete function void print_teams (struct team *head) { //TODO: create a way to keep track of where we are in the list struct team *current = head; //TODO: What will be the condition where our while loop can stop while (current != NULL) { //TODO: print out the team (node) value printf("%s with %d goals\n", current->country, current->goals); //TODO: increment to the next node current = current->next; } }