From f74c7c4488b8a551dc1490e7610baba81a975db8 Mon Sep 17 00:00:00 2001 From: mallocnull Date: Thu, 5 Oct 2017 20:43:18 +0000 Subject: [PATCH] linked list code that compiles wow git add .git add .git add . --- src/.list.c.swp | Bin 0 -> 16384 bytes src/list.c | 264 ++++++++++++++++++++++++++++++++++++++++++++++++ src/list.h | 53 ++++++++++ src/main.c | 3 + src/stack.c | 65 ++++++++++++ src/stack.h | 18 +++- 6 files changed, 400 insertions(+), 3 deletions(-) create mode 100644 src/.list.c.swp create mode 100644 src/list.c create mode 100644 src/list.h create mode 100644 src/stack.c diff --git a/src/.list.c.swp b/src/.list.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..a4f3be2cc170750dc84646e542a81152504efd52 GIT binary patch literal 16384 zcmeI2Ym8h~9l%c!DO8GB0#W1R(%QN+-RVwwN!adaqNvrC5MwK}mSMVkhmLG#HZym+ z4Yu$>q=@1ps8kR?SbWe1!6PG-n#h{(fc-BWVp^YX3e*sxVpIV9p;~JGG=#eZmu=c?9R^?km< zeUIJey?y2GYyOd2Ah$qnf!qSQ1#%1I7RW7-TOhYUZh_naxdn0yybdi;n>FT4W*_adXBpiY-K>}C9;?&VXN?Wy}L`5blIq;AZ$dY=+C=Ja}oP zF;Bs7;MedV9ESVgAbbnH3D>}f;S#tQPK9R>_Gvf{55sq0FYJPyP=ib1TsX;Z7@mQr z;W#`5cf$f)2Q|1BE`fK$GWhEXbcSQ_ID8jwhHK$U_!w-4OW{0t3%qoOF;BoTI09X` z20j2|unyM3a`^k{#ykfn;FoX@+zJO^2W*FJP=>Xz4F0&>n4|DR_yODw2jCW1fG#93 z1sA|s@a$>E{2UI$eegB74Za9Bz+PB@X_$n!!`t95Z{#`n9()11kib>22_~TkWAI-1 z*Qv(*0Um&Za1-1JyWx8HEPN2og>zsPoZv9&_wX>>19!t+(1ooq4yVDNiAjme-}=KT z9Y2x3pYWwCOJsJ75?sl73U&w&PAbvfp-`blXj>-Hvy+_QY1gac84Wr~X>GJ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W1zaNdXddO!_tq)@J4Vfz+ih>7ic6Jsm)>iY~imS}(*Kr!cE+U3*^|K$4sv5Cd(!7U!QpyI6<3>y$K+0*mEBMYb`1t&(75**neifC z%2w|=H1$SBJlfZlcWA0s?Shp&$SLE1RfhQr^DGs(fz_3RT^t9gg&R3yhaS zC--2z-rEdkkD>afOzRvIWX(-@OvoGHCKu5$&zqw1j8S`sfYVIGHPQ{Jh kn9=)fejiPcs>QK6HWQwOxs_ndv+^|l*gOkGs}4OeqDxBvhE literal 0 HcmV?d00001 diff --git a/src/list.c b/src/list.c new file mode 100644 index 0000000..de45d99 --- /dev/null +++ b/src/list.c @@ -0,0 +1,264 @@ +#include "list.h" + +list_t* list_init() { + list_t* list = (list_t*)malloc(sizeof(list_t)); + list->front = NULL; + list->back = NULL; + list->iterator = NULL; + list->size = 0; + + return list; +} + +static list_node_t* list_node_init() { + list_node_t* node = (list_node_t*)malloc(sizeof(list_node_t)); + node->next = NULL; + node->data = NULL; + + return node; +} + +static list_node_t* list_get_raw(list_t *list, int pos) { + int i = 0; + list_node_t *ptr = list->front; + for(i = 0; i < pos; ++i) { + if(i == pos) + return ptr; + + ptr = ptr->next; + } + + return NULL; +} + +static void list_add_first(list_t *list, void *data) { + if(list->size != 0) + return; + + list->front = list->back = list_node_init(); + list->front->data = data; +} + +void list_append(list_t *list, void *data) { + if(list->size == 0) { + list_add_first(list, data); + return; + } + + list->back->next = list_node_init(); + list->back = list->back->next; + list->back->data = data; + list->size++; +} + +void list_prepend(list_t *list, void *data) { + if(list->size == 0) { + list_add_first(list, data); + return; + } + + list_node_t* node = list_node_init(); + node->data = data; + node->next = list->front; + list->front = node; + list->size++; +} + +void list_insert(list_t *list, void *data, int pos) { + if(list->size == 0) { + list_add_first(list, data); + return; + } + + if(pos <= 0) + list_prepend(list, data); + + if(pos >= list->size) + list_append(list, data); + + list_node_t *node = list_node_init(); + list_node_t *prior = list_get_raw(list, pos - 1); + node->data = data; + node->next = prior->next; + prior->next = node; +} + +int list_size(list_t *list) { + return list->size; +} + +void* list_get(list_t *list, int pos) { + if(pos < 0 || pos > list->size - 1) + return NULL; + + return list_get_raw(list, pos)->data; +} + +int list_find(list_t *list, void *data) { + int i; + list_node_t *ptr = list->front; + for(i = 0; i < list->size; ++i) { + if(ptr->data == data) + return i; + + ptr = ptr->next; + } + + return -1; +} + +int list_func_find(list_t *list, listfindfptr ffptr) { + int i; + list_node_t *ptr = list->front; + for(i = 0; i < list->size; ++i) { + if((*ffptr)(ptr->data) == TRUE) + return i; + + ptr = ptr->next; + } + + return -1; +} + +void* list_remove(list_t *list, int pos) { + if(pos <= 0) + list_remove_front(list); + + list_node_t *prior = list_get_raw(list, pos-1); + list_node_t *node = prior->next; + prior->next = node->next; + void *data = node->data; + free(node); + + if(prior->next == NULL) + list->back = prior; + + return data; +} + +void* list_remove_front(list_t *list) { + list_node_t *after = list->front->next; + void *data = list->front->data; + free(list->front); + list->front = after; + + return data; +} + +void* list_remove_back(list_t *list) { + return list_remove(list, list->size - 1); +} + +void list_iter_reset(list_t *list) { + list->iterator = NULL; + list->iter_prev = NULL; + list->iter_pos = -1; +} + +void* list_iter_next(list_t *list) { + if(list->iter_pos == -1) { + list->iter_prev = list->iterator; + list->iterator = list->front; + } else if(list->iterator != NULL) { + list->iter_prev = list->iterator; + list->iterator = list->iterator->next; + } else + return NULL; + + list->iter_pos++; + return list->iterator == NULL ? NULL : list->iterator->data; +} + +void* list_iter_remove(list_t *list) { + if(list->iter_prev == NULL && list->iter_pos != 0) + return NULL; + + list_node_t *after = list->iterator->next; + void *data = list->iterator->data; + free(list->iterator); + list->iterator = list->iter_prev; + list->iter_prev = NULL; + + if(list->iterator == NULL) + list->front = after; + else + list->iterator->next = after; + + if(after == NULL) + list->back = list->iterator; + + list->size--; + list->iter_pos--; + return data; +} + +void list_iter_insert_before(list_t *list, void *data) { + if(list->size == 0) { + list_add_first(list, data); + return; + } + + list_node_t *node = list_node_init(); + node->data = data; + node->next = list->iterator; + + if(list->iter_pos == 0) + list->front = node; + else + list->iter_prev->next = node; + + list->iter_prev = node; + list->size++; + list->iter_pos++; +} + +void list_iter_insert_after(list_t *list, void *data) { + if(list->size == 0) { + list_add_first(list, data); + return; + } + + list_node_t *node = list_node_init(); + node->data = data; + node->next = list->iterator->next; + list->iterator->next = node; + + if(node->next == NULL) + list->back = node; + + list->size++; +} + +int list_iter_pos(list_t *list) { + return list->iter_pos; +} + +void list_free(list_t *list) { + list_node_t *ptr = list->front, *next; + while(ptr != NULL) { + next = ptr->next; + free(ptr); + ptr = next; + } +} + +void list_nodes_free(list_t *list) { + list_node_t *ptr = list->front, *next; + while(ptr != NULL) { + next = ptr->next; + free(ptr->data); + free(ptr); + ptr = next; + } +} + +void list_nodes_func_free(list_t *list, listfreefptr ffptr) { + list_node_t *ptr = list->front, *next; + while(ptr != NULL) { + next = ptr->next; + (*ffptr)(ptr->data); + free(ptr); + next = ptr; + } +} + diff --git a/src/list.h b/src/list.h new file mode 100644 index 0000000..cfbe10c --- /dev/null +++ b/src/list.h @@ -0,0 +1,53 @@ +#ifndef LIST_H +#define LIST_H + +#include +#include "common.h" + +typedef void(*listfreefptr)(void*); +typedef BOOL(*listfindfptr)(void*); + +typedef struct list_node_t list_node_t; +struct list_node_t { + list_node_t *next; + void *data; +}; + +typedef struct list_t list_t; +struct list_t { + list_node_t *front; + list_node_t *back; + int size; + + list_node_t *iterator; + list_node_t *iter_prev; + int iter_pos; +}; + +list_t* list_init(); + +void list_append(list_t*, void*); +void list_prepend(list_t*, void*); +void list_insert(list_t*, void*, int); + +int list_size(list_t*); +void* list_get(list_t*, int); +int list_find(list_t*, void*); +int list_func_find(list_t*, listfindfptr); + +void* list_remove(list_t*, int); +void* list_remove_front(list_t*); +void* list_remove_back(list_t*); + +void list_iter_reset(list_t*); +void* list_iter_next(list_t*); +void* list_iter_remove(list_t*); +void list_iter_insert_before(list_t*, void*); +void list_iter_insert_after(list_t*, void*); +int list_iter_pos(list_t*); + +void list_free(list_t*); +void list_nodes_free(list_t*); +void list_nodes_func_free(list_t*, listfreefptr); + +#endif diff --git a/src/main.c b/src/main.c index 907073e..1cb7e48 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,8 @@ #include "client/client.h" #include "server/server.h" +#include "stack.h" + +#include int main(int argc, char **argv) { if(argc == 2 && strcmp(argv[1], "server") == 0) { diff --git a/src/stack.c b/src/stack.c new file mode 100644 index 0000000..e6d8e96 --- /dev/null +++ b/src/stack.c @@ -0,0 +1,65 @@ +#include "stack.h" + +stack_t* stack_init() { + stack_t *stack = (stack_t*)malloc(sizeof(stack_t)); + stack->next = NULL; + stack->data = NULL; + + return stack; +} + +void stack_push(stack_t *stack, void *data) { + stack_t *node = stack_init(); + node->data = data; + node->next = stack->next; + stack->next = node; +} + +void* stack_pop(stack_t *stack) { + if(stack_can_pop(stack) == FALSE) + return NULL; + + stack_t *this = stack->next; + void *data = this->data; + stack->next = this->next; + free(this); + + return data; +} + +BOOL stack_can_pop(stack_t *stack) { + return stack->next != NULL; +} + +void stack_free(stack_t *stack) { + stack_t *ptr = stack, *next; + while(ptr != NULL) { + next = ptr->next; + free(ptr); + ptr = next; + } +} + +void stack_node_free(stack_t *stack) { + stack_t *ptr = stack, *next; + while(ptr != NULL) { + next = ptr->next; + if(ptr->data != NULL) + free(ptr->data); + + free(ptr); + ptr = next; + } +} + +void stack_node_func_free(stack_t *stack, stackffptr ffptr) { + stack_t *ptr = stack, *next; + while(ptr != NULL) { + next = ptr->next; + if(ptr->data != NULL) + (*ffptr)(ptr->data); + + free(ptr); + ptr = next; + } +} diff --git a/src/stack.h b/src/stack.h index af4c5da..bdafec5 100644 --- a/src/stack.h +++ b/src/stack.h @@ -1,13 +1,25 @@ #ifndef STACK_H #define STACK_H -struct stack_node_t { +#include +#include "common.h" -}; +typedef void(*stackffptr)(void*); typedef struct stack_t stack_t; struct stack_t { - + stack_t *next; + void *data; }; +stack_t* stack_init(); + +void stack_push(stack_t*, void*); +void* stack_pop(stack_t*); +BOOL stack_can_pop(stack_t*); + +void stack_free(stack_t*); +void stack_node_free(stack_t*); +void stack_node_func_free(stack_t*, stackffptr); + #endif