From 6fcbe861aedcd17c60e7eb301209cad060caf9b0 Mon Sep 17 00:00:00 2001 From: "DLABAL, Eduard" Date: Tue, 13 May 2025 10:41:18 +0200 Subject: [PATCH] Implement removePersonFromList function to delete a person node from the linked list; update documentation in users.h --- include/file_operations.h | 6 ------ include/users.h | 11 +++++++++++ src/users.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) delete mode 100644 include/file_operations.h diff --git a/include/file_operations.h b/include/file_operations.h deleted file mode 100644 index e17a4d5..0000000 --- a/include/file_operations.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef __FILE_OPERATIONS_H__ -#define __FILE_OPERATIONS_H__ - - - -#endif \ No newline at end of file diff --git a/include/users.h b/include/users.h index 4ba26b6..9ec16d6 100644 --- a/include/users.h +++ b/include/users.h @@ -111,5 +111,16 @@ int addPersonToList(node_t **_head, char *_name, char *_surname, uint32_t _depar */ int addTimeEvent(node_t *_person); +/** + * @brief Removes a person node from a linked list. + * + * This function removes the node pointed to by _person from the linked list + * whose head is pointed to by _head. Both parameters are double pointers to + * allow modification of the head pointer and the node pointer. + * + * @param _head Double pointer to the head of the linked list. + * @param _person Double pointer to the node representing the person to remove. + * @return int Returns 0 on success, or a negative value on failure. + */ int removePersonFromList(node_t **_head, node_t **_person); #endif \ No newline at end of file diff --git a/src/users.c b/src/users.c index 585148b..c703ad5 100644 --- a/src/users.c +++ b/src/users.c @@ -159,4 +159,36 @@ int addTimeEvent( _person->user.last_time_event = current_time; return 1; +} + +int removePersonFromList(node_t **_head, node_t **_person) +{ + if (_head == NULL || *_head == NULL || _person == NULL || *_person == NULL) + return 0; + + node_t *current = *_head; + node_t *prev = NULL; + + while (current != NULL) + { + if (current == *_person) + { + if (prev == NULL) + { + *_head = current->next; + } + else + { + prev->next = current->next; + } + free(current->user.name); + free(current->user.surname); + free(current); + *_person = NULL; + return 1; + } + prev = current; + current = current->next; + } + return 0; } \ No newline at end of file