16
16
17
17
HPriorityQueue * hPriorityQueueInitialization (void (* freeFn )(void * ), int (* cmp )(const void * , const void * )) {
18
18
19
+ if (freeFn == NULL ) {
20
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
21
+ ERROR_TEST -> errorCode = INVALID_ARG ;
22
+ return NULL ;
23
+ #else
24
+ fprintf (stderr , INVALID_ARG_MESSAGE , "free function pointer" , "heap priority queue data structure" );
25
+ exit (INVALID_ARG );
26
+ #endif
27
+ } else if (cmp == NULL ) {
28
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
29
+ ERROR_TEST -> errorCode = INVALID_ARG ;
30
+ return NULL ;
31
+ #else
32
+ fprintf (stderr , INVALID_ARG_MESSAGE , "comparator function pointer" , "heap priority queue data structure" );
33
+ exit (INVALID_ARG );
34
+ #endif
35
+ }
36
+
19
37
HPriorityQueue * queue = (HPriorityQueue * ) malloc (sizeof (HPriorityQueue ));
38
+ if (queue == NULL ) {
39
+ fprintf (stderr , FAILED_ALLOCATION_MESSAGE , "queue" , "heap priority queue data structure" );
40
+ exit (FAILED_ALLOCATION );
41
+ }
20
42
21
43
queue -> heap = binaryHeapInitialization (freeFn , cmp );
22
44
@@ -37,9 +59,22 @@ HPriorityQueue *hPriorityQueueInitialization(void (*freeFn)(void *), int (*cmp)(
37
59
void hpQueueEnqueue (HPriorityQueue * queue , void * item ) {
38
60
39
61
if (queue == NULL ) {
62
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
63
+ ERROR_TEST -> errorCode = NULL_POINTER ;
64
+ return ;
65
+ #else
66
+ fprintf (stderr , NULL_POINTER_MESSAGE , "queue" , "heap priority queue data structure" );
67
+ exit (NULL_POINTER );
68
+ #endif
40
69
41
70
} else if (item == NULL ) {
42
-
71
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
72
+ ERROR_TEST -> errorCode = INVALID_ARG ;
73
+ return ;
74
+ #else
75
+ fprintf (stderr , INVALID_ARG_MESSAGE , "item pointer" , "heap priority queue data structure" );
76
+ exit (INVALID_ARG );
77
+ #endif
43
78
}
44
79
45
80
binaryHeapInsert (queue -> heap , item );
@@ -58,9 +93,22 @@ void hpQueueEnqueue(HPriorityQueue *queue, void *item) {
58
93
void hpQueueEnqueueAll (HPriorityQueue * queue , void * items , int length ) {
59
94
60
95
if (queue == NULL ) {
96
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
97
+ ERROR_TEST -> errorCode = NULL_POINTER ;
98
+ return ;
99
+ #else
100
+ fprintf (stderr , NULL_POINTER_MESSAGE , "queue" , "heap priority queue data structure" );
101
+ exit (NULL_POINTER );
102
+ #endif
61
103
62
104
} else if (items == NULL ) {
63
-
105
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
106
+ ERROR_TEST -> errorCode = INVALID_ARG ;
107
+ return ;
108
+ #else
109
+ fprintf (stderr , INVALID_ARG_MESSAGE , "items array pointer" , "heap priority queue data structure" );
110
+ exit (INVALID_ARG );
111
+ #endif
64
112
}
65
113
66
114
binaryHeapInsertAll (queue -> heap , items , length );
@@ -80,8 +128,22 @@ void hpQueueEnqueueAll(HPriorityQueue *queue, void *items, int length) {
80
128
void * hpQueueDequeue (HPriorityQueue * queue ) {
81
129
82
130
if (queue == NULL ) {
131
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
132
+ ERROR_TEST -> errorCode = NULL_POINTER ;
133
+ return NULL ;
134
+ #else
135
+ fprintf (stderr , NULL_POINTER_MESSAGE , "queue" , "heap priority queue data structure" );
136
+ exit (NULL_POINTER );
137
+ #endif
83
138
84
139
} else if (hpQueueIsEmpty (queue )) {
140
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
141
+ ERROR_TEST -> errorCode = EMPTY_DATA_STRUCTURE ;
142
+ return NULL ;
143
+ #else
144
+ fprintf (stderr , EMPTY_DATA_STRUCTURE_MESSAGE , "heap priority queue data structure" );
145
+ exit (EMPTY_DATA_STRUCTURE );
146
+ #endif
85
147
86
148
}
87
149
@@ -101,8 +163,22 @@ void *hpQueueDequeue(HPriorityQueue *queue) {
101
163
void * hpQueuePeek (HPriorityQueue * queue ) {
102
164
103
165
if (queue == NULL ) {
166
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
167
+ ERROR_TEST -> errorCode = NULL_POINTER ;
168
+ return NULL ;
169
+ #else
170
+ fprintf (stderr , NULL_POINTER_MESSAGE , "queue" , "heap priority queue data structure" );
171
+ exit (NULL_POINTER );
172
+ #endif
104
173
105
174
} else if (hpQueueIsEmpty (queue )) {
175
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
176
+ ERROR_TEST -> errorCode = EMPTY_DATA_STRUCTURE ;
177
+ return NULL ;
178
+ #else
179
+ fprintf (stderr , EMPTY_DATA_STRUCTURE_MESSAGE , "heap priority queue data structure" );
180
+ exit (EMPTY_DATA_STRUCTURE );
181
+ #endif
106
182
107
183
}
108
184
@@ -120,7 +196,20 @@ void *hpQueuePeek(HPriorityQueue *queue) {
120
196
*/
121
197
122
198
int hpQueueGetLength (HPriorityQueue * queue ) {
199
+
200
+ if (queue == NULL ) {
201
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
202
+ ERROR_TEST -> errorCode = NULL_POINTER ;
203
+ return -1 ;
204
+ #else
205
+ fprintf (stderr , NULL_POINTER_MESSAGE , "queue" , "heap priority queue data structure" );
206
+ exit (NULL_POINTER );
207
+ #endif
208
+
209
+ }
210
+
123
211
return binaryHeapGetSize (queue -> heap );
212
+
124
213
}
125
214
126
215
@@ -135,7 +224,20 @@ int hpQueueGetLength(HPriorityQueue *queue) {
135
224
*/
136
225
137
226
int hpQueueIsEmpty (HPriorityQueue * queue ) {
227
+
228
+ if (queue == NULL ) {
229
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
230
+ ERROR_TEST -> errorCode = NULL_POINTER ;
231
+ return -1 ;
232
+ #else
233
+ fprintf (stderr , NULL_POINTER_MESSAGE , "queue" , "heap priority queue data structure" );
234
+ exit (NULL_POINTER );
235
+ #endif
236
+
237
+ }
238
+
138
239
return binaryHeapIsEmpty (queue -> heap );
240
+
139
241
}
140
242
141
243
@@ -150,6 +252,18 @@ int hpQueueIsEmpty(HPriorityQueue *queue) {
150
252
*/
151
253
152
254
void * * hpQueueToArray (HPriorityQueue * queue ) {
255
+
256
+ if (queue == NULL ) {
257
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
258
+ ERROR_TEST -> errorCode = NULL_POINTER ;
259
+ return NULL ;
260
+ #else
261
+ fprintf (stderr , NULL_POINTER_MESSAGE , "queue" , "heap priority queue data structure" );
262
+ exit (NULL_POINTER );
263
+ #endif
264
+
265
+ }
266
+
153
267
void * * arr = binaryHeapToArray (queue -> heap );
154
268
155
269
int length = hpQueueGetLength (queue );
@@ -174,6 +288,17 @@ void **hpQueueToArray(HPriorityQueue *queue) {
174
288
175
289
void clearHPQueue (HPriorityQueue * queue ) {
176
290
291
+ if (queue == NULL ) {
292
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
293
+ ERROR_TEST -> errorCode = NULL_POINTER ;
294
+ return ;
295
+ #else
296
+ fprintf (stderr , NULL_POINTER_MESSAGE , "queue" , "heap priority queue data structure" );
297
+ exit (NULL_POINTER );
298
+ #endif
299
+
300
+ }
301
+
177
302
clearBinaryHeap (queue -> heap );
178
303
179
304
}
@@ -188,6 +313,17 @@ void clearHPQueue(HPriorityQueue *queue) {
188
313
189
314
void destroyHPQueue (HPriorityQueue * queue ) {
190
315
316
+ if (queue == NULL ) {
317
+ #ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
318
+ ERROR_TEST -> errorCode = NULL_POINTER ;
319
+ return ;
320
+ #else
321
+ fprintf (stderr , NULL_POINTER_MESSAGE , "queue" , "heap priority queue data structure" );
322
+ exit (NULL_POINTER );
323
+ #endif
324
+
325
+ }
326
+
191
327
destroyBinaryHeap (queue -> heap );
192
328
193
329
free (queue );
0 commit comments