From 8c47a184d723a622f6dbf8817277e07eee9e368d Mon Sep 17 00:00:00 2001 From: lvntky Date: Sun, 18 Aug 2024 00:17:41 +0300 Subject: [PATCH] [feature] more linkedlist functions --- include/ccontainer/cc_linkedlist.h | 79 ++++++++++++++++++++++++++++-- test/source/cc_linkedlist_test.h | 15 ++++++ test/source/cc_vector_test.h | 1 + test/source/ccontainer_test_main.c | 2 + 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 test/source/cc_linkedlist_test.h diff --git a/include/ccontainer/cc_linkedlist.h b/include/ccontainer/cc_linkedlist.h index b5ebce0..60cab00 100644 --- a/include/ccontainer/cc_linkedlist.h +++ b/include/ccontainer/cc_linkedlist.h @@ -61,6 +61,11 @@ typedef struct cc_linkedlist_node { struct cc_linkedlist_node *next; } cc_linkedlist_node_t; +typedef struct cc_linkedlist { + cc_linkedlist_node_t *head; + size_t size; +} cc_linkedlist_t; + // ===================================================================== // Library Function Declarations // ===================================================================== @@ -72,7 +77,15 @@ typedef struct cc_linkedlist_node { * @param data_size size of the data for allocating memory for it * @return cc_linkedlist_node_t* */ -cc_linkedlist_node_t *cc_linkedlist_create_node(void *data, size_t data_size); +cc_linkedlist_node_t *cc_linkedlist_node_create(void *data, size_t data_size); + +/** + * @brief initialize an empty list + * + * @param list list that head node points to NULL + * @param data_size given by user + */ +void cc_linkedlist_init(cc_linkedlist_t *list, size_t data_size); /** * @brief gets the current size of linked list @@ -82,6 +95,51 @@ cc_linkedlist_node_t *cc_linkedlist_create_node(void *data, size_t data_size); */ size_t cc_linkedlist_size(cc_linkedlist_node_t *head); +/** + * @brief insert data beggining of the list + * + * @param list list that data will be instered + * @param data data that will be insert + */ +void cc_linkedlist_inser_front(cc_linkedlist_t *list, void *data); + +/** + * @brief insert data end of the list + * + * @param list list that data will be instered + * @param data data that will be insert + */ +void cc_linkedlist_inser_back(cc_linkedlist_t *list, void *data); + +/** + * @brief delete the first node with given data + * + * @param list + * @param data + * @param cmp + */ +void cc_linkedlist_node_delete(cc_linkedlist_t *list, void *data, + int (*cmp)(void *, void *)); + +/** + * @brief search node with given data + * + * @param list + * @param data + * @param cmp + * @return cc_linkedlist_node_t* node that contains requested data + */ +cc_linkedlist_node_t *cc_linkedlist_node_search(cc_linkedlist_t *list, + void *data, + int (*cmp)(void *, void *)); + +/** + * @brief delete the entire list from heap + * + * @param list to delete + */ +void cc_linkedlist_free(cc_linkedlist_t *list); + // ===================================================================== // Function Definitions // ===================================================================== @@ -126,6 +184,20 @@ size_t cc_linkedlist_size(cc_linkedlist_node_t *head) return counter; } +void cc_linkedlist_init(cc_linkedlist_t *list, size_t data_size) +{ + list->head = NULL; + list->size = data_size; +} + +void cc_linkedlist_inser_front(cc_linkedlist_t *list, void *data) +{ + cc_linkedlist_node_t *newnode = + cc_linkedlist_create_node(data, list->size); + newnode->next = list->head; + list->head = newnode; +} + #endif // CC_LINKEDLIST_IMPLEMENTATION #ifdef __cplusplus @@ -174,7 +246,4 @@ AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ -*/ - -// TODO: -// I should make a yasnippet out of this \ No newline at end of file +*/ \ No newline at end of file diff --git a/test/source/cc_linkedlist_test.h b/test/source/cc_linkedlist_test.h new file mode 100644 index 0000000..da13688 --- /dev/null +++ b/test/source/cc_linkedlist_test.h @@ -0,0 +1,15 @@ +#pragma once +#define CC_LINKEDLIST_IMPLEMENTATION +#include "../../include/ccontainer/cc_linkedlist.h" +#include "unity.h" +#include + +void cc_linkedlist_init_test(void) +{ + cc_linkedlist_t *list = + (cc_linkedlist_t *)malloc(sizeof(cc_linkedlist_t)); + cc_linkedlist_init(list, 10); + + TEST_ASSERT_EQUAL(list->size, 10); + TEST_ASSERT_EQUAL(list->head, NULL); +} \ No newline at end of file diff --git a/test/source/cc_vector_test.h b/test/source/cc_vector_test.h index c9daa58..30a2f1b 100644 --- a/test/source/cc_vector_test.h +++ b/test/source/cc_vector_test.h @@ -1,3 +1,4 @@ +#pragma once #define CC_VECTOR_IMPLEMENTATION #include "../../include/ccontainer/cc_vector.h" #include "unity.h" diff --git a/test/source/ccontainer_test_main.c b/test/source/ccontainer_test_main.c index d4abcad..3b70cdf 100644 --- a/test/source/ccontainer_test_main.c +++ b/test/source/ccontainer_test_main.c @@ -1,5 +1,6 @@ #include "unity.h" #include "cc_vector_test.h" +#include "cc_linkedlist_test.h" void setUp(void) { @@ -21,6 +22,7 @@ int main(void) RUN_TEST(cc_vector_iterator_begin_test); RUN_TEST(cc_vector_front_test); RUN_TEST(cc_vector_back_test); + RUN_TEST(cc_linkedlist_init_test); return UNITY_END(); }