Vorrei sapere , se ho una lista di elementi fatti così :
E di queste strutture ne ho allocate 10,codice:typedef struct Structure { int Data; struct Structure *Next; }Element;
come faccio poi a svuotare l'intera lista ?
Grazie a tutti
Vorrei sapere , se ho una lista di elementi fatti così :
E di queste strutture ne ho allocate 10,codice:typedef struct Structure { int Data; struct Structure *Next; }Element;
come faccio poi a svuotare l'intera lista ?
Grazie a tutti
Le hai allocate come? E' una lista o un vettore?
"Let him who has understanding reckon the number of the beast, for it is a human number.
Its number is rw-rw-rw-."
Le ho allocate con la malloc, è una lista...Originariamente inviato da Ippo343
Le hai allocate come? E' una lista o un vettore?
Devi deallocare lo spazio allocato ... usi la free.
No MP tecnici (non rispondo nemmeno!), usa il forum.
Basati su questo : (Fatto da me)
Come faccio a svuotare questa benedetta lista ?codice:#include <stdio.h> #include <stdlib.h> typedef struct Structure { int Data; struct Structure *Next; }Element; Element *AddElements (); Element *ViewElements (Element *List); int main () { Element *List; AddElements (); system ("CLS"); ViewElements (List); system ("PAUSE"); return 0; } Element *AddElements () { Element *List = NULL; Element *Pointer; int Elements; int i; printf ("Elementi della lista : "); scanf ("%i", & Elements); if (Elements == 0) { List = NULL; }else{ List = (Element *)malloc(sizeof(Element)); printf ("Inserisci il 1 elemento : "); scanf ("%i", & List -> Data); Pointer = List; for (i = 0 ; i < Elements - 1 ; i++) { Pointer -> Next = (Element *)malloc(sizeof(Element)); Pointer = Pointer -> Next; printf ("Inserisci il %i elemento : ", i + 2); scanf ("%i", & Pointer -> Data); } Pointer -> Next = NULL; } return List; } Element *ViewElements (Element *List) { printf ("Lista :\n\n"); while (List != NULL) { printf ("%i -----> ", List -> Data); } printf ("NULL\n\n"); }
Te l'ho già detto ... prendi tutti i benedetti puntatori diversi da NULL, dall'ultimo al primo, e li usi con la benedetta free per deallocare la memoria.
No MP tecnici (non rispondo nemmeno!), usa il forum.