user3353842
user3353842

Reputation: 25

Problems loading my linked list

I can't seem to get my code to load each node correctly. It will either load the correct strings or the correct ints, but not both except when I declare the head NULL after every run through the while loop.

It is a homework assignment so hints and not just fixed code would be appreciated.

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

#define NAME_LENGTH 20
#define CHAMP_LENGTH 20
#define TEAM_LENGTH 10

typedef struct player{
    char* name;
    char* champion;
    char* team;
    int kills;
    int deaths;
    struct player *next;
}Player;

Player* new_player(char* name, char* champion, char* team, int kills, int deaths);
Player* insert_by_player(Player* head, Player* node);
void print(Player* head);
int update_records(Player* head, char* champion, char* champ_name, int kills, int death);
void free_node(Player* node);
Player* delete_player_even(Player* head);
Player* delete_plyaer_odd(Player* head);
Player* reverse_list(Player* head);

int main(int argc, char* argv[])
{
    if (argc<1)
    {
      printf("Incorrect number of arguements\n");
      return 0;
    }

    Player* head = NULL;
    Player* node = NULL;
    char name[NAME_LENGTH] = "";
    char champion[CHAMP_LENGTH] = "";
    char team[TEAM_LENGTH] = "";
    int kills, deaths = 0;
    FILE *in = fopen(argv[1], "r");

    while (1)
    {
      fscanf(in, "%s%*c%s%*c%s%*c%d%*c%d%*c", name, champion, team, &kills, &deaths);

      if (feof(in)) break;

      node = new_player(name, champion, team, kills, deaths);
      head = insert_by_player(head, node);

    }
    print(head);
    fclose(in);
return 0;
}

Player* new_player(char* name, char* champion, char* team, int kills, int deaths)
{
    Player* n = malloc(sizeof(Player));
    n->name = name;
    n->champion = champion;
    n->team = team;
    n->kills = kills;
    n->deaths = deaths;
    n->next = NULL;

    return n;
}

Player* insert_by_player(Player* head, Player* node)
{
    Player* temp;

    if (head == NULL)
    {
      head = node;
      return head;
    }
    else
    head->next = insert_by_player(head->next, node);

return head;
}

void print(Player* head)
{
    if (head == NULL)
       return;
    else
    {
      printf("%s %s %s %d %d\n", head->name, head->champion, head->team, head->kills, head->deaths);
      print(head->next);
    }
}

The input file is labeled as roster.txt

Yellowstar Thresh Fnatic 0 4
Dyrus Jayce Solomid 6 2
Chaox Jinx None 8 1
Hotshotgg Nidalee CLG 4 4
Phreak Orianna Riot 10 5
Bioamp Chogath None 2 2

Upvotes: 0

Views: 61

Answers (2)

G one
G one

Reputation: 2729

Changes

typedef struct player{
    char name[NAME_LENGTH];
    char champion[CHAMP_LENGTH];
    char team[TEAM_LENGTH];
    int kills;
    int deaths;
    struct player *next;
}Player;


Player* new_player(char* name, char* champion, char* team, int kills, int deaths)
{
    Player* n = malloc(sizeof(Player));
    strcpy(n->name , name);
    strcpy(n->champion, champion);
    strcpy(n->team , team);
    n->kills = kills;
    n->deaths = deaths;
    n->next = NULL;

    return n;
}

Upvotes: 1

Rahul
Rahul

Reputation: 3509

Use char array in your struct.

typedef struct player{
    char name[50];
    char champion[50];
    char team[25];
    int kills;
    int deaths;
    struct player *next;
}Player;

Otherwise it may be the case of Dangling pointers.

Upvotes: 2

Related Questions