function - C Linked List - understending, how to delete the same element on list -


can tell my, how it's possible, function working ? structure :

struct el{     int key;     struct el *next; }; typedef struct el ellisty; typedef ellisty *list; 

and function:

void delete(list *l, int zm) {     list p, *k;     k = l;     while ((*k))     {         if ((*k)->key == zm) {             p = *k;             *k = (*k)->next;             free(p);         }         else         {             k = &(*k)->n;         }     } } 

if can illustrate it, awesome.

i didn't tried it, i'm happy you. let's take look. other users suggest, double typedef annoying; code isn't clear @ , it's more complex understand code do. struct composition key, label, , pointer next struct. function delete() comparing int zm, takes argument, node of linked list. second argument linked list root pointer, or start pointer.

about function: in words, function reading value (key) of linked list , comparing int zm. if key different, (*k) next pointer. method permitts read every node starting node argument. take care in last line n should next.

if key , zm equals, function free node of list free() function (because linked list node build malloc or calloc function) , set value of k - double pointer , (*k), value of first pointer, pointer - point next one, in order check next node , delete if needed.

in particular, if zm equals key, function:

  • saves value of pointer in p variable;

  • set k points next struct, because while condition configured on k value , possible find 2 or more nodes same key;

  • free correct memory pointed p;

it important focus on 2 things: 1) setting k values points next struct important because prevents code not stop first value finds. 2) while condition, (*k), means checking value of k, pointer, until null, means end.

hope it's clear enough, i'm here other help.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -