diff --git a/ex2/ex2.c b/ex2/ex2.c index ab078d38da6916933ec9fc5fba672b83c286470f..9165343e8480740acd4460cbb803aafa356ea904 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -9,23 +9,35 @@ * */ -#include <math.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> // LinkedListNode.h + typedef struct LinkedListNode { struct LinkedListNode *next; int32_t value; } LinkedListNode; +/** + * @brief Creation of a new node. + * + * @param value + * @return LinkedListNode* + */ LinkedListNode *linked_list_node_new(int32_t value); + +/** + * @brief Deletion of a node. + * + * @param node + */ void linked_list_node_delete(LinkedListNode **node); // LinkedListNode.c + LinkedListNode *linked_list_node_new(int32_t value) { LinkedListNode *node = (LinkedListNode *)malloc(sizeof(LinkedListNode)); @@ -41,19 +53,85 @@ void linked_list_node_delete(LinkedListNode **node) { } // LinkedList.h + typedef struct { LinkedListNode *root; } LinkedList; +/** + * @brief Creation of a new linked list. + * + * @return LinkedList* + */ LinkedList *linked_list_new(); + +/** + * @brief Deletion of the linked list. + * + * @param list + */ void linked_list_delete(LinkedList **list); + +/** + * @brief Count the number of nodes in the list. + * + * @param list + * @return int32_t + */ int32_t linked_list_count(LinkedList *list); + +/** + * @brief Checks if the list is empty or not. + * + * @param list + * @return true + * @return false + */ bool linked_list_is_empty(LinkedList *list); + +/** + * @brief Adds a value at the end of the list. + * + * @param list + * @param value + * @return LinkedListNode* + */ LinkedListNode *linked_list_add_last(LinkedList *list, int32_t value); + +/** + * @brief Removes a node from the list. + * + * @param list + * @param node + */ void linked_list_remove(LinkedList *list, LinkedListNode **node); + +/** + * @brief Displays the list. + * + * @param list + */ void linked_list_print(LinkedList *list); +/** + * @brief Inserts a value at the desired index, if the index does not exist does nothing. + * + * @param list + * @param index + * @param value + */ +void linked_list_insert_at_index(LinkedList *list, int32_t index, int32_t value); + +/** + * @brief Deletes a node at the desired index, if the index does not exist does nothing. + * + * @param list + * @param index + */ +void linked_list_remove_at_index(LinkedList *list, int32_t index); + // LinkedList.c + LinkedList *linked_list_new() { LinkedList *list = (LinkedList *)malloc(sizeof(LinkedList)); @@ -180,15 +258,18 @@ int main() { int32_t values_length = 4; int32_t values[values_length]; + // Reading of the 4 input values to be added to the list. for (int32_t i = 0; i < values_length; i += 1) { int32_t value; scanf("%d", &value); values[i] = value; } + // Reading of the index where the value 0 must be inserted. int32_t insert_index; scanf("%d", &insert_index); + // Reading of the index where the value must be removed. int32_t remove_index; scanf("%d", &remove_index); @@ -196,13 +277,16 @@ int main() { LinkedList *list = linked_list_new(); + // Addition of the 4 numbers read as input in the list. for (int32_t i = 4 - 1; i >= 0; i -= 1) { linked_list_add_last(list, values[i]); } + // Insertion of the number 0 at the index specified in input. linked_list_insert_at_index(list, insert_index, 0); linked_list_print(list); + // Deletion of the value at the index read in input. linked_list_remove_at_index(list, remove_index); linked_list_print(list);