LINKED LISTS IN C
Suppose I have a simple List and already insert several Nodes. Which of these options to Free Memory is the correct 1 or 2? He always told me not to touch the node * head
#include <stdio.h> #include <stdlib.h> typedef struct _Nodo{ int dato; struct _Nodo *siguiente; }Nodo; //Prototipos void LiberarNodos( Nodo *cabeza); int main(){ Nodo *cabeza=NULL; return 0; } //OPCION 1 void LiberarNodos( Nodo *cabeza){ Nodo *test; test=cabeza; while( test !=NULL){ free(test); test=test->siguiente; } } //OPCION 2 void LiberarNodos( Nodo *cabeza){ Nodo *test; while( cabeza !=NULL){ test=cabeza; cabeza=cabeza->siguiente; free(test); } } In Option 1 : it is proposed by me, but I have not seen it in any tutorial
In Option 2 : If it appears in some but it seems that it does not release the First Node, it starts with the Second Node.