概述
#include
#include
#include
typedef struct WhiteListNode{
char isAllow;//1 allow,0 reject
char *macAddress;//mac
struct WhiteListNode* next;
}WhiteListNode_t,*pWhiteListNode_t;
pWhiteListNode_t head = NULL;//privte var
pWhiteListNode_t init()
{
head = malloc(sizeof(WhiteListNode_t));
if(head != NULL)
{
head->next = NULL;
}
return head;
}
pWhiteListNode_t create_node(char *macAddress,char isAllow)
{
pWhiteListNode_t p = (pWhiteListNode_t)malloc(sizeof(WhiteListNode_t));
if(p != NULL)
{
p->next = NULL;
p->macAddress = macAddress;
p->isAllow = isAllow;
}
return p;
}
//-----------------------插入------------------------------------
int append_node_after(pWhiteListNode_t p,pWhiteListNode_t new)
{
if(p == NULL || new == NULL)
{
return -1;
}
new->next = p->next;
p->next = new;
return 0;
}
int insert_data(char *macAddress,char isAllow)
{
int ret = 0;
pWhiteListNode_t new = NULL;
if(head == NULL)
{
return -1;
}
new = create_node(macAddress,isAllow);
ret = append_node_after(head,new);
return ret;
}
//-----------------------删除------------------------------------
int del_after_node(pWhiteListNode_t p)
{
pWhiteListNode_t tmp = NULL;
if(p == NULL)
{
return -1;
}
tmp = p->next;
if(tmp!=NULL)
{
p->next = tmp->next;
free(tmp);
}
return 0;
}
//-----------------------查找------------------------------------
pWhiteListNode_t find_node(char *macAddress)
{
pWhiteListNode_t tmp = NULL;
if(head == NULL)
{
printf("no listn");
return NULL;
}
tmp = head;
while(tmp->next!=NULL)
{
if(strcmp(tmp->next->macAddress,macAddress) == 0)
{
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
int del_data(char *macAddress)
{
int ret = 0;
pWhiteListNode_t p = find_node(macAddress);//find
if(p != NULL)
{
ret = del_after_node(p);
}
return ret;
}
int list_empty(pWhiteListNode_t head)
{
if(head == NULL)
{
return -1; //链表不存在
}
if(head->next == NULL)
{
return 1; //链表为空
}
else
{
return 0;//链表非空
}
}
void print_list(pWhiteListNode_t head)
{
pWhiteListNode_t tmp = NULL;
if(head == NULL)
{
printf("list is not exist!n");
return;
}
if(list_empty(head))
{
printf("list is empty!n");
return;
}
tmp = head->next;
while(tmp!=NULL)
{
printf("%d %sn",tmp->isAllow,tmp->macAddress);
tmp = tmp->next;
}
return;
}
//-----------------------destory------------------------------------
int clear_list(pWhiteListNode_t head)
{
if(head == NULL)
{
return -1;
}
while(head->next != NULL)
{
del_after_node(head);
}
return 0;
}
int destroy()
{
if(head == NULL)
{
return -1;
}
clear_list(head);
free(head);
head = NULL;
return 0;
}
int main(void)
{
head = init();
insert_data("abc16", 0);
insert_data("ab:12:32", 1);
insert_data("abc11", 0);
insert_data("abc2", 0);
insert_data("abc3", 3);
print_list(head);
printf("-----------------n");
print_list(head);
printf("----------------n");
del_data("abc11");
del_data("abc2");
print_list(head);
//pWhiteListNode_t node = find_node("abc3");
pWhiteListNode_t node = find_node("abc33");
printf("----------------n");
if(node != NULL){
printf("find data:%s %d n" ,node->macAddress,node->isAllow);
}else{
printf("find rs is null n");
}
destroy();
return 0;
}
注意:
1,设备的mac地址唯一
2,isAllow表示是否允许设备加入网络,由用户决定是否允许其加入
最后
以上就是体贴棒棒糖为你收集整理的zigbee c 语言,zigbee用简单链表实现白名单(C语言链表的简单实现)的全部内容,希望文章能够帮你解决zigbee c 语言,zigbee用简单链表实现白名单(C语言链表的简单实现)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复