创建一个动态分配的链表

标签 c string linked-list

我正在尝试创建一个游戏,在某个时候当用户进入游戏时,将为该玩家创建一个结构。 如果玩家键入其名称,游戏会提示玩家以下内容:

Type player name:> George
Choose one of the following Otions:
    [+]1) to remove George from player List.
    [+]0) to keep George.

玩家需要输入 3 个名字,然后决定是删除还是保留其中一个或全部。

我遇到的问题是,如果玩家决定从列表中删除一个或多个列表,我将无法保留创建的(保留的)列表。

这是可以编译的部分程序:

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

#define PERCENT 5
#define LEFT "left"
#define RIGHT "Right"

struct node {
    int player_ID;
    struct node* prev;
    struct node* next;
    char move_player[256];
    char player_name[ 31 ];
} node;

struct node *root;
struct node *current;
struct node *player_node( int player_ID, const char *player_name );
void delete_player ( void *player_list );

int main( void )
{
    char name[256] = { 0 };
    int i = 0, clean;

    root = player_node( PERCENT, RIGHT );
    current =  root;

    do{
        struct node *player = player_node( PERCENT, LEFT );
        player->prev = current;
        current->next = player;
        current = player;

        printf( "Type player name:> ");
        if ( fgets( name, 256, stdin ) == NULL )
        {
            printf("Error, fgets()\n");
            exit( EXIT_FAILURE );
        }
        name[ strcspn( name, "\n" ) ] = '\0';
        strncpy( player->player_name, name, strlen( name ) );
        printf( "Choose one of the following Otions:\n" );
        printf( "\t[+]1) to remove %s from player List.\n", current->player_name );
        printf( "\t[+]0) to keep %s.\n", current->player_name );


        int opt = 0;
        if ( scanf( "%d", &opt) != 1 )
        {
            printf("Error, scanf()\n");
        }else if ( opt == 1 )
        {
            delete_player ( player );
        }
        while ( ( clean = getchar() ) != '\n' && clean != EOF );
        i++;
    }while( i < 3 );

    struct node *tmp = root->next;
    while ( tmp != NULL )
    {
        printf( "Name = %s", tmp->player_name );
        tmp = tmp->next;
    }
    free( root );
}

struct node *player_node( int player_ID, const char *const movement )
{
    struct node *player = malloc( sizeof( struct node ) );
    player->prev = NULL;
    player->next = NULL;
    player->player_ID = player_ID;
    strncpy( player->move_player, movement, strlen( movement) );
    strncpy( player->player_name, "NULL", 5 );
    return player;
}

void delete_player ( void *player_list )
{
    struct node *local_client = (struct node* )player_list;
    if ( local_client == current )
    {
        current = local_client->prev;
        current->next = NULL;
    } else
    {
        local_client->prev->next = local_client->next;
        local_client->next->prev = local_client->prev;
    }
    free( local_client );
}

如果代码达到这个:

struct node *tmp = root->next;
while ( tmp != NULL )
{
    printf( "Name = %s", tmp->player_name );
}

列表中没有剩余玩家,因为列表未打印。

为什么列表是空的?

最佳答案

使用strcpy()而不是 strncpy()。后者是not safer比前者。

改变:

strncpy(player->player_name, name, strlen( name ));

到:

strcpy(player->player_name, name);

对这个调用做同样的事情:

strncpy( player->move_player, movement, strlen( movement) );

因为在这两种情况下您都复制 NULL 终止符。

然后,当 printf() 或任何标准字符串函数处理您的字符串时,它不知道何时停止...

你的问题的核心是 [strlen()] 3计算字符串的长度,不考虑 NULL 终止符:

The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

我还会将 strncpy( player->player_name, "NULL", 5 ); 更改为同构。


如果您必须使用strncpy(),那么只需在第三个参数中加1,如下所示:

strncpy(player->player_name, name, strlen( name ) + 1);

但是,请记住,在此代码中使用 strncpy() 是没有意义的,只会降低可读性,因为它会增加复杂性并使代码变得不那么干净。

关于创建一个动态分配的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53393874/

相关文章:

c - openCV中的矩阵运算

java - 如何将 arrayList 转换为由连字符分隔的字符串

c - 在c中的链表末端节点插入一个元素

python - 递归计算链表中的节点数

c - 为什么 C99 中不包含定点类型?

c - UNIX 网络编程中的前缀是什么意思?

ruby - 当字符串包含数字时,为什么 Ruby 的 String#to_i 有时会返回 0?

c - 奇怪的 sqlite 数据库字符串文件路径错误

python - 什么时候链接列表优先于列表?

C - 头文件中的结构体定义