Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program roster.c maintains a roster for a sports team. Each player has a las

ID: 3760656 • Letter: T

Question

The program roster.c maintains a roster for a sports team. Each player has a last name, firstname, and a number. Complete the program so it uses a dynamically allocated linked list to storethe roster and have the following functions:
1. append_to_list: ask the user to enter player’s last name, first name and number, then add

the player to the end of the linked list.a. It should check whether the player has already existed by number. If so, the function

should print a message and exit.b. If the player does not exist, allocate memory for the player, store the data, and appendthe player to the end of the linked list.c. If the list is empty, the function should return the pointer to the newly created player.d. Otherwise, add the player to the end of the linked list and return the pointer to thelinked list.2. find_player: find the player by number, print the player’s last name and first name in theformat “last name, first name”. If the player is not found, print a message.3. printList: print the name and number of all the players.4. clearList: when the user exists the program, all the memory allocated for the linked listshould be deallocated.


Note: use read_line function included in the program for reading in last names and first names.

Roster.c (below):

#include <stdio.h>
#include <string.h>
#include <ctype.h>


#define NAME_LEN 30
struct player{
   int number;
  
char first_name[NAME_LEN+1];
   char last_name[NAME_LEN+1];
   struct player *next;

};

struct player *append_to_list(struct player *roster);
void find_player(struct player *roster);
void printList(struct player *roster);
void clearList(struct player *roster);
int read_line(char str[], int n);

/**********************************************************
* main: Prompts the user to enter an operation code,     *
*       then calls a function to perform the requested   *
*       action. Repeats until the user enters the        *
*       command 'q'. Prints an error message if the user *
*       enters an illegal code.                          *
**********************************************************/

int main(void)
{  
   char code;
struct player *team_roster = NULL;  
   printf("Operation Code: a for appending to the roster, f for finding a player"
   ", p for printing the roster; q for quit. ");

  for (;;) {    
printf("Enter operation code: ");    
scanf(" %c", &code);    
while (getchar() != ' ')   /* skips to end of line */      
;    
switch (code) {      
case 'a': team_roster = append_to_list(team_roster);                
   break;      
case 'f': find_player(team_roster);                
   break;      
case 'p': printList(team_roster);                
   break;      
case 'q': clearList(team_roster);

return 0;

      default: printf("Illegal code ");    }    printf(" ");  }
}
struct player *append_to_list(struct player *roster){

//add your code here and remove the return NULL; statement
return NULL;

}
void find_player(struct player *roster){

//add your code here

}void printList(struct player *roster){

//add your code here

}void clearList(struct player *roster){

//add your code here

}
int read_line(char str[], int n)
{  
int ch, i = 0;
  
while (isspace(ch = getchar()))    
   ;  
str[i++] = ch;  
while ((ch = getchar()) != ' ') {    
   if (i < n)      
     str[i++] = ch;      
}   
str[i] = '';   
return i;
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define NAME_LEN 30
struct player{
int number;
char first_name[NAME_LEN+1];
char last_name[NAME_LEN+1];
struct player *next;
};

struct player *append_to_list(struct player *roster);
void find_player(struct player *roster);
void printList(struct player *roster);
void clearList(struct player *roster);
int read_line(char str[], int n);

/**********************************************************
* main: Prompts the user to enter an operation code, *
* then calls a function to perform the requested *
* action. Repeats until the user enters the *
* command 'q'. Prints an error message if the user *
* enters an illegal code. *
**********************************************************/

int main(void)
{
char code;
struct player *team_roster = NULL;
printf("Operation Code: a for appending to the roster, f for finding a player"
", p for printing the roster; q for quit. ");
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'a': team_roster = append_to_list(team_roster);
break;
case 'f': find_player(team_roster);
break;
case 'p': printList(team_roster);
break;
case 'q': clearList(team_roster);
return 0;
default: printf("Illegal code "); } printf(" "); }
}
struct player *append_to_list(struct player *roster){
struct player *x, *temp;
char first[NAME_LEN+1], last[NAME_LEN+1];
int num, found;
x = roster;
printf("Enter the first name of the player: ");
scanf("%s", first);
printf("Enter the last name of the player: ");
scanf("%s", last);
printf("Enter the number of the player: ");
scanf("%i", &num);
found = 0;
while(x != NULL)
{
if(x->number == num)
found = 1;
x = x->next;
}
if(found)
{
printf("Player with this number already exists... ");
return roster;
}
else
{
temp = (struct player *)malloc(sizeof(struct player));
strcpy(temp->first_name, first);
strcpy(temp->last_name, last);
temp->number = num;
temp->next = NULL;
if(roster == NULL)
{
return temp;

}
else
{
x = roster;
while(x->next != NULL)
x = x->next;
x->next = temp;
return roster;
}
}
}
void find_player(struct player *roster){
int num;
printf("Enter the number of the player to search for: ");

scanf("%i", &num);
while(roster != NULL)
{
if(roster->number == num)
{
printf("%s %s", roster->last_name, roster->first_name);
break;
}
roster = roster->next;
}
if(roster == NULL)
printf("The player does not exist... ");
}void printList(struct player *roster){
while(roster != NULL)
{
printf("%s %s %i ", roster->last_name, roster->first_name, roster->number);
roster = roster->next;
}
}void clearList(struct player *roster){
struct player *x;
while(roster != NULL)
{
x = roster;
roster = roster->next;
free(x);
}
}
int read_line(char str[], int n)
{
int ch, i = 0;
  
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}   
str[i] = '';   
return i;
}