2018-02-07 12:09:44 +00:00
|
|
|
typedef struct Node{
|
2018-02-09 03:20:04 +00:00
|
|
|
unsigned long long id;
|
2018-02-07 12:09:44 +00:00
|
|
|
void *value;
|
2018-02-09 03:20:04 +00:00
|
|
|
int if_setvalue;
|
2018-02-08 02:37:44 +00:00
|
|
|
struct Node *next;
|
|
|
|
struct Node *last;
|
|
|
|
}Node;
|
2018-02-07 12:09:44 +00:00
|
|
|
|
|
|
|
typedef struct List{
|
|
|
|
Node *head;
|
|
|
|
Node *tail;
|
2018-02-09 03:20:04 +00:00
|
|
|
unsigned long long length;
|
2018-02-08 02:37:44 +00:00
|
|
|
}List;
|
2018-02-07 12:09:44 +00:00
|
|
|
|
2018-02-08 09:35:30 +00:00
|
|
|
int safeMode(int ifon);//Safe mode is used to make sure that all malloced will be freed.
|
2018-02-09 03:20:04 +00:00
|
|
|
List *init_list(void);
|
|
|
|
Node *init_node(void);
|
|
|
|
int init_value(Node *,void *);
|
|
|
|
unsigned long long getId(void);
|
2018-02-07 12:09:44 +00:00
|
|
|
int insertInHead(List *p_list, Node *p_node);
|
2018-02-07 15:28:28 +00:00
|
|
|
int insertInTail(List *p_list, Node *p_node);
|
2018-02-07 12:09:44 +00:00
|
|
|
int removeById(List *p_list, unsigned long id);
|
|
|
|
int removeByNode(List *p_list, Node *p_node);
|
|
|
|
int popFromHead(List *p_list);
|
|
|
|
int popFromTail(List *p_list);
|
2018-02-09 03:20:04 +00:00
|
|
|
unsigned long long len(List *p_list);
|
|
|
|
Node *findById(List *p_list, const unsigned long long id);
|
2018-02-08 09:10:32 +00:00
|
|
|
Node *findByValue(List *p_list, const char *type, const void *value);
|
2018-02-08 04:06:30 +00:00
|
|
|
int releaseList(List *p_list);
|
|
|
|
int releaseNode(Node *p_node);
|
2018-02-07 15:28:28 +00:00
|
|
|
|
2018-02-08 09:35:30 +00:00
|
|
|
/*Something about safe mode*/
|
|
|
|
int if_safeMode = 0;
|
|
|
|
List *node_list; //Store nodes which haven't been freed.
|
|
|
|
List *list_list; //Store lists which haven't been freed.
|
|
|
|
|
2018-02-09 03:20:04 +00:00
|
|
|
Node *init_node(void){
|
|
|
|
Node *p_node = (Node *) malloc(sizeof(Node));
|
|
|
|
p_node->id = getId();
|
|
|
|
p_node->if_setvalue = 0;
|
|
|
|
if(if_safeMode) insertInTail(node_list,p_node);
|
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
List *init_list(void){
|
|
|
|
List *p_list = (List *) malloc(sizeof(List));
|
2018-02-08 09:35:30 +00:00
|
|
|
p_list->head = NULL;
|
|
|
|
p_list->tail = NULL;
|
|
|
|
Node *p_node = init_node();
|
2018-02-09 03:20:04 +00:00
|
|
|
init_value(p_node,(void *)p_list);
|
2018-02-08 09:35:30 +00:00
|
|
|
if(if_safeMode) insertInHead(list_list,p_node);
|
|
|
|
return p_list;
|
|
|
|
}
|
|
|
|
|
2018-02-09 03:20:04 +00:00
|
|
|
unsigned long long getId(void){
|
|
|
|
unsigned long long id = 0;
|
|
|
|
srand(time(0));
|
|
|
|
id = ((random()%9)+1)*10;
|
|
|
|
for(int i = 0; i < 18; i++){
|
|
|
|
id = random()%10;
|
|
|
|
id *= 10;
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:28:28 +00:00
|
|
|
int insertInHead(List *p_list, Node *p_node){
|
|
|
|
p_list->head->last = p_node;
|
2018-02-08 04:06:30 +00:00
|
|
|
p_node->last = NULL;
|
2018-02-07 15:28:28 +00:00
|
|
|
p_node->next = p_list->head;
|
|
|
|
p_list->head = p_node;
|
2018-02-08 02:37:44 +00:00
|
|
|
return 0;
|
2018-02-07 15:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int insertInTail(List *p_list, Node *p_node){
|
|
|
|
p_list->tail->next = p_node;
|
2018-02-08 04:06:30 +00:00
|
|
|
p_node->next = NULL;
|
2018-02-07 15:28:28 +00:00
|
|
|
p_node->last = p_list->tail;
|
|
|
|
p_list->tail = p_node;
|
2018-02-08 02:37:44 +00:00
|
|
|
return 0;
|
2018-02-07 15:28:28 +00:00
|
|
|
}
|
2018-02-08 04:06:30 +00:00
|
|
|
|
|
|
|
int releaseNode(Node *p_node){
|
|
|
|
free(p_node->value);
|
|
|
|
free(p_node);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int releaseList(List *p_list){
|
|
|
|
Node *p_node, *pl_node;
|
|
|
|
p_node = p_list->head;
|
|
|
|
while (p_node != NULL){
|
|
|
|
pl_node = p_node;
|
|
|
|
p_node = p_node->next;
|
|
|
|
releaseNode(pl_node);
|
|
|
|
}
|
|
|
|
free(p_list);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-08 05:50:05 +00:00
|
|
|
|
2018-02-08 03:23:06 +00:00
|
|
|
int removeById(List *p_list, unsigned long id){
|
|
|
|
Node *tmp = p_list->head;
|
2018-02-08 05:50:05 +00:00
|
|
|
if(tmp == NULL) return -1;//这说明p_list指向空列表
|
2018-02-08 03:23:06 +00:00
|
|
|
do{
|
2018-02-08 05:50:05 +00:00
|
|
|
if(tmp->id == id) {
|
2018-02-08 03:23:06 +00:00
|
|
|
tmp->last->next = tmp->next;
|
|
|
|
tmp->next->last = tmp->last;
|
|
|
|
return 1;//找到了
|
|
|
|
}
|
2018-02-08 05:50:05 +00:00
|
|
|
else{
|
2018-02-08 03:23:06 +00:00
|
|
|
tmp = tmp->next;
|
2018-02-08 05:50:05 +00:00
|
|
|
}
|
|
|
|
} while(tmp != NULL);
|
2018-02-08 03:23:06 +00:00
|
|
|
|
|
|
|
return 0;//没找到
|
|
|
|
}
|
|
|
|
|
|
|
|
int removeByNode(List *p_list, Node *p_node){
|
|
|
|
Node *tmp = p_list->head;
|
|
|
|
if(tmp == NULL)
|
|
|
|
return -1;//这说明p_list指向空列表
|
|
|
|
|
|
|
|
do{
|
|
|
|
if(tmp == p_node){
|
|
|
|
tmp->last->next = tmp->next;
|
|
|
|
tmp->next->last = tmp->last;
|
|
|
|
return 1;//找到了
|
|
|
|
}
|
2018-02-08 05:50:05 +00:00
|
|
|
else{
|
2018-02-08 03:23:06 +00:00
|
|
|
tmp = tmp->next;
|
2018-02-08 05:50:05 +00:00
|
|
|
}
|
|
|
|
}while(tmp != NULL);
|
2018-02-08 03:23:06 +00:00
|
|
|
|
|
|
|
return 0;//没找到
|
|
|
|
}
|
|
|
|
|
|
|
|
int popFromHead(List *p_list){
|
|
|
|
if(p_list->head == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(p_list->head->next == NULL){
|
|
|
|
p_list->head = NULL;
|
|
|
|
p_list->tail = NULL;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
p_list->head->next->last = NULL;
|
|
|
|
p_list->head = p_list->head->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int popFromTail(List *p_list){
|
|
|
|
if(p_list->head == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(p_list->head->next == NULL){
|
|
|
|
p_list->head = NULL;
|
|
|
|
p_list->tail = NULL;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
p_list->tail->last->next = NULL;
|
|
|
|
p_list->tail = p_list->tail->last;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-08 09:10:32 +00:00
|
|
|
|
|
|
|
/*The method in this function won't be better than going through the list
|
|
|
|
* node by node.The worst situation happens when the matched node is in
|
|
|
|
* the middle of the list.*/
|
2018-02-09 03:20:04 +00:00
|
|
|
Node *findById(List *p_list, const unsigned long long id){
|
2018-02-08 09:10:32 +00:00
|
|
|
Node *ph_node = p_list->head;
|
|
|
|
Node *pt_node = p_list->tail;
|
|
|
|
int direction = 0;
|
|
|
|
while(ph_node != pt_node){
|
|
|
|
if (direction == 0){
|
|
|
|
if (ph_node->id == id){
|
|
|
|
return ph_node;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
ph_node = ph_node->next;
|
|
|
|
}
|
|
|
|
direction = 1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
if (pt_node->id == id){
|
|
|
|
return pt_node;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
pt_node = pt_node->last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *findByValue(List *p_list, const char *type, const void *value){
|
|
|
|
Node *p_node = p_list->head;
|
|
|
|
while(p_node != NULL){
|
|
|
|
if(!strcmp(type,"int")){
|
|
|
|
if(*((int *)p_node) == *((int *)value)){
|
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!strcmp(type,"double")){
|
|
|
|
if(*((double *)p_node) == *((double *)value)){
|
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!strcmp (type,"string")){
|
|
|
|
if(!strcmp((char *)p_node->value,(char *)value))
|
|
|
|
{
|
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|