周立功阐释高效的双向链表如何用
ead, dlist_node_t *p_node); // 添加结点至链表头部
20 int dlist_del (dlist_head_t *p_head, dlist_node_t *p_node); // 删除一个结点
2122 dlist_node_t *dlist_prev_get (dlist_head_t *p_head, dlist_node_t *p_pos); // 寻找某一结点的前一结点
23 dlist_node_t *dlist_next_get (dlist_head_t *p_head, dlist_node_t *p_pos); // 寻找某一结点的后一结点
24 dlist_node_t *dlist_tail_get (dlist_head_t *p_head); // 获取尾结点
25 dlist_node_t *dlist_begin_get (dlist_head_t *p_head); // 获取开始位置,第一个用户结点
26 dlist_node_t *dlist_end_get (dlist_head_t *p_head); // 获取结束位置,尾结点下一个结点的位置
27
28 int dlist_foreach (dlist_head_t *p_head,
29 dlist_node_process_t pfn_node_process,
30 void *p_arg);同样以int类型数据为例,来展示这些接口的使用方法。为了使用链表,首先应该定义一个结构体,将链表结点作为其一个成员,此外,再添加一些应用相关的数据,如定义如下包含链表结点和int型数据的结构体:
typedef struct _dlist_int{
dlist_node_t node; // 包含链表结点
int data; // int类型数据
}dlist_int_t;综合范例程序详见程序清单3.48。
程序清单3.48 综合范例程序
1 #include
2 #include "dlist.h"
3
4 typedef struct _dlist_int{
5 dlist_node_t node; // 包含链表结点
7 int data; // int类型数据
8 }dlist_int_t;
9
10 int list_node_process (void *p_arg, dlist_node_t *p_node)
11 {
12 printf("%d \r\n", ((dlist_int_t *)p_node) -> data);
13 return 0;
14 }15
16 int main(int argc, char *argv[])
17 {
18 dlist_head_t head; // 定义链表头结点
- 电源软启动的实用设计技巧(07-16)
- 周立功:动态分布内存——malloc()函数与calloc()函数(07-22)
- 周立功“程序设计与数据结构”:深度解剖动态分布内存的free()函数与realloc()函数(07-25)
- 周立功教你学程序设计技术:做好软件模块的分层设计,回调函数要这样写(07-30)
- 周立功教你学C语言编程:教你数组是如何保存指针的(07-31)
- 算法的泛化问题,这些坑你可能都经历过!|周立功教你学软件设计(08-01)
