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

roster.c #include <stdio.h> #include <string.h> #include <ctype.h> #define NAME_

ID: 3760297 • Letter: R

Question

roster.c

#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 code here and remove the return NULL; statement
   return NULL;
}

void find_player(struct player *roster)
{
   //add code here

}
void printList(struct player *roster){

   //add code here

}
void clearList(struct player *roster)
{
   //add 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

I added psudo code for above functions empty. you can modify according to program needs. I added main logic find the player, appaend and print list and clear list logics in below.

struct player *append_to_list(struct player *roster){


    struct player *new_node = (struct player*) malloc(sizeof(struct player));
    struct player *last = *roster;

    /* 2. put in the data */
    new_node->data = new_data;

    /* 3. This new player is going to be the last player, so make next of
          it as NULL*/
    new_node->next = NULL;

    /* 4. If the Linked List is empty, then make the new player as head */
    if (*head_ref == NULL)
    {
       *head_ref = new_node;
       return;
    }

    /* 5. Else traverse till the last node */
    while (last->next != NULL)
        last = last->next;

    /* 6. Change the next of last node */
    last->next = new_node;
    return;
  
   return NULL;
}

void find_player(struct player *roster)
{
   int GetNth(struct node* head, int index)
{
    struct node* current = head;
    int count = 0; /* the index of the node we're currently
                  looking at */
    while (current != NULL)
    {
       if (count == index)
          return(current->data);
       count++;
       current = current->next;
    }

    /* if we get to this line, the caller was asking
       for a non-existent element so we assert fail */
    assert(0);            
}

}
void printList(struct player *roster){

   r=head;
    if(r==NULL)
    {
    return;
    }
    while(r!=NULL)
    {
    printf("%d ",r->data);
    r=r->next;
    }
    printf(" ");

}
void clearList(struct player *roster)
{
   struct node *temp, *prev;
    temp=head;
    while(temp!=NULL)
    {
    if(temp->data==num)
    {
        if(temp==head)
        {
        head=temp->next;
        free(temp);
        return 1;
        }
        else
        {
        prev->next=temp->next;
        free(temp);
        return 1;
        }
    }
    else
    {
        prev=temp;
        temp= temp->next;
    }
    }
    return 0;

}