summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
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
commit586a95dde36c1d4f1dfd2b7590db87f6d9d55d12 (patch)
treecb5e11b62edfa28c79cc7190f5c83e4bc873cc66
parent7083f1d8f72d4e45a82fd40cd4822356429f0e23 (diff)
Do some more work on the linked list
-rw-r--r--.gitignore4
-rw-r--r--src/Makefile1
-rw-r--r--src/ll.c75
-rw-r--r--src/ll.h7
-rw-r--r--src/shared.h5
5 files changed, 80 insertions, 12 deletions
diff --git a/.gitignore b/.gitignore
index b635958..2bba462 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,9 @@
1main 1main
2encryption 2encryption
3ll
3a.out 4a.out
4bin/ 5bin/
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
28shared.o: shared.c shared.h 28shared.o: shared.c shared.h
29 29
30encryption: encryption.c encryption.h shared.o shared.h 30encryption: encryption.c encryption.h shared.o shared.h
31ll: ll.o shared.o
31 32
32c clean: 33c 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
diff --git a/src/ll.c b/src/ll.c
index ae62e89..4819226 100644
--- a/src/ll.c
+++ b/src/ll.c
@@ -47,17 +47,26 @@ int dllnode_free(dllnode **n) {
47 return 0; 47 return 0;
48} 48}
49 49
50void dlinkedlist_init(dlinkedlist **ll) { 50dlinkedlist * 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
60void dlinkedlist_free(dlinkedlist **ll) { 60int 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
73int dlinkedlist_firstitem(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb) { 82int 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
224size_t dlinkedlist_size(const dlinkedlist * const ll) {
225 if(!ll)
226 RETURNWERR(EINVAL, -1);
227
228 return ll->size;
229}
230
231int 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
248int genfree(void *data) {
249 free(data);
250 return 0;
251}
252
253int 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
diff --git a/src/ll.h b/src/ll.h
index f5846fb..6e79ccd 100644
--- a/src/ll.h
+++ b/src/ll.h
@@ -6,8 +6,8 @@
6typedef int (*dlinkedlist_freecallback)(void*); 6typedef int (*dlinkedlist_freecallback)(void*);
7typedef struct dlinkedlist dlinkedlist; 7typedef struct dlinkedlist dlinkedlist;
8 8
9void dlinkedlist_init(dlinkedlist **ll); 9dlinkedlist * dlinkedlist_init(void);
10void dlinkedlist_free(dlinkedlist **ll); 10int dlinkedlist_free(dlinkedlist **ll);
11 11
12int dlinkedlist_insert(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb); 12int dlinkedlist_insert(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb);
13int dlinkedlist_append(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb); 13int dlinkedlist_append(dlinkedlist * const ll, void *data, dlinkedlist_freecallback dfreecb);
@@ -16,4 +16,7 @@ void *dlinkedlist_getfirst(const dlinkedlist * const ll);
16void *dlinkedlist_getlast(const dlinkedlist * const ll); 16void *dlinkedlist_getlast(const dlinkedlist * const ll);
17int dlinkedlist_remove(dlinkedlist * const ll, size_t index); 17int dlinkedlist_remove(dlinkedlist * const ll, size_t index);
18 18
19size_t dlinkedlist_size(const dlinkedlist * const ll);
20int 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