// 头文件
typedef struct list_s List, *Plist;
struct list_s{
Plist prev, next;
};
//初始化 h:头节点指针
do{ \
(h)->prev = NULL; \
(h)->next = NULL; \
}while(0)
//h:头节点指针 l:自定义结构体中List类型成员的指针
do{ \
(l)->prev = (h); \
(l)->next = (h)->next; \
(h)->next = (l); \
}while(0)
//
do{ \
Plist t = h; \
while(t->next) t = t->next; \
(l)->prev = (t); \
(l)->next = (t)->next; \
(t)->next = (l); \
}while(0)
//l:要读取的节点的list成员的指针
//struct_type:自定义结构体的类型名
//struct_field: List 类型成员的成员名称(变量名)
((struct_type*)((char*)(l)-offsetof(struct_type,struct_field)))
//要删除节点的list的指针
do{ \
if((l)->next == NULL) \
{ \
(l)->prev->next = (l)->next; \
(l)->prev = NULL; \
}else{ \
(l)->prev->next = (l)->next; \
(l)->next->prev = (l)->prev; \
} \
}while(0)
#include <stdio.h>
#include "GeneralList.h"
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct{
int a;
List list;
}TEST,*pTEST;
int main(int argc, char** argv) {
TEST test1 = {1234,{NULL, NULL}};
TEST test2 = {2234,{NULL, NULL}};
TEST test3 = {3234,{NULL, NULL}};
TEST test4 = {99099, {NULL, NULL}};
List head;
ListInit(&head);
ListTailInsert(&head,&test1.list);
ListTailInsert(&head,&test2.list);
ListTailInsert(&head,&test3.list);
ListTailInsert(&head, &test4.list);
List* temp = head.next;
for(temp = head.next; temp != NULL; temp = temp->next)
{
pTEST data_addr = ListGetData(temp,TEST,list);
printf("%d\n", data_addr->a);
}
ListDelet(&test3.list);
for(temp = head.next; temp != NULL; temp = temp->next)
{
pTEST data_addr = ListGetData(temp,TEST,list);
printf("%d\n", data_addr->a);
}
return 0;
}