From 305f6955c92730d14dbd1e01945c1341d86fc11d Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sun, 11 Feb 2018 09:20:19 +0800 Subject: [PATCH] Debug. Modified some functions. --- list/list.c | 13 ++++++++++--- list/list.h | 44 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/list/list.c b/list/list.c index 6ec8597..7a7a4c4 100644 --- a/list/list.c +++ b/list/list.c @@ -8,19 +8,26 @@ int main(int argc, char **argv){ rand_init(); + safeMode(1); List *t_list = init_list(); for(int i = 0; i < 9; i++){ Node *t_node = init_node(); int *t_i = (int *)malloc(sizeof(int)); *t_i = i; - init_value(t_node,(void *)t_i); + init_value(t_node,"int",(void *)t_i); insertInTail(t_list,t_node); } + + /*Node *t_node = init_node(); + insertInTail(t_list,t_node); + init_value(t_node,(void *)"there");*/ + int *f_i = (int *)malloc(sizeof(int)); *f_i = 3; + char *f_s = "there"; Node *f_node = findByValue(t_list,"int",(void *)f_i); - releaseList(t_list); - + releaseAll(); + return 0; } diff --git a/list/list.h b/list/list.h index 1eb98bb..43b7d5e 100644 --- a/list/list.h +++ b/list/list.h @@ -7,6 +7,7 @@ typedef struct Node{ unsigned long long id; void *value; int if_setvalue; + const char *type; struct Node *next; struct Node *last; }Node; @@ -21,7 +22,7 @@ int safeMode(int ifon);//Safe mode is used to make sure that all malloced will b int releaseAll(void); List *init_list(void); Node *init_node(void); -int init_value(Node *,void *); +int init_value(Node *,const char *,void *); void init_rand(void); unsigned long long getId(void); @@ -88,14 +89,15 @@ List *init_list(void){ p_list->tail = NULL; if(if_safeMode){ Node *p_node = init_node(); - init_value(p_node,(void *)p_list); + init_value(p_node,"pointer",(void *)p_list); insertInTail(list_list,p_node); } return p_list; } -int init_value(Node *p_node,void * p_value){ +int init_value(Node *p_node,const char *type,void * p_value){ p_node->if_setvalue = 1; + p_node->type = type; p_node->value = p_value; return 0; } @@ -150,7 +152,10 @@ int releaseNode(Node *p_node){ } if(p_node->if_setvalue == 1){ free(p_node->value); + p_node->value = NULL; } + p_node->last = NULL; + p_node->next = NULL; free(p_node); return 0; } @@ -159,18 +164,27 @@ int releaseList(List *p_list){ Node *p_node, *pl_node; p_node = p_list->head; if(if_safeMode == 1){ - Node *tar_list = findByValue(list_list,"int",(void *)p_list); + Node *tar_list = findByValue(list_list,"pointer",(void *)p_list);//turn pointer in to int to compare. removeByNode(list_list,tar_list); } while (p_node != NULL){ pl_node = p_node; p_node = p_node->next; + pl_node->next = NULL; + pl_node->last = NULL; releaseNode(pl_node); } + p_list->head = NULL; + p_list->tail = NULL; + p_list->length = 0; free(p_list); return 0; } +unsigned long long len(List *p_list){ + return p_list->length; +} + int removeById(List *p_list, unsigned long id){ Node *tmp = p_list->head; if( isListEmpty(p_list) ) @@ -179,7 +193,8 @@ int removeById(List *p_list, unsigned long id){ if(tmp->id == id) { tmp->last->next = tmp->next; tmp->next->last = tmp->last; - releaseNode(tmp); + //releaseNode(tmp); not necessary + p_list->length -= 1; return 1;//found } else{ @@ -198,7 +213,8 @@ int removeByNode(List *p_list, Node *p_node){ if(tmp == p_node){ tmp->last->next = tmp->next; tmp->next->last = tmp->last; - releaseNode(tmp); + //releaseNode(tmp); not necessary + p_list->length -= 1; return 1;//found } else{ @@ -213,10 +229,11 @@ int popFromHead(List *p_list){ if( isListEmpty(p_list) ) return -1; else{ - Node *tmp = p_list->head; + //Node *tmp = p_list->head; p_list->head->next->last = NULL; p_list->head = p_list->head->next; - releaseNode(tmp); + //releaseNode(tmp); not necessary + p_list->length -= 1; } if( isListEmpty(p_list) ){ @@ -230,10 +247,11 @@ int popFromTail(List *p_list){ if( isListEmpty(p_list) ) return -1; else{ - Node *tmp = p_list->tail; + //Node *tmp = p_list->tail; p_list->tail->last->next = NULL; p_list->tail = p_list->tail->last; - releaseNode(tmp); + //releaseNode(tmp); not necessary + p_list->length -= 1; } if( isListEmpty(p_list) ){ @@ -275,6 +293,7 @@ Node *findById(List *p_list, const unsigned long long id){ Node *findByValue(List *p_list, const char *type, const void *value){ Node *p_node = p_list->head; while(p_node != NULL){ + if(strcmp(p_node->type,type)) continue;//continue when type is not the same. if(!strcmp(type,"int")){ if(*((int *)p_node->value) == *((int *)value)){ return p_node; @@ -291,6 +310,11 @@ Node *findByValue(List *p_list, const char *type, const void *value){ return p_node; } } + else if(!strcmp(type,"pointer")){ + if(p_node->value == value){ + return p_node; + } + } p_node = p_node->next;