1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-13 23:43:13 +01:00

feat: add queue_free

This commit is contained in:
Théo LUDWIG 2023-08-03 22:57:32 +02:00
parent 6a40df3ad1
commit 1e475a59b1
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
4 changed files with 23 additions and 1 deletions

View File

@ -63,7 +63,7 @@ struct linked_list *linked_list_reverse(struct linked_list *list);
void linked_list_reverse_mutate(struct linked_list *list); void linked_list_reverse_mutate(struct linked_list *list);
/** /**
* @brief Free the linked list. * @brief Frees the linked list.
* @since v2.1.0 * @since v2.1.0
*/ */
void linked_list_free(struct linked_list *list); void linked_list_free(struct linked_list *list);

View File

@ -43,3 +43,16 @@ void *queue_pop(struct queue *queue) {
queue->length = queue->length - 1; queue->length = queue->length - 1;
return data; return data;
} }
void queue_free(struct queue *queue) {
if (queue == NULL) {
exit(EXIT_FAILURE);
}
struct queue_node *node = queue->first;
while (node != NULL) {
struct queue_node *node_next = node->next;
free(node);
node = node_next;
}
free(queue);
}

View File

@ -42,4 +42,10 @@ void queue_push(struct queue *queue, void *data);
*/ */
void *queue_pop(struct queue *queue); void *queue_pop(struct queue *queue);
/**
* @brief Frees the queue.
* @since v2.1.0
*/
void queue_free(struct queue *queue);
#endif #endif

View File

@ -10,6 +10,7 @@ void queue_initialization_test() {
struct queue *queue = queue_initialization(); struct queue *queue = queue_initialization();
assert(queue->length == 0); assert(queue->length == 0);
assert(queue->first == NULL); assert(queue->first == NULL);
queue_free(queue);
} }
void queue_push_test() { void queue_push_test() {
@ -22,6 +23,7 @@ void queue_push_test() {
assert(((uintptr_t)queue->first->next->data) == 8); assert(((uintptr_t)queue->first->next->data) == 8);
assert(((uintptr_t)queue->first->next->next->data) == 15); assert(((uintptr_t)queue->first->next->next->data) == 15);
assert(queue->first->next->next->next == NULL); assert(queue->first->next->next->next == NULL);
queue_free(queue);
} }
void queue_pop_test() { void queue_pop_test() {
@ -35,4 +37,5 @@ void queue_pop_test() {
assert(((uintptr_t)queue->first->data) == 8); assert(((uintptr_t)queue->first->data) == 8);
assert(((uintptr_t)queue->first->next->data) == 15); assert(((uintptr_t)queue->first->next->data) == 15);
assert(queue->first->next->next == NULL); assert(queue->first->next->next == NULL);
queue_free(queue);
} }