使用链表实现,方便数据实时动态增加与删除。
函数 | 功能 | 使用 |
---|---|---|
Create | 创建链表头结点 | stu_to_link( ) |
Delete | 删除指定结点 | dev_update( ) |
Insert | 增加设备结点 | stu_to_link( ) , dev_update( ) |
dev_print | 打印链表 | init_sys( ) |
stu_to_link | 结构体更新到链表 | init_sys( ),ipcs_pro( ) |
link_to_stu | 链表更新到结构体 | dev_pro( ) |
find_id | 通过id在链表中查询存在 | dev_update( ) ,sw_pro( ) |
get_dev_id | 通过id获取设备节点数据 | sw_pro( ),area_sw_update( ) |
#ifndef __SL2000_LINK_H_
#define __SL2000_LINK_H_
struct sys_dev *Create(struct sys_dev main_dev);
struct sys_dev *Delete(struct sys_dev * head,int type);
struct sys_dev *Insert(struct sys_dev * head,struct sys_dev new_dev);
void dev_print(struct sys_dev *head);
struct sys_dev * stu_to_link(struct sys_dev *head);
void link_to_stu(struct sys_dev *head);
int find_id(char id,struct sys_dev *head,struct sys_dev *dev);
struct sys_dev * get_dev_id(char id,struct sys_dev *head);
#endif
#include "config.h"
#include "link_pro.h"
extern struct sys_all g_dev[1]; //系统配置结构体全局变量
struct sys_dev *Create(struct sys_dev main_dev)
{
struct sys_dev *pNew=NULL;
pNew=(struct sys_dev *)malloc(sizeof(struct sys_dev));
if(NULL == pNew)
printf("create malloc err...\n");
pNew->connect_sta = main_dev.connect_sta;//主控设备设置连接
strcpy(pNew->name,main_dev.name);
pNew->join_sta = main_dev.join_sta;
pNew->node.id = main_dev.node.id;
pNew->node.type = main_dev.node.type;
pNew->sw_sta = main_dev.sw_sta;
pNew->bind_dev = main_dev.bind_dev;
pNew->next = NULL;
printf("plink create ok...\n");
return pNew;
}
struct sys_dev *Insert(struct sys_dev * head,struct sys_dev new_dev)
{
struct sys_dev *pNew = NULL;
struct sys_dev *pFind = NULL;
pNew=(struct sys_dev *)malloc(sizeof(struct sys_dev));
if(NULL == pNew)
printf("create malloc err...\n");
pNew->connect_sta = new_dev.connect_sta;//主控设备设置连接
strcpy(pNew->name,new_dev.name);
pNew->join_sta = new_dev.join_sta;
pNew->node.id = new_dev.node.id;
pNew->node.type = new_dev.node.type;
pNew->sw_sta = new_dev.sw_sta;
pNew->bind_dev = new_dev.bind_dev;
pNew->next = NULL;
//新的节点,放在链表最后
if(NULL==head)
return pNew;
else{
pFind=head;
while(pFind->next!=NULL)
{
pFind=pFind->next;
}
pFind->next = pNew;
}
printf("plink insert ok...\n");
return head;
}
struct sys_dev *Delete(struct sys_dev * head,int type)
{
struct sys_dev *pFind=NULL;
struct sys_dev *pPrev=NULL;
pFind=head;
if(NULL==head){
printf("设备列表为空!\n");
return head;
}
while(pFind->node.type!=type && pFind->next!=NULL)
{
pPrev=pFind;
pFind=pFind->next;
}
if(pFind->node.type==type)
{
if(head==pFind){
head=pFind->next;
}else{
pPrev->next=pFind->next;
}
free(pFind);
printf("%d 设备节点已删除.\n",type);
}else{
printf("设备列表中没有要删除的设备.\n");
}
return head;
}
void dev_print(struct sys_dev *head)
{
struct sys_dev * p;
p=head;
if(NULL==head){
printf("列表为空!\n");
}else{
printf("%d 个设备已连接。\n",g_dev->count_dev);
while(p!=NULL){
printf("类型:%d 名称:%s\n",p->node.type,p->name);
p=p->next;
}
}
}
struct sys_dev * stu_to_link(struct sys_dev *head)
{
int i;
//通过配置文件,生成动态设备列表
for(i=0;i<g_dev->count_dev;i++){
if(i==0)
head = Create(g_dev->sys_dev[i]);
else
head = Insert(head,g_dev->sys_dev[i]);
}
return head;
}
void link_to_stu(struct sys_dev *head)
{
struct sys_dev * p;
int i=0;
int max = g_dev->count_dev;
p=head;
if(NULL==head){
printf("列表为空!\n");
}else{
while(p!=NULL){
g_dev->sys_dev[i].connect_sta = p->connect_sta;
strcpy(g_dev->sys_dev[i].name,p->name);
g_dev->sys_dev[i].join_sta = p->join_sta;
g_dev->sys_dev[i].node.id = p->node.id;
g_dev->sys_dev[i].node.type = p->node.type;
g_dev->sys_dev[i].sw_sta = p->sw_sta;
g_dev->sys_dev[i].bind_dev = p->bind_dev;
p=p->next;
if(i<=max)
i++;
}
}
}
int find_id(char id,struct sys_dev *head,struct sys_dev *dev)//
{
struct sys_dev * p;
p=head;
if(NULL==p){
printf("列表为空!\n");
return 0;
}else{
while(p!=NULL){
if(p->node.type==id){
if(dev!=NULL)
memcpy(dev,p,sizeof(struct sys_dev));
return 1;
}
p=p->next;
}
}
return 0;
}
struct sys_dev * get_dev_id(char id,struct sys_dev *head)//
{
struct sys_dev * p = NULL;
p=head;
if(NULL==p){
printf("列表为空!\n");
return NULL;
}else{
while(p!=NULL){
if(p->node.type==id){
return p;
}
p=p->next;
}
}
return NULL;
}
Copyright © 2016 www.91arm.com 【91创客学堂】