0

For an assignment, I must create a linked list in C for the storage of sports players that I read in from a CSV. I have written an addplayer function to allocate memory for a node and append it to the end of the list.

There is also an isEmpty function to test whether the first pointer is equal to NULL or not. I'm currently testing the addplayer function with print statements and the isEmpty function. This compiles but does not work properly. For some reason, isEmpty returns that the list is empty, even though I am setting *first to point to the new node if list is empty in the addplayer function.

Here is my code setting up the structures and functions:

//define struct Season to store season info
struct Season {
        char year[100];
        struct Season *nextseason; //pointer to instance of next season
};

//define struct Player to store player info
struct Player {
        char name[100];
        int salary;
        struct Season season; //create instance of season in struct Player
        struct Player *nextplayer; //pointer to next instance of Player
};

//function prototypes
void addPlayer(char name[], int salary, char year[], struct Player *first);
void isEmpty(struct Player *first);

}
}

//checks if list is empty
void isEmpty(struct Player *first){
        if (first == NULL){
                printf("List is empty");
        }
        else{
                printf("List is not empty");

//insert new players at the end of the list
void addPlayer(char name[], int salary, char year[], struct Player *first){

        //allocate memory for new player
        struct Player *newplayer;
        newplayer = (struct Player*)malloc(sizeof(struct Player));
        strcpy(newplayer->name, name);
        newplayer->salary = salary;
        newplayer->nextplayer = NULL;


        ///allocate memory for new season
        struct Season *newseason;
        newseason = malloc(sizeof(struct Season));
        newplayer->season = *newseason;
        struct Season *seasonptr = &(newplayer->season);
        strcpy(seasonptr->year, year);
        seasonptr->nextseason = NULL;

        struct Player *last = first;

        //if head is null, the list is empty
        if (first == NULL){
                first = newplayer;
                printf("the list is empty and the new node was added");
        }

        //otherwise, find the last node and attach the new node to it
        else {  printf("went to else statement list is not empty");
                while (last->nextplayer != NULL){ //while the node's pointer is not equal to NULL, continue
                        last = last->nextplayer;
                }
                last->nextplayer = newplayer;   //when last.nextplayer == NULL, make last's newplayer pointer point to newplayer
        }


}

Any help debugging is appreciated. I know this code may have other errors, but I will say I tried. I'm pretty sure the code is not returning the correct instance of first and that first needs to be passed with a pointer to it.

  • 1
    Common error. In C all function args are passed by value. It means `first` is a local variable in the function and setting it does not set the caller's variable. See the duplicate post for more details and ways to fix. – kaylum May 30 '22 at 23:54

0 Answers0