From d60fcba0c8841462efc55fa35440e514254dc986 Mon Sep 17 00:00:00 2001 From: Eduard Dlabal Date: Tue, 22 Apr 2025 21:16:25 +0200 Subject: [PATCH] Implement UUID generation in addPersonToList and ensure uniqueness across existing nodes --- include/users.h | 10 ++++++++++ src/users.c | 29 +++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/include/users.h b/include/users.h index 54f0072..39ac006 100644 --- a/include/users.h +++ b/include/users.h @@ -26,6 +26,16 @@ typedef struct node struct node *next; } node_t; +/** + * @brief Generates a unique identifier (UUID) for a new node in the linked list. + * + * This function traverses the linked list starting from the given head node + * and generates a unique 16-bit unsigned integer identifier that does not + * conflict with any existing UUIDs in the list. + * + * @param _head Pointer to the head node of the linked list. + * @return A 16-bit unsigned integer representing the generated UUID. + */ uint16_t generateUUID(node_t *_head); uint16_t searchUUIDByName(node_t *_head); diff --git a/src/users.c b/src/users.c index 68a9d0e..7b11b0f 100644 --- a/src/users.c +++ b/src/users.c @@ -9,11 +9,35 @@ #include #include -int addPersonToList( +uint16_t generateUUID +( + node_t *_head +) +{ + uint16_t uuid = rand() % (UINT16_MAX - 1 + 1) + 1; + node_t *current = _head; + while (current != NULL) + { + if (current->user.uuid == uuid) + { + uuid = rand() % (UINT16_MAX - 1 + 1) + 1; + current = _head; + } + else + { + current = current->next; + } + } + return uuid; +} + +int addPersonToList +( node_t **_head, char *_name, char *_surname, - uint32_t _department) + uint32_t _department +) { node_t *new_node = (node_t *)malloc(sizeof(node_t)); if (new_node == NULL) @@ -26,6 +50,7 @@ int addPersonToList( return 0; } + new_node->user.uuid = generateUUID(*_head); new_node->user.name = (char *)malloc(strlen(_name) + 1); new_node->user.surname = (char *)malloc(strlen(_surname) + 1);