微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 硬件工程师文库 > 周立功阐释高效的双向链表如何用

周立功阐释高效的双向链表如何用

时间:08-25 来源:周立功单片机 点击:

ead, dlist_node_t *p_node);                // 添加结点至链表头部

20  int dlist_del (dlist_head_t *p_head, dlist_node_t *p_node);                     // 删除一个结点

21

22  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;                               // 定义链表头结点

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top