commit b2d67cca8d3a2dc7e4c56b420b20235d7ab6d6a4 Author: parys Date: Mon Jan 22 12:46:01 2024 +0100 Let's get started diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..640c253 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +main +valgrind-out.txt diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1410b8a --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2023 Maciej Samborski + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c7acbef --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# ll + +A generic singly linked list in C. + diff --git a/example.c b/example.c new file mode 100644 index 0000000..cefee0a --- /dev/null +++ b/example.c @@ -0,0 +1,54 @@ +#include + +#define LL_IMPLEMENTATION +#include "ll.h" + +#define llPrint(type, format, list) do { \ + for (size_t i = 0; i < llLen(list); ++i) \ + { \ + printf(format"\n", llGet(type, list, i)); \ + } \ +} while (0) + +int main(void) +{ + llList list = llCreateList(); + + llAppend(list, 75); + llAppend(list, 120); + llAppend(list, 2377); + + llPrepend(list, 665); + + int x = llGet(int, list, 0); + printf("x: %d\n", x); + + int * y = llGetRef(int, list, 1); + printf("*y: %d\n", *y); + + printf("%zu\n", llLen(list)); + + llPrint(int, "%d", list); + + llList list2 = llCreateList(); + + llAppend(list2, 'H'); + llAppend(list2, 'i'); + llAppend(list2, ' '); + llAppend(list2, 'o'); + llAppend(list2, 'm'); + llAppend(list2, '!'); + + llInsert(list2, 3, 'm'); + + for (size_t i = 0; i < llLen(list2); ++i) + { + printf("%c", llGet(char, list2, i)); + } + printf("\n"); + + llFree(&list); + llFree(&list2); + + return 0; +} diff --git a/ll.h b/ll.h new file mode 100644 index 0000000..90c2e95 --- /dev/null +++ b/ll.h @@ -0,0 +1,230 @@ +#ifndef LL_H_ +#define LL_H_ + +#include +#include +#include + +typedef struct llNode llNode; +typedef struct llList llList; + +struct llNode { + void * data; + llNode * next; +}; + +struct llList { + llNode * nodes; +}; + +llList llCreateList(); +llNode * _llCreateNode(void * value); +void llFree(llList * list); +void _llAppend(llList * list, void * value); +llList * _llPrepend(llList * list, void * value); +llList * _llInsert(llList * list, size_t id, void * value); +llList * _llDelete(llList * list, size_t id); +size_t _llGet(llList * list, size_t id); +void * _llGetRef(llList * list, size_t id); +size_t llLen(llList list); + +#define llAppend(list, value) _llAppend(&list, (void *) value) +#define llPrepend(list, value) list = *_llPrepend(&list, (void *) value) +#define llInsert(list, id, value) list = *_llInsert(&list, id, (void *) value) +#define llDelete(list, id) list = *_llDelete(&list, id) +#define llGet(type, list, id) (type) _llGet(&list, id) +#define llGetRef(type, list, id) (type *) _llGetRef(&list, id) +#define llCreateNode(value) _llCreateNode((void *) value) + +#ifdef LL_IMPLEMENTATION + +llList llCreateList() +{ + return (llList) { + .nodes = NULL, + }; +} + +llNode * _llCreateNode(void * value) +{ + llNode * node = malloc(sizeof(llNode)); + + node->data = value; + node->next = NULL; + + return node; +} + +void llFreeNodes(llNode * node) +{ + if (node->next != NULL) + llFreeNodes(node->next); + + free(node); +} + +void llFree(llList * list) +{ + if (list->nodes == NULL) return; + + llNode * node = list->nodes; + + if (node->next != NULL) + llFreeNodes(node); + else + free(node); +} + +void _llAppend(llList * list, void * value) +{ + if (list->nodes == NULL) + { + list->nodes = llCreateNode(value); + return; + } + + llNode * node = list->nodes; + + while (node->next != NULL) + node = node->next; + + node->next = llCreateNode(value); +} + +llList * _llPrepend(llList * list, void * value) +{ + llNode * node = llCreateNode(value); + + node->next = list->nodes; + list->nodes = node; + + return list; +} + +llList * _llInsert(llList * list, size_t id, void * value) +{ + llNode * prev = NULL; + llNode * node = list->nodes; + + if (id == 0) + { + list->nodes = llCreateNode(value); + list->nodes->next = node; + + return list; + } + + while (id > 0) + { + if (node->next == NULL && id > 1) + { + fprintf(stderr, "RUNTIME ERROR: Index out of bounds\n"); + llFree(list); + exit(1); + } + + prev = node; + node = node->next; + --id; + } + + prev->next = _llCreateNode(value); + prev->next->next = node; + + return list; +} + +llList * _llDelete(llList * list, size_t id) +{ + llNode * prev = NULL; + llNode * node = list->nodes; + + if (list->nodes == NULL) return list; + + if (id == 0) + { + list->nodes = node->next; + free(node); + + return list; + } + + while (id > 0) + { + if (node->next == NULL) + { + fprintf(stderr, "RUNTIME ERROR: Index out of bounds\n"); + llFree(list); + exit(1); + } + + prev = node; + node = node->next; + --id; + } + + prev->next = node->next; + + free(node); + + return list; +} + +size_t _llGet(llList * list, size_t id) +{ + llNode * node = list->nodes; + + while (id > 0) + { + if (node->next == NULL) + { + fprintf(stderr, "RUNTIME ERROR: Index out of bounds\n"); + llFree(list); + exit(1); + } + + node = node->next; + --id; + } + + return (size_t) node->data; +} + +void * _llGetRef(llList * list, size_t id) +{ + llNode * node = list->nodes; + + while (id > 0) + { + if (node->next == NULL) + { + fprintf(stderr, "RUNTIME ERROR: Index out of bounds\n"); + llFree(list); + exit(1); + } + + node = node->next; + --id; + } + + return (void *) &node->data; +} + +size_t llLen(llList list) +{ + llNode * node = list.nodes; + + size_t len = 0; + + while (node != NULL) + { + ++len; + node = node->next; + } + + return len; +} + +#endif // LL_IMPLEMENTATION + +#endif // LL_H_