0

次のコードは正常に機能していますが、要素が削除されると、親構造から削除されます。すべての要素をそのまま維持するには、親構造(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に保持するにはどうすればよいですか?*

4

1 に答える 1

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);
于 2013-02-21T18:40:22.247 に答える