简体   繁体   中英

Search into linked list

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 

So Im having these. I need to search for a player's Task. I have made

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..

My problem is why test=player->task_head is null. the problem is at player->tasks_head i have also tried with obj tasks = player->tasks_head any help? Thanks

I'm afraid you have never set the tasks_head pointer to point to the list's head, have you?

It should be done first time in the list constructor and every time you add an item to the head of the list it should be changed to the updated one.

Hope it helped!

You haven't initialized players_head in your code. There must be a place in your main (or some other function) that you must type:

    players_head = malloc(sizeof(struct Player));

and then initialize the pointer to some player.

Also, you can type:

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

so that you can instance player objects without having to type "struct" before (it renames struct Player to player).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM