Debug.
Modified some functions.
This commit is contained in:
parent
6cb730d793
commit
305f6955c9
11
list/list.c
11
list/list.c
@ -8,19 +8,26 @@
|
|||||||
|
|
||||||
int main(int argc, char **argv){
|
int main(int argc, char **argv){
|
||||||
rand_init();
|
rand_init();
|
||||||
|
safeMode(1);
|
||||||
List *t_list = init_list();
|
List *t_list = init_list();
|
||||||
|
|
||||||
for(int i = 0; i < 9; i++){
|
for(int i = 0; i < 9; i++){
|
||||||
Node *t_node = init_node();
|
Node *t_node = init_node();
|
||||||
int *t_i = (int *)malloc(sizeof(int));
|
int *t_i = (int *)malloc(sizeof(int));
|
||||||
*t_i = i;
|
*t_i = i;
|
||||||
init_value(t_node,(void *)t_i);
|
init_value(t_node,"int",(void *)t_i);
|
||||||
insertInTail(t_list,t_node);
|
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));
|
int *f_i = (int *)malloc(sizeof(int));
|
||||||
*f_i = 3;
|
*f_i = 3;
|
||||||
|
char *f_s = "there";
|
||||||
Node *f_node = findByValue(t_list,"int",(void *)f_i);
|
Node *f_node = findByValue(t_list,"int",(void *)f_i);
|
||||||
releaseList(t_list);
|
releaseAll();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
44
list/list.h
44
list/list.h
@ -7,6 +7,7 @@ typedef struct Node{
|
|||||||
unsigned long long id;
|
unsigned long long id;
|
||||||
void *value;
|
void *value;
|
||||||
int if_setvalue;
|
int if_setvalue;
|
||||||
|
const char *type;
|
||||||
struct Node *next;
|
struct Node *next;
|
||||||
struct Node *last;
|
struct Node *last;
|
||||||
}Node;
|
}Node;
|
||||||
@ -21,7 +22,7 @@ int safeMode(int ifon);//Safe mode is used to make sure that all malloced will b
|
|||||||
int releaseAll(void);
|
int releaseAll(void);
|
||||||
List *init_list(void);
|
List *init_list(void);
|
||||||
Node *init_node(void);
|
Node *init_node(void);
|
||||||
int init_value(Node *,void *);
|
int init_value(Node *,const char *,void *);
|
||||||
void init_rand(void);
|
void init_rand(void);
|
||||||
|
|
||||||
unsigned long long getId(void);
|
unsigned long long getId(void);
|
||||||
@ -88,14 +89,15 @@ List *init_list(void){
|
|||||||
p_list->tail = NULL;
|
p_list->tail = NULL;
|
||||||
if(if_safeMode){
|
if(if_safeMode){
|
||||||
Node *p_node = init_node();
|
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);
|
insertInTail(list_list,p_node);
|
||||||
}
|
}
|
||||||
return p_list;
|
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->if_setvalue = 1;
|
||||||
|
p_node->type = type;
|
||||||
p_node->value = p_value;
|
p_node->value = p_value;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -150,7 +152,10 @@ int releaseNode(Node *p_node){
|
|||||||
}
|
}
|
||||||
if(p_node->if_setvalue == 1){
|
if(p_node->if_setvalue == 1){
|
||||||
free(p_node->value);
|
free(p_node->value);
|
||||||
|
p_node->value = NULL;
|
||||||
}
|
}
|
||||||
|
p_node->last = NULL;
|
||||||
|
p_node->next = NULL;
|
||||||
free(p_node);
|
free(p_node);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -159,18 +164,27 @@ int releaseList(List *p_list){
|
|||||||
Node *p_node, *pl_node;
|
Node *p_node, *pl_node;
|
||||||
p_node = p_list->head;
|
p_node = p_list->head;
|
||||||
if(if_safeMode == 1){
|
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);
|
removeByNode(list_list,tar_list);
|
||||||
}
|
}
|
||||||
while (p_node != NULL){
|
while (p_node != NULL){
|
||||||
pl_node = p_node;
|
pl_node = p_node;
|
||||||
p_node = p_node->next;
|
p_node = p_node->next;
|
||||||
|
pl_node->next = NULL;
|
||||||
|
pl_node->last = NULL;
|
||||||
releaseNode(pl_node);
|
releaseNode(pl_node);
|
||||||
}
|
}
|
||||||
|
p_list->head = NULL;
|
||||||
|
p_list->tail = NULL;
|
||||||
|
p_list->length = 0;
|
||||||
free(p_list);
|
free(p_list);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned long long len(List *p_list){
|
||||||
|
return p_list->length;
|
||||||
|
}
|
||||||
|
|
||||||
int removeById(List *p_list, unsigned long id){
|
int removeById(List *p_list, unsigned long id){
|
||||||
Node *tmp = p_list->head;
|
Node *tmp = p_list->head;
|
||||||
if( isListEmpty(p_list) )
|
if( isListEmpty(p_list) )
|
||||||
@ -179,7 +193,8 @@ int removeById(List *p_list, unsigned long id){
|
|||||||
if(tmp->id == id) {
|
if(tmp->id == id) {
|
||||||
tmp->last->next = tmp->next;
|
tmp->last->next = tmp->next;
|
||||||
tmp->next->last = tmp->last;
|
tmp->next->last = tmp->last;
|
||||||
releaseNode(tmp);
|
//releaseNode(tmp); not necessary
|
||||||
|
p_list->length -= 1;
|
||||||
return 1;//found
|
return 1;//found
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
@ -198,7 +213,8 @@ int removeByNode(List *p_list, Node *p_node){
|
|||||||
if(tmp == p_node){
|
if(tmp == p_node){
|
||||||
tmp->last->next = tmp->next;
|
tmp->last->next = tmp->next;
|
||||||
tmp->next->last = tmp->last;
|
tmp->next->last = tmp->last;
|
||||||
releaseNode(tmp);
|
//releaseNode(tmp); not necessary
|
||||||
|
p_list->length -= 1;
|
||||||
return 1;//found
|
return 1;//found
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
@ -213,10 +229,11 @@ int popFromHead(List *p_list){
|
|||||||
if( isListEmpty(p_list) )
|
if( isListEmpty(p_list) )
|
||||||
return -1;
|
return -1;
|
||||||
else{
|
else{
|
||||||
Node *tmp = p_list->head;
|
//Node *tmp = p_list->head;
|
||||||
p_list->head->next->last = NULL;
|
p_list->head->next->last = NULL;
|
||||||
p_list->head = p_list->head->next;
|
p_list->head = p_list->head->next;
|
||||||
releaseNode(tmp);
|
//releaseNode(tmp); not necessary
|
||||||
|
p_list->length -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( isListEmpty(p_list) ){
|
if( isListEmpty(p_list) ){
|
||||||
@ -230,10 +247,11 @@ int popFromTail(List *p_list){
|
|||||||
if( isListEmpty(p_list) )
|
if( isListEmpty(p_list) )
|
||||||
return -1;
|
return -1;
|
||||||
else{
|
else{
|
||||||
Node *tmp = p_list->tail;
|
//Node *tmp = p_list->tail;
|
||||||
p_list->tail->last->next = NULL;
|
p_list->tail->last->next = NULL;
|
||||||
p_list->tail = p_list->tail->last;
|
p_list->tail = p_list->tail->last;
|
||||||
releaseNode(tmp);
|
//releaseNode(tmp); not necessary
|
||||||
|
p_list->length -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( isListEmpty(p_list) ){
|
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 *findByValue(List *p_list, const char *type, const void *value){
|
||||||
Node *p_node = p_list->head;
|
Node *p_node = p_list->head;
|
||||||
while(p_node != NULL){
|
while(p_node != NULL){
|
||||||
|
if(strcmp(p_node->type,type)) continue;//continue when type is not the same.
|
||||||
if(!strcmp(type,"int")){
|
if(!strcmp(type,"int")){
|
||||||
if(*((int *)p_node->value) == *((int *)value)){
|
if(*((int *)p_node->value) == *((int *)value)){
|
||||||
return p_node;
|
return p_node;
|
||||||
@ -291,6 +310,11 @@ Node *findByValue(List *p_list, const char *type, const void *value){
|
|||||||
return p_node;
|
return p_node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if(!strcmp(type,"pointer")){
|
||||||
|
if(p_node->value == value){
|
||||||
|
return p_node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
p_node = p_node->next;
|
p_node = p_node->next;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user