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

Please help! C89 Function - How to modify append_to_list to insert players in an

ID: 3765753 • Letter: P

Question

Please help!
C89 Function - How to modify append_to_list to insert players in an ordered list( by last and first name) and the list will remain ordered after insertion? Ex: "John Smith" would be after "Bob Smith" but before "George Washington" in the list. Thanks for any help provided.

Here's the struct for player:
struct player{   
   int number;
   char first_name[NAME_LEN+1];
   char last_name[NAME_LEN+1];
   struct player *next;
};

here's the struct for append_to_list:

struct player *append_to_list(struct player *roster){
   struct player *cur, *new_node;

   new_node = malloc(sizeof(struct player));
   if (new_node == NULL) {
       printf("Database is full; can't add more players. Try deleting some first ");
       return roster;
   }

   printf("Enter player number: ");
   scanf("%d", &new_node->number);

   for (cur = roster;cur != NULL;cur = cur->next)
   if (cur != NULL && new_node->number == cur->number) {
       printf("Player already exists. ");
       free(new_node);
       return roster;
   }

   printf("Enter player last name: ");
   read_line(new_node->last_name, NAME_LEN);
   printf("Enter player first name: ");
   read_line(new_node->first_name, NAME_LEN);

   if(roster == NULL){
       roster = new_node;
       return roster;
   }else{
       for(cur = roster; cur->next!= NULL; cur = cur->next);
       cur->next = new_node;
       new_node->next = NULL;
       return roster;
   }
}

Explanation / Answer

Complete Program:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define NAME_LEN 20

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

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;
}

struct player *append_to_list(struct player *roster)
{
   struct player *cur, *new_node, *prev;

   new_node = malloc(sizeof(struct player));
new_node->next = NULL;

   if(new_node == NULL)
{
       printf("Database is full; can't add more players. Try deleting some first ");
       return roster;
   }

   printf("Enter player number: ");
   scanf("%d", &new_node->number);

   for(cur = roster; cur != NULL;cur = cur->next)
{
     if(cur != NULL && new_node->number == cur->number)
  {
         printf("Player already exists. ");
         free(new_node);
         return roster;
     }
}

   printf("Enter player last name: ");
   read_line(new_node->last_name, NAME_LEN);
   printf("Enter player first name: ");
   read_line(new_node->first_name, NAME_LEN);

   if(roster == NULL)
{
       roster = new_node;
       return roster;
   }
else
{
  cur = roster;
  prev = NULL;

       while(cur!= NULL && strcmp(cur->last_name, new_node->last_name) < 0)
  {
   prev = cur;
   cur = cur->next;
  }
         
  if(prev == NULL)
  {
   new_node->next = roster;
   roster = new_node;
  }
  else if(cur == NULL)
  {
   prev->next = new_node;
  }
  else if(cur != NULL && strcmp(cur->last_name, new_node->last_name) > 0)
  {
   prev->next = new_node;
   new_node->next = cur;
  }
  else
  {
   while(cur != NULL && strcmp(cur->first_name, new_node->first_name) < 0)
   {
    prev = cur;
    cur = cur->next;
   }

   if(prev == NULL)
   {
    new_node->next = roster;
    roster = new_node;
   }
   else if(cur == NULL)
   {
    prev->next = new_node;
   }
   else
   {
    prev->next = new_node;
    new_node->next = cur;
   }
  }
      
       return roster;
   }
}

void printList(struct player *roster)
{
if(roster == NULL)
{
  printf("List is empty! ");
  return;
}

printf("%-20s%-20s%-20s ", "PlayerNumber", "FirstName", "LastName");
for(struct player *cur = roster; cur != NULL; cur = cur->next)
{
  printf("%-20d%-20s%-20s ", cur->number, cur->first_name, cur->last_name);
}
printf(" ");
}

int main()
{
struct player *team = NULL;
char choice;

printf("Enter Y/y to add a player to the list: ");
scanf("%c", &choice);

while(choice == 'Y' || choice == 'y')
{
  team = append_to_list(team);

  printf(" Enter Y/y to add one more player to the list: ");
  scanf("%c", &choice);
}

printList(team);

return 0;
}

Sample Output: