Usuwanie całej listy jednokierunkowej.

0

Witam,
Chcialbym aby na koncu funkcji zawsze byla czyszczona cala lista jednokierunkowa, zrobilem taki cos ale nie trybi:

struct lista2 *help100 = PoczatekListy2;
    struct lista2 *help101 = PoczatekListy2->next;
    while(help100 != NULL)
    {
        if(help101->next == NULL)
        {
            free(help101);
            help100->next = NULL;
        }
        help100 = help100->next;
        help101 = help101->next;
    }
4
void destroy(struct node* root){
    if(root){
        struct node* temp = root;
        struct node* help = root;
        do{
            help = temp;
            temp = temp->next;
            free(help);
        }while(temp);
    }
}
3
void destroy (struct node *current_node) {
  if (current_node) {
    destroy(current_node->next);
    free(current_node);
  }
}
4

Trochę krótsza wersja by grzesiek51114:

void destroy(struct node* root){
    struct node* currNode;
    struct node* next = root;

    while(currNode = next) {
       next = currNode->next;
       free(currNode);
    }
        
}
0

Ale Panowie, mam sobie taką listę, na ktorej mam elementy.

struct lista2{
    rank y;
    struct lista2 *next;
};
typedef struct lista2 element2;
typedef element2 *ADRES2;
element2 *PoczatekListy2 = NULL;

Chce ją usunąć to jak to zastosować? Tak?:

if(PoczatekListy2){
        struct lista2 *temp = PoczatekListy2;
        struct lista2 *help = PoczatekListy2;
        do{
            help = temp;
            temp = temp->next;
            free(help);
        }while(temp);
    }
2
void destroy (element2 *current_node) {
  if (current_node) {
    destroy(current_node->next);
    free(current_node);
  }
}

wywołujesz
destroy(PoczatekListy2);
no litości

1 użytkowników online, w tym zalogowanych: 0, gości: 1