微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我有一个带有标记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 举报,一经查实,本站将立刻删除。