Skip to content

Commit cf09c15

Browse files
committed
Added terminating errors to heap priority queue data structure.
1 parent 0d35e13 commit cf09c15

File tree

1 file changed

+138
-2
lines changed

1 file changed

+138
-2
lines changed

DataStructure/Queues/HPriorityQueue.c

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,29 @@
1616

1717
HPriorityQueue *hPriorityQueueInitialization(void (*freeFn)(void *), int (*cmp)(const void *, const void *)) {
1818

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+
1937
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+
}
2042

2143
queue->heap = binaryHeapInitialization(freeFn, cmp);
2244

@@ -37,9 +59,22 @@ HPriorityQueue *hPriorityQueueInitialization(void (*freeFn)(void *), int (*cmp)(
3759
void hpQueueEnqueue(HPriorityQueue *queue, void *item) {
3860

3961
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
4069

4170
} 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
4378
}
4479

4580
binaryHeapInsert(queue->heap, item);
@@ -58,9 +93,22 @@ void hpQueueEnqueue(HPriorityQueue *queue, void *item) {
5893
void hpQueueEnqueueAll(HPriorityQueue *queue, void *items, int length) {
5994

6095
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
61103

62104
} 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
64112
}
65113

66114
binaryHeapInsertAll(queue->heap, items, length);
@@ -80,8 +128,22 @@ void hpQueueEnqueueAll(HPriorityQueue *queue, void *items, int length) {
80128
void *hpQueueDequeue(HPriorityQueue *queue) {
81129

82130
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
83138

84139
} 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
85147

86148
}
87149

@@ -101,8 +163,22 @@ void *hpQueueDequeue(HPriorityQueue *queue) {
101163
void *hpQueuePeek(HPriorityQueue *queue) {
102164

103165
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
104173

105174
} 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
106182

107183
}
108184

@@ -120,7 +196,20 @@ void *hpQueuePeek(HPriorityQueue *queue) {
120196
*/
121197

122198
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+
123211
return binaryHeapGetSize(queue->heap);
212+
124213
}
125214

126215

@@ -135,7 +224,20 @@ int hpQueueGetLength(HPriorityQueue *queue) {
135224
*/
136225

137226
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+
138239
return binaryHeapIsEmpty(queue->heap);
240+
139241
}
140242

141243

@@ -150,6 +252,18 @@ int hpQueueIsEmpty(HPriorityQueue *queue) {
150252
*/
151253

152254
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+
153267
void **arr = binaryHeapToArray(queue->heap);
154268

155269
int length = hpQueueGetLength(queue);
@@ -174,6 +288,17 @@ void **hpQueueToArray(HPriorityQueue *queue) {
174288

175289
void clearHPQueue(HPriorityQueue *queue) {
176290

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+
177302
clearBinaryHeap(queue->heap);
178303

179304
}
@@ -188,6 +313,17 @@ void clearHPQueue(HPriorityQueue *queue) {
188313

189314
void destroyHPQueue(HPriorityQueue *queue) {
190315

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+
191327
destroyBinaryHeap(queue->heap);
192328

193329
free(queue);

0 commit comments

Comments
 (0)