diff options
Diffstat (limited to 'comp/cw/code/tokenizer/ads/dll')
-rw-r--r-- | comp/cw/code/tokenizer/ads/dll/Makefile | 5 | ||||
-rw-r--r-- | comp/cw/code/tokenizer/ads/dll/dll.c | 62 | ||||
-rw-r--r-- | comp/cw/code/tokenizer/ads/dll/dll.h | 19 | ||||
-rw-r--r-- | comp/cw/code/tokenizer/ads/dll/dll.o | bin | 0 -> 2424 bytes | |||
-rwxr-xr-x | comp/cw/code/tokenizer/ads/dll/dlltest | bin | 0 -> 20640 bytes | |||
-rw-r--r-- | comp/cw/code/tokenizer/ads/dll/dlltest.c | 20 |
6 files changed, 106 insertions, 0 deletions
diff --git a/comp/cw/code/tokenizer/ads/dll/Makefile b/comp/cw/code/tokenizer/ads/dll/Makefile new file mode 100644 index 0000000..651681d --- /dev/null +++ b/comp/cw/code/tokenizer/ads/dll/Makefile @@ -0,0 +1,5 @@ +all: dll.c + cc dll.c -c -o dll.o + +test: all + cc dll.o dlltest.c -o dlltest diff --git a/comp/cw/code/tokenizer/ads/dll/dll.c b/comp/cw/code/tokenizer/ads/dll/dll.c new file mode 100644 index 0000000..b24b13f --- /dev/null +++ b/comp/cw/code/tokenizer/ads/dll/dll.c @@ -0,0 +1,62 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +typedef struct dll_t dll_t; + +typedef struct dll_t { + void *data; + dll_t *next; + dll_t *prev; +} dll_t; + +dll_t *dllalloc(){ + dll_t *output = malloc(sizeof(dll_t)); + if (output == NULL) + return NULL; + output->next = NULL; + output->prev = NULL; + return output; +} + +void dllsetdata(dll_t *node, void *data){ + node->data = malloc(sizeof(data)); + memcpy(node->data, data, sizeof(data)); +} + +void dllsetnext(dll_t *node, dll_t *next){ + if (node->next == NULL) { + node->next = next; + node->next->prev = node; + } + else + dllsetnext(node->next, next); +} +void dllsetprev(dll_t *node, dll_t *prev){ + if (node->prev == NULL) { + node->prev = prev; + node->prev->next = node; + } + else + dllsetprev(node->prev, prev); +} + +void *dllgetat(dll_t *head, int index){ + if (index == 0) + return head->data; + else { + if (head->next != NULL) { + return dllgetat(head->next, index - 1); + }else { + return NULL; + } + } +} + +void dllfreeall(dll_t *head){ + if (head->next != NULL) + dllfreeall(head->next); + free(head->data); + free(head); +} + diff --git a/comp/cw/code/tokenizer/ads/dll/dll.h b/comp/cw/code/tokenizer/ads/dll/dll.h new file mode 100644 index 0000000..44940ce --- /dev/null +++ b/comp/cw/code/tokenizer/ads/dll/dll.h @@ -0,0 +1,19 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +typedef struct dll_t dll_t; + +typedef struct dll_t { + void *data; + dll_t *next; + dll_t *prev; +} dll_t; + +dll_t *dllalloc(); +void dllsetdata(dll_t *node, void *data); +void dllsetnext(dll_t *node, dll_t *next); +void dllsetprev(dll_t *node, dll_t *prev); +void *dllgetat(dll_t *head, int index); +void dllfreeall(dll_t *head); + diff --git a/comp/cw/code/tokenizer/ads/dll/dll.o b/comp/cw/code/tokenizer/ads/dll/dll.o Binary files differnew file mode 100644 index 0000000..2c9a145 --- /dev/null +++ b/comp/cw/code/tokenizer/ads/dll/dll.o diff --git a/comp/cw/code/tokenizer/ads/dll/dlltest b/comp/cw/code/tokenizer/ads/dll/dlltest Binary files differnew file mode 100755 index 0000000..83f900c --- /dev/null +++ b/comp/cw/code/tokenizer/ads/dll/dlltest diff --git a/comp/cw/code/tokenizer/ads/dll/dlltest.c b/comp/cw/code/tokenizer/ads/dll/dlltest.c new file mode 100644 index 0000000..4544d2e --- /dev/null +++ b/comp/cw/code/tokenizer/ads/dll/dlltest.c @@ -0,0 +1,20 @@ +#include <stdio.h> + +#include "dll.h" + +int main(){ + dll_t *head = dllalloc(); + dllsetdata(head, "hello"); + + for (int i = 0; i < 3; i++){ + dll_t *node = dllalloc(); + dllsetdata(node, "hi"); + dllsetnext(head, node); + } + + for (int i = 0; i < 4; i++) + printf("%s\n", (char *)dllgetat(head, i)); + printf("%s\n", (char *)head->prev->next->data); + + dllfreeall(head); +} |