Home > OS >  Delete elements from linked list
Delete elements from linked list

Time:12-30

The function not working properly it's deleating only the id_capt and changing the value of another to zero...

Besides I want to do verification that the sensor does belong to the list but it's not working

void suppression_capt(Liste* liste)//id donner par utilisateur a modifier
{
   float new_longitude,new_latitude;//vals a modifier
   char id_capt_user[10];

   Element *courant = liste->premier;
   Element *precedent =courant;

    printf("donner id_capt= ");
    scanf("%s",id_capt_user);

   while(courant != NULL)
   {
   if(strcmp(courant->capt.id_capt,id_capt_user)==0)
        {
           precedent=courant->suivant;
           free(courant);
        }
         courant=courant->suivant;
   }
   printf("\n");
}

Besides I want to do verification that the sensor does belong to the list but it's not working


    /*do
    {
       printf("donner id_capt= ");
       scanf("%s",id_capt_user);                //verification du capteur
       courant=courant->suivant;

    }while(strcmp(courant->capt.id_capt,id_capt_user)!=0);*/

and here is the struct that I used

typedef struct//valeur d'une cellule/noeud
{
    char id_capt[10];
    float longitude;
    float latitude;
}capt_hum;

typedef struct//liste chainee
{
    capt_hum capt;
    struct Element* suivant;
    struct Element* precedent;
}Element;

CodePudding user response:

You should note that you are accessing a pointer which you have already freed


if(strcmp(courant->capt.id_capt,id_capt_user)==0)
        {
           precedent=courant->suivant;
           free(courant);
        }
         courant=courant->suivant;

As for deleting an element from a linked list freeing it is not enough as you need to update the element’s neighbors as well. Say you have a list as follows: A <-> B <-> C And you wish to remove B, you will also need to update A and C to point to each other A <-> C

if(strcmp(courant->capt.id_capt,id_capt_user)==0)
        {
           temp = courant->suivant;
           precedent=courant->precedent;
           free(courant);
           precedent->suivant = temp;
           temp->precedent = precedent;
           courant = precedent; //perhaps replace with break
        }
         courant=courant->suivant;

With this implementation you will iterate the entire list an remove any node that contains the string you’ve entered. You could replace the commented line with “break” if you wish to stop seeking once you’ve found the node to delete. You should add some check for the cases where the node to remove is either the first or the last so you wont accidentally try to assign them when the pointers are NULL or garbage.

CodePudding user response:

after looking and trying because I'm still new to doubly linked list this program worked

void supprimer_capt(Liste **list) {
Element* tmp = *list;
Element* prev = NULL;
char id_capt_user[10];
printf("donner id_capt= ");
scanf("%s",id_capt_user);
while (tmp != NULL) 
{
   if (strcmp(tmp->capt.id_capt,id_capt_user)==0) 
   {
      if (prev == NULL) 
      {
         tmp = tmp->suivant;
         free(*list);
         *list = tmp;
      } else {
       prev->suivant = tmp->suivant;
            free(tmp);
            tmp = prev->suivant;
        }
    } else {
        prev = tmp;
        tmp = tmp->suivant;
    }
}

}

I know I there must be two pointers prev and next and thanx to this function I have a better understanding and the link sent by Mr.Roberet

CodePudding user response:

void suppression_capt(Liste* liste)//id donner par utilisateur a modifier
{
   float new_longitude,new_latitude;//vals a modifier
   char id_capt_user[10];

   Element *courant = liste->premier;
   Element *temp = courant;
   Element *precedent = courant->precedent;
   Element *suivant = null;

    printf("donner id_capt= ");
    scanf("%s",id_capt_user);

   while(courant != NULL)
   {
       precedent=courant->precedent;
       suivant=courant->suivant;
       if(strcmp(courant->capt.id_capt,id_capt_user)==0)
       {
           if(precedent != null) is  {
               precedent->suivant=courant->suivant;
           }
           if(suivant != null) is  {
               suivant->precedent=courant->precedent;
           }
           temp = courant;
           free(courant);
       }
         courant=temp->suivant;
   }
   printf("\n");
}
  • Related