加载我的链表时出现问题

时间:2014-02-27 07:09:59

标签: c linked-list

我似乎无法让我的代码正确加载每个节点。它将加载正确的字符串或正确的整数,但不是两者都加载,除非我在每次运行while循环后声明头为NULL。

这是一项家庭作业,因此提示并不仅仅是固定代码。

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

输入文件标记为roster.txt

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

2 个答案:

答案 0 :(得分:2)

在结构中使用char数组。

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

否则可能是悬空指针的情况。

答案 1 :(得分:1)

更改

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