赞
踩
双向链表也叫双链表,它的每个数据节点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任何一个节点开始,都可以很方便的访问它的前驱结点和后继节点。别人常常来构造双向循环链表,今天我们特立独行一下,先来尝试构造不带头结点双向非循环链表。示例代码上传至 https://github.com/chenyufeng1991/DoubleLinkedList 。
(1)定义不带头结点的双向非循环链表的节点类型
- typedef struct NodeList{
-
- int element;
- struct NodeList *prior;
- struct NodeList *next;
- }Node;
(2)初始化双链表
- //1.初始化不带头结点的非循环双向链表
- void initList(Node *pNode){
-
- pNode = NULL;
- printf("%s函数执行,链表初始化完成\n",__FUNCTION__);
- }
(3)尾插法构造双向非循环链表:
- //创建非循环双向链表
- Node *createList(Node *pNode){
-
- Node *pInsert;
- Node *pMove;
- pInsert = (Node*)malloc(sizeof(Node));
- memset(pInsert, 0, sizeof(Node));
- pInsert->next = NULL;
- pInsert->prior = NULL;
-
- scanf("%d",&(pInsert->element));
- pMove = pNode;
-
- if (pInsert->element <= 0) {
-
- printf("%s函数执行,输入数据非法,建立链表停止\n",__FUNCTION__);
- return NULL;
- }
-
- while (pInsert->element > 0) {
- if (pNode == NULL) {
- pNode = pInsert;
- pMove = pNode;
- }else{
-
- pMove->next = pInsert;
- pInsert->prior = pMove;
- pMove = pMove->next;
- }
-
- pInsert = (No

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。