mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
fix: remove memory leaks
This commit is contained in:
parent
e5dad3fa8d
commit
9d291a0e82
@ -36,6 +36,21 @@ struct Graph *createGraph(int V, int E)
|
||||
return graph;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Deallocates memory associated with a given graph.
|
||||
*
|
||||
* @param ptr Pointer to the graph structure to be deallocated.
|
||||
*/
|
||||
void deleteGraph(struct Graph *graph)
|
||||
{
|
||||
if (graph == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
free(graph->edge);
|
||||
free(graph);
|
||||
}
|
||||
|
||||
// A structure to represent a subset for union-find
|
||||
struct subset
|
||||
{
|
||||
@ -131,6 +146,7 @@ void KruskalMST(struct Graph *graph)
|
||||
}
|
||||
// Else discard the next_edge
|
||||
}
|
||||
free(subsets);
|
||||
|
||||
// print the contents of result[] to display the
|
||||
// built MST
|
||||
@ -182,6 +198,7 @@ int main()
|
||||
graph->edge[4].weight = 4;
|
||||
|
||||
KruskalMST(graph);
|
||||
deleteGraph(graph);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user