diff options
| author | @syxhe <https://t.me/syxhe> | 2025-03-24 15:53:14 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-03-24 15:53:14 -0500 |
| commit | 586a95dde36c1d4f1dfd2b7590db87f6d9d55d12 (patch) | |
| tree | cb5e11b62edfa28c79cc7190f5c83e4bc873cc66 | |
| parent | 7083f1d8f72d4e45a82fd40cd4822356429f0e23 (diff) | |
Do some more work on the linked list
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | src/Makefile | 1 | ||||
| -rw-r--r-- | src/ll.c | 75 | ||||
| -rw-r--r-- | src/ll.h | 7 | ||||
| -rw-r--r-- | src/shared.h | 5 |
5 files changed, 80 insertions, 12 deletions
| @@ -1,7 +1,9 @@ | |||
| 1 | main | 1 | main |
| 2 | encryption | 2 | encryption |
| 3 | ll | ||
| 3 | a.out | 4 | a.out |
| 4 | bin/ | 5 | bin/ |
| 5 | *.o | 6 | *.o |
| 6 | *test* | 7 | *test* |
| 7 | *.enc \ No newline at end of file | 8 | *.enc |
| 9 | .vscode/ \ No newline at end of file | ||
diff --git a/src/Makefile b/src/Makefile index d12eb72..c5a08c7 100644 --- a/src/Makefile +++ b/src/Makefile | |||
| @@ -28,6 +28,7 @@ arena.o: arena.c arena.h shared.h | |||
| 28 | shared.o: shared.c shared.h | 28 | shared.o: shared.c shared.h |
| 29 | 29 | ||
| 30 | encryption: encryption.c encryption.h shared.o shared.h | 30 | encryption: encryption.c encryption.h shared.o shared.h |
| 31 | ll: ll.o shared.o | ||
| 31 | 32 | ||
| 32 | c clean: | 33 | c clean: |
| 33 | rm -rvf $(BINARIES) $(wildcard *.o) $(wildcard *.test*) $(wildcard *.enc) \ No newline at end of file | 34 | rm -rvf $(BINARIES) $(wildcard *.o) $(wildcard *.test*) $(wildcard *.enc) \ No newline at end of file |
| @@ -47,17 +47,26 @@ int dllnode_free(dllnode **n) { | |||
| 47 | return 0; | 47 | return 0; |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | void dlinkedlist_init(dlinkedlist **ll) { | 50 | dlinkedlist * dlinkedlist_init(void) { |
| 51 | (*ll) = xcalloc(1, sizeof(**ll)); | 51 | dlinkedlist *ll = xcalloc(1, sizeof(*ll)); |
| 52 | 52 | ||
| 53 | (*ll)->start = NULL; | 53 | ll->start = NULL; |
| 54 | (*ll)->end = NULL; | 54 | ll->end = NULL; |
| 55 | (*ll)->size = 0; | 55 | ll->size = 0; |
| 56 | 56 | ||
| 57 | return; | 57 | return ll; |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | void dlinkedlist_free(dlinkedlist **ll) { | 60 | int dlinkedlist_free(dlinkedlist **ll) { |
| 61 | if(!ll) { | ||
| 62 | errno = EINVAL; | ||
| 63 | return -1; | ||
| 64 | } | ||
| 65 | if(!(*ll)) { | ||
| 66 | errno = EINVAL; | ||
| 67 | return -1; | ||
| 68 | } | ||
| 69 | |||
| 61 | dllnode *p = (*ll)->start, *n; | 70 | dllnode *p = (*ll)->start, *n; |
| 62 | while(p != NULL) { | 71 | while(p != NULL) { |
| 63 | n = p->next; | 72 | n = p->next; |
| @@ -67,7 +76,7 @@ void dlinkedlist_free(dlinkedlist **ll) { | |||
| 67 | 76 | ||
| 68 | free(*ll); | 77 | free(*ll); |
| 69 | (*ll) = NULL; | 78 | (*ll) = NULL; |
| 70 | return; | 79 | return 0; |
| 71 | } | 80 | } |
| 72 | 81 | ||
| 73 | int dlinkedlist_firstitem(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb) { | 82 | int dlinkedlist_firstitem(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb) { |
| @@ -210,4 +219,52 @@ int dlinkedlist_remove(dlinkedlist * const ll, size_t index) { | |||
| 210 | ll->size--; | 219 | ll->size--; |
| 211 | 220 | ||
| 212 | return 0; | 221 | return 0; |
| 213 | } \ No newline at end of file | 222 | } |
| 223 | |||
| 224 | size_t dlinkedlist_size(const dlinkedlist * const ll) { | ||
| 225 | if(!ll) | ||
| 226 | RETURNWERR(EINVAL, -1); | ||
| 227 | |||
| 228 | return ll->size; | ||
| 229 | } | ||
| 230 | |||
| 231 | int dlinkedlist_isempty(const dlinkedlist * const ll) { | ||
| 232 | if(!ll) | ||
| 233 | RETURNWERR(EINVAL, -1); | ||
| 234 | |||
| 235 | return (ll->size == 0); | ||
| 236 | } | ||
| 237 | |||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | // Sample code | ||
| 242 | #define COMPILE_SAMPLE | ||
| 243 | #ifdef COMPILE_SAMPLE | ||
| 244 | |||
| 245 | #include <stdio.h> | ||
| 246 | #include <string.h> | ||
| 247 | |||
| 248 | int genfree(void *data) { | ||
| 249 | free(data); | ||
| 250 | return 0; | ||
| 251 | } | ||
| 252 | |||
| 253 | int main() { | ||
| 254 | dlinkedlist *dll = dlinkedlist_init(); | ||
| 255 | dlinkedlist_append(dll, (void*)strdup("THIS"), genfree); | ||
| 256 | dlinkedlist_append(dll, (void*)strdup("IS"), genfree); | ||
| 257 | dlinkedlist_append(dll, (void*)strdup("A"), genfree); | ||
| 258 | dlinkedlist_append(dll, (void*)strdup("STRING"), genfree); | ||
| 259 | |||
| 260 | // This is crashing as of right now | ||
| 261 | char *retval; | ||
| 262 | for(int i = 0; i < dlinkedlist_size(dll); i++) | ||
| 263 | printf("%s ", (((retval = (char*)dlinkedlist_get(dll, i)) != NULL) ? retval : "null") ); | ||
| 264 | printf("\n"); | ||
| 265 | |||
| 266 | dlinkedlist_free(&dll); | ||
| 267 | |||
| 268 | return 0; | ||
| 269 | } | ||
| 270 | #endif \ No newline at end of file | ||
| @@ -6,8 +6,8 @@ | |||
| 6 | typedef int (*dlinkedlist_freecallback)(void*); | 6 | typedef int (*dlinkedlist_freecallback)(void*); |
| 7 | typedef struct dlinkedlist dlinkedlist; | 7 | typedef struct dlinkedlist dlinkedlist; |
| 8 | 8 | ||
| 9 | void dlinkedlist_init(dlinkedlist **ll); | 9 | dlinkedlist * dlinkedlist_init(void); |
| 10 | void dlinkedlist_free(dlinkedlist **ll); | 10 | int dlinkedlist_free(dlinkedlist **ll); |
| 11 | 11 | ||
| 12 | int dlinkedlist_insert(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb); | 12 | int dlinkedlist_insert(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb); |
| 13 | int dlinkedlist_append(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb); | 13 | int dlinkedlist_append(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb); |
| @@ -16,4 +16,7 @@ void *dlinkedlist_getfirst(const dlinkedlist * const ll); | |||
| 16 | void *dlinkedlist_getlast(const dlinkedlist * const ll); | 16 | void *dlinkedlist_getlast(const dlinkedlist * const ll); |
| 17 | int dlinkedlist_remove(dlinkedlist * const ll, size_t index); | 17 | int dlinkedlist_remove(dlinkedlist * const ll, size_t index); |
| 18 | 18 | ||
| 19 | size_t dlinkedlist_size(const dlinkedlist * const ll); | ||
| 20 | int dlinkedlist_isempty(const dlinkedlist * const ll); | ||
| 21 | |||
| 19 | #endif \ No newline at end of file | 22 | #endif \ No newline at end of file |
diff --git a/src/shared.h b/src/shared.h index ee3e06b..af6c56e 100644 --- a/src/shared.h +++ b/src/shared.h | |||
| @@ -5,6 +5,11 @@ | |||
| 5 | 5 | ||
| 6 | #define STATIC_ARRAY_LEN(arr) (sizeof((arr))/sizeof((arr)[0])) | 6 | #define STATIC_ARRAY_LEN(arr) (sizeof((arr))/sizeof((arr)[0])) |
| 7 | 7 | ||
| 8 | #define RETURNWERR(errval, retval) do {\ | ||
| 9 | errno = (errval);\ | ||
| 10 | return (retval);\ | ||
| 11 | } while (0) | ||
| 12 | |||
| 8 | // Defines how `x___alloc()` functions should exit. `___VXGG___XALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions | 13 | // Defines how `x___alloc()` functions should exit. `___VXGG___XALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions |
| 9 | // registered with `atexit()` and `on_exit()`. `___VXGG___XALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `x___alloc()` | 14 | // registered with `atexit()` and `on_exit()`. `___VXGG___XALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `x___alloc()` |
| 10 | // type functions will ALWAYS 'abort', doing otherwise defeats the purpose of the function type | 15 | // type functions will ALWAYS 'abort', doing otherwise defeats the purpose of the function type |
