将结构的指针传递给函数时,保留对结构的更改

问题描述

我有一个带有标记Players的圆形双向链表。每个玩家还有一个链接为空的任务列表,存储在指针tasks_headtasks_sentinel中。

现在在下面的方法中,我想传递选定玩家的结构,以便我可以创建任务并开始通过tasks_headtasks_sentinel填充他的任务列表。如何对播放器的结构进行更改?我已经读过有关通过引用进行调用的信息,但是我做对了吗,使用指针甚至需要使用它吗?

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 tasks sorted linked list
 */
struct Tasks
{
    int tid;                      /*Task's identifier*/
    int difficulty;               /*Task's difficulty*/
    struct Tasks *next;           /*Pointer to the next node*/  
};

int sortedInsertTaskList(struct Players** player,int tid,int difficulty) {
    struct Tasks* node = new Tasks; // Could return null if not enough mem
    
    if(node == NULL) return 0;
    // add data to the new node
    node->tid = tid;
    node->difficulty = difficulty;
    node->next = NULL;

    
    if (isTaskListempty((*player)->tasks_head)) { // if the list is empty,make the head and sentinel point to the new node
        
        (*player)->tasks_head->next = node;
        (*player)->tasks_sentinel->next = node;
        return 1;
    }
    

    // check if the new node can be added to the end of the list
    if ((*player)->tasks_sentinel->next->difficulty <= difficulty) {
        (*player)->tasks_sentinel->next->next = node; // place new node at the end of the list
        (*player)->tasks_sentinel->next = node; // update sentinel
        return 1;
    }

    // insert the new node without breaking the sorting of the list
    struct Tasks* curr = (*player)->tasks_head->next;

    while (curr->next != NULL && curr->next->difficulty < difficulty) {
        curr = curr->next;
    }
    node->next = curr->next;
    curr->next = node;
    return 1;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)