1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,13 @@ typedef struct max_heap
11
11
12
12
Heap * create_heap (Heap * heap ); /*Creates a max_heap structure and returns a
13
13
pointer to the struct*/
14
+ /**
15
+ * @brief Deallocates memory associated with a given heap.
16
+ *
17
+ * @param heap Pointer to the heap structure to be deallocated.
18
+ */
19
+ void delete_heap (Heap * const heap );
20
+
14
21
void down_heapify (Heap * heap , int index ); /*Pushes an element downwards in the
15
22
heap to find its correct position*/
16
23
void up_heapify (Heap * heap , int index ); /*Pushes an element upwards in the heap
@@ -46,6 +53,7 @@ int main()
46
53
printf ("Popping an element.\n" );
47
54
printf ("Top element = %d \n" , top (head ));
48
55
printf ("\n" );
56
+ delete_heap (head );
49
57
return 0 ;
50
58
}
51
59
Heap * create_heap (Heap * heap )
@@ -57,6 +65,16 @@ Heap *create_heap(Heap *heap)
57
65
return heap ;
58
66
}
59
67
68
+ void delete_heap (Heap * const heap )
69
+ {
70
+ if (heap == NULL )
71
+ {
72
+ return ;
73
+ }
74
+ free (heap -> p );
75
+ free (heap );
76
+ }
77
+
60
78
void down_heapify (Heap * heap , int index )
61
79
{
62
80
if (index >= heap -> count )
0 commit comments