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