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;
|
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
|
// A structure to represent a subset for union-find
|
||||||
struct subset
|
struct subset
|
||||||
{
|
{
|
||||||
@ -131,6 +146,7 @@ void KruskalMST(struct Graph *graph)
|
|||||||
}
|
}
|
||||||
// Else discard the next_edge
|
// Else discard the next_edge
|
||||||
}
|
}
|
||||||
|
free(subsets);
|
||||||
|
|
||||||
// print the contents of result[] to display the
|
// print the contents of result[] to display the
|
||||||
// built MST
|
// built MST
|
||||||
@ -182,6 +198,7 @@ int main()
|
|||||||
graph->edge[4].weight = 4;
|
graph->edge[4].weight = 4;
|
||||||
|
|
||||||
KruskalMST(graph);
|
KruskalMST(graph);
|
||||||
|
deleteGraph(graph);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user