#define MAX_ID_LEN 24
#pragma pack(1)
typedef struct TAG_INFO
{
S_Time time;
uint32_t sys_sec;
uint8_t fin;
uint8_t type;
uint8_t freq;
uint8_t ant;
uint8_t rssi;
uint8_t id[MAX_ID_LEN];
}S_TAG_INFO, *P_TAG_INFO;
typedef struct TAG_NODE
{
S_TAG_INFO tag;
struct TAG_NODE *next;
}S_TAG_NODE, *P_TAG_NODE;
#pragma pack()
#define MAX_TAG_MEMP 10
static S_TAG_NODE tag_memp_tab[MAX_TAG_MEMP] = {0};
static S_TAG_NODE tag_memp_list_head;
void tag_memp_init(void)
{
uint16_t i;
P_TAG_NODE cur_node = &tag_memp_list_head;
for(i=0; i<MAX_TAG_MEMP; i++)
{
memset(&tag_memp_tab[i].tag, 0, sizeof(S_TAG_INFO));
tag_memp_tab[i].next = NULL;
cur_node->next = &tag_memp_tab[i];
cur_node = cur_node->next;
}
}
P_TAG_NODE tag_memp_malloc(void)
{
P_TAG_NODE cur_node = &tag_memp_list_head;
P_TAG_NODE temp = NULL;
if( cur_node->next == NULL )
return NULL;
temp = cur_node->next;
cur_node->next = cur_node->next->next;
temp->next = NULL;
return temp;
}
void tag_memp_free(P_TAG_NODE new_node)
{
P_TAG_NODE cur_node = &tag_memp_list_head;
if( new_node == NULL )
{
LOG_E("野指针");
return ;
}
new_node->next = cur_node->next;
cur_node->next = new_node;
}
static uint32_t node_count = 0;
P_TAG_NODE find_node(P_TAG_NODE head, P_TAG_INFO tag)
{
P_TAG_NODE cur_node = NULL;
if( head == NULL || tag == NULL )
{
LOG_E("野指针");
return NULL;
}
cur_node = head->next;
while( cur_node != NULL )
{
if( memcmp(cur_node->tag.id, tag->id, MAX_ID_LEN) == 0)
{
return cur_node;
}
cur_node = cur_node->next;
}
return NULL;
}
uint8_t add_node(P_TAG_NODE head, P_TAG_INFO tag)
{
P_TAG_NODE new_node = NULL;
if( head == NULL || tag == NULL )
{
LOG_E("野指针");
return 1;
}
if( find_node(head, tag) != NULL )
{
return 1;
}
new_node = tag_memp_malloc();
memcpy(new_node, tag, sizeof(S_TAG_NODE));
new_node->next = head->next;
head->next = new_node;
node_count++;
LOG_D("新增节点成功,节点数量%d", node_count);
return 0;
}
void del_node(P_TAG_NODE head, P_TAG_INFO tag)
{
P_TAG_NODE pre_node = NULL;
P_TAG_NODE cur_node = NULL;
if( head == NULL || tag == NULL )
{
LOG_E("野指针");
return ;
}
pre_node = head;
cur_node = head->next;
while( cur_node != NULL )
{
if( memcmp(cur_node->tag.id, tag->id, MAX_ID_LEN) == 0 )
{
break;
}
pre_node = cur_node;
cur_node = cur_node->next;
}
if( cur_node != NULL )
{
pre_node->next = cur_node->next;
tag_memp_free(cur_node);
node_count--;
LOG_D("删除节点成功,节点数量%d", node_count);
return 0;
}
else
{
return 1;
}
}
void list_init(P_TAG_NODE head)
{
memset(head, 0, sizeof(S_TAG_NODE));
head->next = NULL;
node_count = 0;
LOG_D("链表初始化,节点数量%d", node_count);
}
void list_free(P_TAG_NODE head)
{
P_TAG_NODE temp_node = NULL;
P_TAG_NODE cur_node = NULL;
cur_node = head;
while( cur_node->next != NULL )
{
temp_node = cur_node->next;
cur_node->next = cur_node->next->next;
tag_memp_free(temp_node);
node_count--;
LOG_D("释放节点成功,节点数量%d", node_count);
cur_node = cur_node->next;
}
}
#define TAG_INFO_NUM 100
static S_TAG_NODE my_list_head = {0};
static P_TAG_INFO tag_info[TAG_INFO_NUM] = {0};
void main(void)
{
uint8_t i;
tag_memp_init();
list_init(&my_list_head);
for(i=0; i<TAG_INFO_NUM; i++)
{
memset(&tag_info[i], 0, sizeof(P_TAG_INFO));
tag_info[i].id[0] = i;
tag_info[i]->next = NULL;
}
for(i=0; i<TAG_INFO_NUM; i++)
{
add_node(&my_list_head, &tag_info[i]);
}
for(i=0; i<TAG_INFO_NUM; i++)
{
del_node(&my_list_head, &tag_info[i]);
}
for(i=0; i<TAG_INFO_NUM; i++)
{
add_node(&my_list_head, &tag_info[i]);
}
list_free(&my_list_head);
}