这是我从包含学生记录的链接列表的开头删除的代码。
int delete (struct student **q) {
struct student *current;
current=(struct student *)malloc(sizeof(struct student));
current=head;
head=current->link;
free(current);
//display(current);
return 1;
}
这是结构
struct student
{
int id;
char name[10];
char gender[10];
struct student * link;
}*head;
但不是删除整个记录 只有id被更改为0
before deletion
ID Name Gender
1 Yazhini Female
2 Anu Female
3 Janavi Female
4 Haritha Female
删除后
ID Name Gender
0 Yazhini Female
2 Anu Female
3 Janavi Female
4 Haritha Female
答案 0 :(得分:0)
该功能可以按以下方式查看
int delete( struct student **head )
{
int success = *head != NULL;
if ( success )
{
struct student *current = *head;
*head = current->link;
free( current );
}
return success;
}
至于你的功能,它至少有一个内存泄漏,因为首先分配一个节点的内存,并将其地址分配给变量current
current=(struct student *)malloc(sizeof(struct student));
然后重新分配此变量。
current=head;
还不清楚参数q的含义。