gen_linklist.c
2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "gen_linklist.h"
#include "platform.h"
// https://blog.csdn.net/swwlqw/article/details/22498833
/*-------------- insert ------------------------------
* insert data to any postion
*/
unsigned char gen_linklist__insert(GenLinkList* const list,void *pele,unsigned short index)
{
if(list->length < index) return 0;
platform.disable_interrupt();//platform__system_intf.disable_INT();//
GenLinkNode *node = (GenLinkNode *)malloc(sizeof(GenLinkNode));
while(node ==NULL){//debug only
// my_debug.printf_error("gen_linklist__insert() malloc fail \n");
//platform__system_intfp->delay_ms(500);
platform.enable_interrupt();//platform__system_intf.enable_INT();//
return 0;
}
node->next = NULL;
node->pdata = pele;
GenLinkNode *pnode_tmp= list->front;
for(unsigned short i=0;i<index;i++){
pnode_tmp =pnode_tmp->next;
}//for
node->next =pnode_tmp->next;
pnode_tmp->next =node;
platform.enable_interrupt();//platform__system_intf.enable_INT();//
(list->length)++;
return 1;
}
/* -------------- detele ------------------------------
* detele node at any postion
*/
void* gen_linklist__detele(GenLinkList* const list,unsigned short index)
{
if(list->length < index) return NULL;
platform.disable_interrupt();//platform__system_intf.disable_INT();//
GenLinkNode *pnode_tmp= list->front;
for(unsigned short i=0;i<index;i++){
pnode_tmp =pnode_tmp->next;
}//for
GenLinkNode *pnode_record= pnode_tmp->next;
pnode_tmp->next = pnode_tmp->next->next;
void * re =pnode_record->pdata;
free(pnode_record);
if(list->length)
(list->length)--;
platform.enable_interrupt();//platform__system_intf.enable_INT();//
return re;
}
/*-------------- get ele ------------------------------
* get element of index;
*/
void* gen_linklist__get_ele(GenLinkList* const list,unsigned short index)
{
if( list->length < index) return NULL;
GenLinkNode *pnode_tmp= list->front->next;//diff with no head
for(unsigned short i=0;i<index;i++){
//if(pnode_tmp->next != NULL)
pnode_tmp =pnode_tmp->next;
}//for
if(pnode_tmp != NULL)
return pnode_tmp->pdata;
else
return NULL;
}
/*-------------- get len ------------------------------
* get length of linklist
*/
unsigned short gen_linklist__get_len(GenLinkList* const list)
{
return list->length;
}