代码语言:javascript复制
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef unsigned char D_U8;
typedef unsigned short D_U16;
typedef unsigned int D_U32;
typedef struct _D_List D_List;
struct _D_List{
D_List *prev;
D_List *next;
};
typedef struct
{
D_List list;
D_U8 tag3;
}D_Update_Descr;
typedef struct
{
D_List list;
D_U8 tag4;
}D_Device_Descr;
typedef struct
{
D_List list;
D_List *updates;
D_List *devices;
D_U8 tag2;
}D_Loader_Descr;
typedef struct
{
D_List list;
D_U8 tag1;
D_List *loader_desc;
}D_Loader_List;
#define D_MACRO_BEGIN do {
#define D_MACRO_END } while(0)
#define D_LIST_ALLOC(_d)
D_MACRO_BEGIN
_d = malloc(sizeof(*(_d)));
if(_d)
{
memset(_d, 0, sizeof(*(_d)));
((D_List*)(_d))->prev = (D_List*)(_d);
((D_List*)(_d))->next = (D_List*)(_d);
}
else
{
printf("malloc error!n");
}
D_MACRO_END
#define D_LIST_ADD(_l, _d)
D_MACRO_BEGIN
if(_l)
{
D_List *t1, *t2;
t1 = ((D_List*)(_l))->prev;
t2 = ((D_List*)(_d))->prev;
t1->next = (D_List*)(_d);
t2->next = (D_List*)(_l);
((D_List*)(_l))->prev = t2;
((D_List*)(_d))->prev = t1;
}
else
{
(_l) = (D_List*)(_d);
}
D_MACRO_END
int main(void)
{
D_List *list = NULL;
D_Loader_List *temp = NULL;
D_Loader_Descr *temp2 = NULL;
D_Update_Descr *temp3 = NULL;
D_Device_Descr *temp4 = NULL;
D_Loader_List *loader_list = NULL;
D_Loader_Descr *loader_descs = NULL;
D_Update_Descr *update_descs = NULL;
D_Device_Descr *device_descs = NULL;
D_LIST_ALLOC(loader_list);
D_LIST_ALLOC(loader_descs);
D_LIST_ALLOC(update_descs);
D_LIST_ALLOC(device_descs);
D_LIST_ADD(list, loader_list);
D_LIST_ADD(loader_list->loader_desc, loader_descs);
D_LIST_ADD(loader_descs->updates, update_descs);
D_LIST_ADD(loader_descs->devices, device_descs);
loader_list->tag1 = 10;
loader_descs->tag2 = 20;
update_descs->tag3 = 30;
device_descs->tag4 = 40;
temp = (D_Loader_List*)list;
printf("tag1 = %dn",temp->tag1);
printf("temp = 0x%xn", temp);
printf("list = 0x%xn", list);
printf("list->prev = 0x%xn", list->prev);
printf("list->next = 0x%xn", list->next);
printf("temp->loader_descs = 0x%xn",temp->loader_desc);
printf("loader_descs = 0x%xn",loader_descs);
temp2 = (D_Loader_Descr*)(temp->loader_desc);
temp3 = (D_Update_Descr*)(temp2->updates);
printf("temp3 = 0x%xn",temp3);
printf("temp3->list = 0x%xn",temp3->list);
printf("temp3->list->prev = 0x%xn", temp3->list.prev);
printf("temp3->list->next = 0x%xn", temp3->list.next);
temp4 = (D_Device_Descr*)(temp2->devices);
printf("tag2 = %dn", temp2->tag2);
printf("tag3 = %dn", temp3->tag3);
printf("tag4 = %dn", temp4->tag4);
return 0;
}
运行结果图:
链表结构图: