我的链接列表不会从头开始打印

时间:2014-06-30 15:08:40

标签: c linked-list malloc

我对我的程序遇到的问题是我创建了一个链表,但是当我打印出我的链表时,它只打印出列表中的最后一个人。它应该打印出列表中的每个人。这是我在程序中遇到的唯一问题,如果有人能指出我正确的方向,我们将不胜感激。

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

#define MAX_PLAYERS 9
#define MAX_STR_LEN 25

typedef struct Player_ {
    char name[MAX_STR_LEN];
    int number_of_hits;
    int number_of_strikes;
    struct Player_ *next;
} Player;

int pitch_result(void);
Player *add_player(Player *first_player, const char *name);
void destroy_team(Player *first_player);
void display_line_up(Player* first);
void readFile(Player **lineup, const char* file);

int main(int argc, char *argv[]) {
    if(argc < 2){
        exit(1);
    }
    srand(time(NULL));
    int strikes = 0;
    int hits = 0;
    Player *lineup = NULL;
    readFile(&lineup, argv[1]);
    printf("\nLine up for this team is: ");
    display_line_up(lineup);

    printf("\n\n%s is batting \n", lineup->name);

    for ( ; strikes < 3; ) {
        int result = pitch_result();
        if (result) {
            ++hits;
        }
        else
            ++strikes;
    }

    printf("\nScore of game was: %d\n", hits/4);
    return 0;
}

void readFile(Player **lineup, const char* file){
    FILE *input;
    input = fopen(file, "r");
    if(input == NULL){
        printf("Failed to open");
    }
    char player[MAX_STR_LEN];
    while(fscanf(input, "%s", &player) != EOF){
        //      printf("\n%s", player);
        *lineup = add_player(*lineup, player);
    }
}

int pitch_result(void) {
    int result;
    result = rand() % 2;
    return result;
}

void destroy_team(Player *first_player){
    Player *temp = first_player->next;
    Player *free_player;

    while (temp) {
        free_player = temp;
        temp = temp->next;
        free(free_player->name);
        free(free_player);
    }
}

Player *add_player(Player *first_player, const char *name) {
    Player *new_player = (Player*)malloc(sizeof(Player));

    int nameLength;
    nameLength = strlen(name);

    // new_player->name = malloc(nameLength * sizeof(char));

    new_player->next = NULL;

    strncpy(new_player->name, name, nameLength);
    new_player->number_of_hits = 0;
    new_player->number_of_strikes = 0;

    return new_player;
}

void display_line_up(Player *first){

    if(first == NULL)
        printf("\nThe list is empty");
    else {
        while(first != NULL){
            printf("\n%s", first->name);
            first = first->next;
        }
    }
}

这是我运行时读入程序的文件(stats.txt)

Yadier_Molina
Allen_Craig
Daniel_Descalso
Rafael_Furcal
David_Freese
Matt_Holiday
Jon_Jay
Carlos_Beltran
Matt_Adams

这是我为我的程序提供的示例输出

Line up for this team is:   

Yadier_Molina   
Allen_Craig 
Daniel_Descalso 
Rafael_Furcal   
David_Freese    
Matt_Holliday   
Jon_Jay 
Carlos_Beltran  
Matt_Adams  
Yadier_Molina   is  batting 
Strike! 
hit!    
Strike! 
Strike! 

Score   of  game    was:    0

2 个答案:

答案 0 :(得分:2)

您的add_player函数返回指向包含新播放器的节点的指针 - 并且与预先存在的列表没有任何关系(从不使用变量first_player)。

您的readFile函数使用此功能,每次读取新播放器的名称时都会丢弃旧列表:

*lineup = add_player(*lineup, player);

所以结果列表只包含最后一个玩家。

答案 1 :(得分:0)

正如Beta所说,你并没有将玩家连接到列表中。您可以保留指向最后一个元素的指针,您可以从readFile

修改该元素
void readFile(Player **lineup, const char* file){
    Player* last = NULL;
    FILE *input;
    input = fopen(file, "r");
    if(input == NULL){
        printf("Failed to open");
    }
    char player[MAX_STR_LEN];
    while(fscanf(input, "%s", &player) != EOF){
        //      printf("\n%s", player);
        Player* p = add_player(player);
        if (!last)
          *lineup = p;
        else 
          last->next = p;
        last = p;
    }
}

add_player函数只是创建一个新的播放器,我会将其重命名为:create_player(const char* name)