1. 定义链表数据结构
typedef struct tagListNode {
struct tagListNode *next;
struct tagListNode *prev;
} ListNode;
2. 定义链表结构
typedef struct {
ListNode node;
unsigned int count;
} List
3. 定义常用链表操作
// 链表初始化
#LIST_INIT(list) do { \
((List *)(list))->node.next = ((List *)(list))->node.prev = (&((List *)(list))->node);\
(List *)list->count++; \
} while(0)
// 获取列表的跟节点
#define LIST_ROOT(list) (&((List *)list)->node)
// 判断列表是否为空
#define LIST_IS_EMPTY(list) (((List *)list)->count == 0)
4. 链表插入操作原理:
1) 设置插入节点的前驱和后继
2)设置指向节点的前驱和后继
node->prev = where->prev;
node->next = where;
where->prev = node;
node->prev->next = node;