I would like to free an Adjency Linked List memory Here is my data structs and two function which can allocate memory for the graph. How can I free the allocated memory ? Thanks for the help
struct ListPoint {
int dest;
int weight;
struct ListPoint* next;
};
struct List {
struct ListPoint* head;
};
struct Graf {
int V;
struct List* array;
};
struct ListPoint* newAdjencyListPoint(int dest, int weight)
{
struct ListPoint* newPoint =
(struct ListPoint*)malloc(sizeof(struct ListPoint));
newPoint->dest = dest;
newPoint->weight = weight;
newPoint->next = NULL;
return newPoint;
}
struct Graf* createGraph(int V)
{
struct Graf* graf = (struct Graf*)malloc(sizeof(struct Graf));
graf->V = V;
graf->array= (struct List*)malloc(V * sizeof(struct List));
int i;
for (i = 0; i < V; ++i)
graf->array[i].head = NULL;
return graf;
}
The following code will problaby be what you are looking for:
freeLinkedList(struct List list){
struct ListPoint *aux,*it = list.head;
while(it != NULL){ //free a node and go to the next one
aux = it->next;
free(it);
it = aux;
}
}
freeAdjList(struct Graf* adj_list){
for(int i=0;i<adj_list->V;i++) //free each linked list
freeLinkedList(adj_list->array[i]);
free(adj_list->array); //free the linked list array
free(adj_list); //free the adj matrix itself
}