c-搜索链接列表


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,有什么帮助吗?感谢

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

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

希望它能有所帮助!

您的代码中还没有初始化players_head。必须在main(或其他函数(中键入一个位置:

players_head = malloc(sizeof(struct Player));

然后初始化指向某个玩家的指针。

此外,您可以键入:

typedef struct Players
{
...
your code
...
} player;

以便您可以在不必键入"的情况下实例化玩家对象;结构";之前(它将structPlayer重命名为Player(。

最新更新