这个是链表测试,仿照linux下的list.h文件,有兴趣的可以去搜搜list.h
Common_List.h
程序代码:
#ifndef _COMMON_LIST_H_
#define _COMMON_LIST_H_
#include"Common.h"
#include<stddef.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef void* _List_Node[2];
typedef _List_Node* _List;
#define List_Node _List_Node
#define List _List
#ifndef LIST_HEAD_INIT
#define LIST_HEAD_INIT(name) \
{ \
&(name), \
&(name) \
}
#endif
#ifndef LIST_HEAD
#define LIST_HEAD(name)List_Node name = LIST_HEAD_INIT(name)
#endif
/*#define LIST_HEAD_INIT(name) { &(name), &(name); }
功能
初始化一个结点名字为name的双向循环链表的头结点
参数
name:结点名字
*/
#ifndef container_of
#define container_of(ptr, type, member) ((type* )((char*)ptr-offsetof(type,member)))
#endif
/*
#define container_of(ptr, type, member)
功能
已知某一个成员变量的名字、指针和结构体类型的情况下,计算结构体的指针,也就是计算结构体的起始地址。
参数
ptr:某一个成员的指针
type:结构体类型
member:成员变量名字
*/
#ifndef list_entry
#define list_entry(ptr, type, member) \
((type*)((char*)(ptr)-(char*)(&((type *)0)->member)))
#endif
/*list_entry
#define list_entry(ptr, type, member)
功能
获取type类型结构体的起始指针
参数
ptr:type类型的结构体中member成员的指针
type:结构体类型
member:结构体中成员
返回
结构体的起始指针
*/
#ifndef LIST_FOR_EACH
#define LIST_FOR_EACH(pos,head) \
for (pos=head;(pos=Common_List_GetNext(pos))!=head;)
#endif
#ifndef LIST_FOR_PREV
#define LIST_FOR_PREV(pos,head) \
for (pos=head;\
(pos=Common_List_GetPrev(pos))!=head;)
#endif
#ifndef LIST_FOR_EACH_SAVE
#define LIST_FOR_EACH_SAVE(pos,next,head) \
for (pos=next=Common_List_GetNext(head);\
(next!=head)&&(next=Common_List_GetNext(pos));\
pos=next)
#endif
#ifndef LIST_FOR_PREV_SAVE
#define LIST_FOR_PREV_SAVE(pos,next,head) \
for (pos=next=Common_List_GetPrev(head);\
(next!=head)&&(next=Common_List_GetPrev(pos));\
pos=next)
#endif
/******COMMON_LIST****/
void Common_List_Init(List);
void Common_List_Del(List);
List Common_List_Del_Save(List,List);
List Common_List_Del_RetPrev(List);
List Common_List_Del_RetNext(List);
List Common_List_GetPrev(List);
List Common_List_GetNext(List);
void Common_List_Add(List,List);
void Common_List_Add_Tail(List,List);
List Common_List_Merge(List,List);
void Common_List_Cut_Position(List,List);
void Common_List_Cut_Position_InHead(List,List,List);
int Common_List_IsEmpty(List);
typedef const struct F_LIST
{
void (*Init)(List);
void (*Del)(List);
List (*Del_Save)(List,List);
List (*Del_RetPrev)(List);
List (*Del_RetNext)(List);
List (*GetPrev)(List);
List (*GetNext)(List);
void (*Add)(List,List);
void (*Add_Tail)(List,List);
List (*Merge)(List,List);
void (*Cut_Position)(List,List);
void (*Cut_Position_InHead)(List,List,List);
int (*IsEmpty)(List);
}F_LIST;
extern F_LIST f_list;
#ifdef __cplusplus
}
#endif
#endif
Common_List.c
程序代码:
#include"Common_List.h"
#ifdef NDEBUG_LIST
#undef IS_EFAULT_RET
#define IS_EFAULT_RET(E,RET)
#undef ISERR
#define IS_ERR(E,RET)
#endif
F_LIST f_list=
{
Common_List_Init,
Common_List_Del,
Common_List_Del_Save,
Common_List_Del_RetPrev,
Common_List_Del_RetNext,
Common_List_GetPrev,
Common_List_GetNext,
Common_List_Add,
Common_List_Add_Tail,
Common_List_Merge,
Common_List_Cut_Position,
Common_List_Cut_Position_InHead,
Common_List_IsEmpty,
};
typedef enum E_LIST
{
PREV,
NEXT,
LIST_SIZE
}E_LIST;
static void _Insert(List,List,List,List);
static List _Del_Save(List,List);
static void _Insert(List p_front,List p_rear,List q_front,List q_rear)
{
(*p_front)[PREV]=q_rear;
(*q_rear)[NEXT]=p_front;
(*q_front)[PREV]=p_rear;
(*p_rear)[NEXT]=q_front;
}
static List _Del_Save(List list,List t_list)
{
_Insert((*list)[NEXT],list,list,(*list)[PREV]);
return t_list;
}
void Common_List_Init(List list)
{
(*list)[PREV]=list;
(*list)[NEXT]=list;
}
void Common_List_Del(List list)
{
IS_EFAULT_RET(list,;);
IS_EFAULT_RET((*list)[NEXT],;);
IS_EFAULT_RET((*list)[PREV],;);
_Insert((*list)[NEXT],list,list,(*list)[PREV]);
}
List Common_List_Del_Save(List list,List t_list)
{
IS_EFAULT_RET(list,NULL);
return _Del_Save(list,t_list);
}
List Common_List_Del_RetPrev(List list)
{
IS_EFAULT_RET(list,NULL);
IS_EFAULT_RET((*list)[PREV],NULL);
return _Del_Save(list,(*list)[PREV]);
}
List Common_List_Del_RetNext(List list)
{
IS_EFAULT_RET(list,NULL);
IS_EFAULT_RET((*list)[NEXT],NULL);
return _Del_Save(list,(*list)[NEXT]);
}
List Common_List_GetPrev(List list)
{
IS_ERR(*list==NULL,NULL);
return (*list)[PREV];
}
List Common_List_GetNext(List list)
{
IS_ERR(*list==NULL,NULL);
return (*list)[NEXT];
}
void Common_List_Add(List p_front,List q_front)
{
IS_EFAULT_RET(p_front,;);
IS_EFAULT_RET(q_front,;);
IS_EFAULT_RET((*p_front)[PREV],;);
IS_EFAULT_RET((*q_front)[PREV],;);
_Insert(p_front,(*p_front)[PREV],q_front,(*q_front)[PREV]);
}
void Common_List_Add_Tail(List p_front,List q_front)
{
IS_EFAULT_RET(p_front,;);
IS_EFAULT_RET(q_front,;);
IS_EFAULT_RET((*p_front)[PREV],;);
IS_EFAULT_RET((*q_front)[PREV],;);
_Insert((*p_front)[NEXT],p_front,q_front,(*q_front)[PREV]);
}
List Common_List_Merge(List list,List t_list)
{
List next_list=Common_List_GetNext(t_list);
Common_List_Add(list,t_list);
Common_List_Del(t_list);
return next_list;
}
void Common_List_Cut_Position(List list,List t_list)
{
IS_EFAULT_RET(list,;);
IS_EFAULT_RET(t_list,;);
IS_EFAULT_RET((*t_list)[PREV],;);
IS_EFAULT_RET((*list)[PREV],;);
_Insert(t_list,(*t_list)[PREV],list,(*list)[PREV]);
}
void Common_List_Cut_Position_InHead(List list,List t_list,List head)
{
Common_List_Cut_Position(list,t_list);
Common_List_Add(t_list,head);
}
int Common_List_IsEmpty(List list)
{
IS_EFAULT_RET(list,0);
return ((*list)[PREV]==list)&&((*list)[NEXT]==list);
}
main.c
程序代码:
#include"Common_List.h"
#include<string.h>
typedef struct Node
{
int num;
char name[10];
List_Node list_node;
}Node,*P_Node;
void Insert(List,int ,const char*);
void Print(List);
void Del(List);
int main(void )
{
List p_list=NULL;
LIST_HEAD(h_list_node);
LIST_HEAD(h_t_list_node);
puts("1号链表:\n");
Insert(&h_list_node,3,"abc");
Insert(&h_list_node,4,"def");
Insert(&h_list_node,5,"ghi");
Print(&h_list_node);
puts("2号链表:\n");
Insert(&h_t_list_node,-1,"xxx");
Insert(&h_t_list_node,-2,"yyy");
Insert(&h_t_list_node,-3,"zzz");
Print(&h_t_list_node);
puts("链表合并并去头节点:\n");
p_list=f_list.Merge(&h_list_node,&h_t_list_node);
Print(&h_list_node);
puts("链表分割并加入头节点:\n");
f_list.Cut_Position_InHead(&h_list_node,p_list,&h_t_list_node);
puts("分割后1号链表:\n");
Print(&h_list_node);
puts("\n分割后2号链表:\n");
Print(&h_t_list_node);
Del(&h_list_node);
Del(&h_t_list_node);
return 0;
}
void Insert(List h_list,int num,const char* name)
{
P_Node p_node=NULL;
f_node.Mal((void** )&p_node,0,sizeof (Node));
IS_ERR(p_node==NULL,;);
p_node->num=num;
strcpy(p_node->name,name);
f_list.Init(&p_node->list_node);
f_list.Add(&p_node->list_node,h_list);
}
void Print(List h_list)
{
List list=NULL;
List t_list=NULL;
LIST_FOR_EACH(list,h_list)
{
P_Node p_node=list_entry(list,Node,list_node);
printf("%d\n",p_node->num);
printf("%s\n\n",p_node->name);
}
}
void Del(List h_list)
{
List list=f_list.GetNext(h_list);
while (!f_list.IsEmpty(h_list))
{
P_Node p_node=list_entry(list,Node,list_node);
list=f_list.Del_RetNext(list);
free(p_node);
}
}
感觉还有很多功能需要完善,初步通过测试
~
[此贴子已经被作者于2018-1-4 21:39编辑过]