0

This is my code for deleting from beginning of a linked list that contains student record.

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

This is the structure

struct student
{
    int id;
    char name[10];
    char gender[10];
    struct student * link;
}*head;

But instead of deleting the entire record only the id is changed to 0

before deletion
  ID       Name Gender
   1    Yazhini Female 
   2        Anu Female 
   3     Janavi Female 
   4    Haritha Female

after deletion

  ID       Name Gender
   0    Yazhini Female 
   2        Anu Female 
   3     Janavi Female 
   4    Haritha Female
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
aish
  • 55
  • 2
  • 8

1 Answers1

0

The function can look the following way

int delete( struct student **head ) 
{
    int success = *head != NULL;

    if ( success )
    {
        struct student *current = *head;
        *head = current->link;
        free( current );
    }

    return success;
}

As for your function then it has at least a memory leak because at first a memory for a node is allocated and its address is assigned to the variable current

current=(struct student *)malloc(sizeof(struct student));

and then this variable is reassigned.

current=head;

Also it is not clear what the parameter q means.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335