从链表的开头删除

Deleting from beginning of a linked list

这是我的代码,用于从包含学生记录的链表的开头删除。

    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

函数可以如下所示

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是什么意思