insertInTail() and findByValue() has been tested and modified.
It seems that both of them work fine now.
This commit is contained in:
Saturneric 2018-02-10 22:00:08 +08:00
parent bc82a686b0
commit 6cb730d793
2 changed files with 35 additions and 13 deletions

View File

@ -4,11 +4,23 @@
* This file create by saturneric at 20:04 on Feb 7th. * This file create by saturneric at 20:04 on Feb 7th.
* *******************************************************/ * *******************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "list.h" #include "list.h"
int main(int argc, char **argv){ int main(int argc, char **argv){
rand_init();
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);
insertInTail(t_list,t_node);
}
int *f_i = (int *)malloc(sizeof(int));
*f_i = 3;
Node *f_node = findByValue(t_list,"int",(void *)f_i);
releaseList(t_list);
return 0; return 0;
} }

View File

@ -1,3 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct Node{ typedef struct Node{
unsigned long long id; unsigned long long id;
void *value; void *value;
@ -17,6 +22,7 @@ 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 *,void *);
void init_rand(void);
unsigned long long getId(void); unsigned long long getId(void);
@ -94,13 +100,16 @@ int init_value(Node *p_node,void * p_value){
return 0; return 0;
} }
void rand_init(void){
srand((unsigned)time(NULL));
}
unsigned long long getId(void){ unsigned long long getId(void){
unsigned long long id = 0; unsigned long long id = 0;
srand(time(0)); id = ((random()%9)+1);
id = ((random()%9)+1)*10; for(int i = 0; i < 15; i++){
for(int i = 0; i < 18; i++){
id = random()%10;
id *= 10; id *= 10;
id += random()%10;
} }
return id; return id;
} }
@ -116,6 +125,7 @@ int insertInHead(List *p_list, Node *p_node){
p_node->next = p_list->head; p_node->next = p_list->head;
p_list->head = p_node; p_list->head = p_node;
} }
p_list->length += 1;
return 0; return 0;
} }
@ -130,6 +140,7 @@ int insertInTail(List *p_list, Node *p_node){
p_node->last = p_list->tail; p_node->last = p_list->tail;
p_list->tail = p_node; p_list->tail = p_node;
} }
p_list->length += 1;
return 0; return 0;
} }
@ -265,12 +276,12 @@ 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(type,"int")){ if(!strcmp(type,"int")){
if(*((int *)p_node) == *((int *)value)){ if(*((int *)p_node->value) == *((int *)value)){
return p_node; return p_node;
} }
} }
else if(!strcmp(type,"double")){ else if(!strcmp(type,"double")){
if(*((double *)p_node) == *((double *)value)){ if(*((double *)p_node->value) == *((double *)value)){
return p_node; return p_node;
} }
} }
@ -280,9 +291,8 @@ Node *findByValue(List *p_list, const char *type, const void *value){
return p_node; return p_node;
} }
} }
else{
return NULL; p_node = p_node->next;
}
} }
return NULL; return NULL;