双向链表是指含有往前和日后两个方向的链表,即每一个结点中除存放下一个节点指针外,还增长一个指向其前一个节点的指针。其头指针head是惟一肯定的。node
从双向链表中的任意一个结点开始,均可以很方便地访问它的前驱结点和后继结点,这种数据结构形式使得双向链表在查找时更加方便,特别是大量数据的遍历。因为双向链表具备对称性,能方便地完成各类插入、删除等操做,但须要注意先后方向的操做。编程
Huawei LiteOS系统中的双向链表模块为用户提供下面几个接口。数据结构
功能分类 | 接口名 | 描述 |
---|---|---|
初始化链表 | LOS_ListInit | 对链表进行初始化 |
增长节点 | LOSListAdd | 将新节点添加到链表中 |
在链表尾端插入节点 | LOS_ListTailInsert | 将节点插入到双向链表尾端 |
删除节点 | LOS_ListDelete | 将指定的节点从链表中删除 |
判断双向链表是否为空 | LOS_ListEmpty | 判断链表是否为空 |
删除节点并初始化链表 | LOS_ListDelInit | 将指定的节点从链表中删除使用该节点初始化链表 |
双向链表的典型开发流程:函数
使用双向链表,首先要申请内存,删除节点的时候要注意释放掉内存。测试
本实例实现以下功能:指针
代码实现以下:code
#include "los_list.h" #include<stdio.h> VOID list_test(void) { /*初始化,判断是否为空*/ printf("initial......\n"); LOS_DL_LIST* head; head = (LOS_DL_LIST*)malloc(sizeof(LOS_DL_LIST)); LOS_ListInit(head); if (!ListEmpty(head)) { printf("initial failed\n"); return; } /*增长一个节点,在尾端插入一个节点*/ printf("node add and tail add......\n"); LOS_DL_LIST* node1 = (LOS_DL_LIST*)malloc(sizeof(LOS_DL_LIST)); LOS_DL_LIST* node2 = (LOS_DL_LIST*)malloc(sizeof(LOS_DL_LIST)); LOS_DL_LIST* tail = (LOS_DL_LIST*)malloc(sizeof(LOS_DL_LIST)); LOS_ListAdd(node1,head); LOS_ListAdd(node2,node1); if((node1->pstPrev == head) || (node2->pstPrev == node1)){ printf("add node success\n"); } LOS_ListTailInsert(tail,head); if(tail->pstPrev == node2){ printf("add tail success\n"); } /*删除双向链表节点*/ printf("delete node......\n"); LOS_ListDelete(node1); free(node1); if(head->pstNext == node2){ printf("delete node success\n"); } }
编译运行获得的结果为:
blog