gpt4 book ai didi

c - 从结构中删除元素

转载 作者:行者123 更新时间:2023-11-30 15:51:35 25 4
gpt4 key购买 nike

我有以下代码,该代码工作正常,除了当删除元素时,它会从父结构中删除。我需要父结构(first_name)与所有元素保持完整:

struct names_list {
char username[20];
struct names_list * next;
};
pthread_mutex_t name_list;

int retu = 0;
char username[20];
struct names_list * curr_name = first_name;
struct names_list * temp_name = NULL;
while( curr_name != NULL )
{
pthread_mutex_lock(&name_list);
strncpy(username, curr_name->username, sizeof( username ) - 1 );
pthread_mutex_unlock(&name_list);
//validate
retu = validate_name(sock,buff,sizeof(buff),username,domain);
switch ( retu )
{
case 0:
pthread_mutex_lock(&name_list);
if (temp_name == NULL)
first_name = curr_name->next; //this is used for removing when current is first record
else
temp_name->next = curr_name->next; //this is used for removing current record from mid-list

free(curr_name);//Freeing should be needed
curr_name = curr_name->next;
pthread_mutex_unlock(&name_list);
break;

case 1:
pthread_mutex_lock(&name_list);
temp_name = curr_name;
curr_name = curr_name->next;
pthread_mutex_unlock(&name_list);
break;
}

if (retu == 2)
break;
}

pthread_mutex_lock(&name_list);
curr_name = first_name;
pthread_mutex_unlock(&name_list);

*如何仅从 curr_name 中删除元素并保留 first_name 中的原始结构?*

最佳答案

这是您的解决方案。

struct names_list {
char username[20];
struct names_list * next;
};
pthread_mutex_t name_list;


int retu = 0;
char username[20];
struct names_list * temp_list_head=NULL;
struct names_list * prev_list_node=NULL;
struct names_list * curr_name = first_name;
struct names_list * temp_name = NULL;

//make a copy of your list
while (curr_name){
struct names_list *tempNode = malloc(sizeof( struct names_list));
*tempNode = *curr_name;
if (temp_list_head== NULL)
temp_list_head = tempNode;
if (prev_list_node!=NULL)
prev_list_node->next = tempNode;
prev_list_node = tempNode;
curr_name = curr_name->next;
}

curr_name = temp_list_head;

while( curr_name != NULL )
{
pthread_mutex_lock(&name_list);
strncpy(username, curr_name->username, sizeof( username ) - 1 );
pthread_mutex_unlock(&name_list);
//validate
retu = validate_name(sock,buff,sizeof(buff),username,domain);
switch ( retu )
{
case 0:
pthread_mutex_lock(&name_list);
if (temp_name == NULL)
temp_list_head = curr_name->next; //this is used for removing when current is first record
else
temp_name->next = curr_name->next; //this is used for removing current record from mid-list
free(curr_name);//Freeing should be needed
// curr_name = curr_name->next; //not needed, its removed from the list
pthread_mutex_unlock(&name_list);
break;
case 1:
pthread_mutex_lock(&name_list);
temp_name = curr_name;
curr_name = curr_name->next;
pthread_mutex_unlock(&name_list);
break;
}
if (retu == 2)
break;
}
pthread_mutex_lock(&name_list);
curr_name = temp_list_head;
pthread_mutex_unlock(&name_list);

关于c - 从结构中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15009734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com