首页 > 解决方案 > 搜索到链表

问题描述

struct Players
{
    int pid;                      /*Player's identifier*/
    int is_alien;                 /*Alien flag*/
    int evidence;                 /*Amount of evidence*/
    struct Players *prev;         /*Pointer to the previous node*/
    struct Players *next;         /*Pointer to the next node*/
    struct Tasks *tasks_head;     /*Pointer to the head of player's task list*/
    struct Tasks *tasks_sentinel; /*Pointer to the sentinel of player's task list*/
};

/**
 * Structure defining a node of the airplane linked list
 */
struct Tasks
{
    int tid;                      /*Task's identifier*/
    int difficulty;               /*Task's difficulty*/
    struct Tasks *next;           /*Pointer to the next node*/  
};
struct Player *players_head; //Global pointer 

所以我有这些。我需要搜索玩家的任务。我做了

struct Players *player=players_head;
struct Tasks *tasks,*test;
test=(struct Tasks*)malloc(sizeof(struct Tasks));

if(player == NULL) {
    return 0;
}

while(player != NULL) {
    test=player->tasks_head;
    //some other code..

我的问题是为什么test=player->task_head 为空。问题出在player->tasks_head 我也试过 objtasks = player->tasks_head有什么帮助吗?谢谢

标签: clinked-list

解决方案


恐怕您从未将 tasks_head 指针设置为指向列表的头部,是吗?

它应该在列表构造函数中第一次完成,并且每次将项目添加到列表的头部时,它都应该更改为更新的项目。

希望它有所帮助!


推荐阅读