从结构中删除元素

时间:2013-02-21 18:32:39

标签: c struct pthreads

我有以下代码正常工作,除了删除元素时,它将从父结构中删除。我需要父节点结构(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中?*

1 个答案:

答案 0 :(得分:1)

这是您的解决方案。

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);