C++ 将指向结构的指针传递给函数时保留对结构的更改
我有一个带有哨兵的循环双链接列表,名为C++ 将指向结构的指针传递给函数时保留对结构的更改,c++,pointers,struct,C++,Pointers,Struct,我有一个带有哨兵的循环双链接列表,名为Players。每个玩家还有一个任务链接列表,最初为空,存储在指针tasks\u head和tasks\u sentinel中 现在,在我下面的方法中,我想传递一个选定玩家的结构,这样我就可以创建任务,并开始通过tasks\u head和tasks\u sentinel填充他的任务列表。对播放器结构所做的更改如何保持?我读过关于引用调用的文章,但我做得对吗?在使用指针时是否需要 struct Players { int pid;
Players
。每个玩家还有一个任务链接列表,最初为空,存储在指针tasks\u head
和tasks\u sentinel
中
现在,在我下面的方法中,我想传递一个选定玩家的结构,这样我就可以创建任务,并开始通过tasks\u head
和tasks\u 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;
}
struct播放器
{
int pid;/*玩家标识符*/
int是_alien;/*alien标志*/
int证据;/*证据数量*/
struct Players*prev;/*指向上一个节点的指针*/
struct Players*next;/*指向下一个节点的指针*/
struct Tasks*Tasks\u head;/*指向玩家任务列表头部的指针*/
struct Tasks*Tasks\u sentinel;/*指向玩家任务列表哨兵的指针*/
};
/**
*结构定义任务排序链表的节点
*/
结构任务
{
int tid;/*任务的标识符*/
int难度;/*任务的难度*/
结构任务*下一步;/*指向下一个节点的指针*/
};
内部分类任务列表(结构玩家**玩家,内部tid,内部难度){
struct Tasks*node=new Tasks;//如果没有足够的mem,则可能返回null
如果(node==NULL)返回0;
//将数据添加到新节点
节点->tid=tid;
节点->难度=难度;
节点->下一步=空;
如果(isTaskListEmpty((*player)->tasks_head)){//如果列表为空,则使head和sentinel指向新节点
(*玩家)->任务头->下一步=节点;
(*玩家)->任务\哨兵->下一步=节点;
返回1;
}
//检查是否可以将新节点添加到列表的末尾
如果(*玩家)->任务\u哨兵->下一步->难度任务\u哨兵->下一步->下一步=节点;//将新节点放在列表的末尾
(*player)->tasks\u sentinel->next=node;//更新sentinel
返回1;
}
//在不破坏列表排序的情况下插入新节点
结构任务*curr=(*player)->Tasks\u head->next;
while(curr->next!=NULL&&curr->next->难度<难度){
当前=当前->下一步;
}
节点->下一步=当前->下一步;
当前->下一步=节点;
返回1;
}