1,双向链表相当于两个单向循环链表。

2,双向链表的结点定义。

1 struct DULNode
2 {
3 int data;
4 struct DULNode * prior;
5 struct DULNode * next;
6 };
7
8 typedef struct DULNode * linklist;

3,单循环链表的操作都适用于双循环链表。
4,双循环链表的操作集合仍在头文件defs.h中。

5,InitList操作。双循环链表初始化操作示意图

 1 #include"defs.h"
2
3 void InitList(linklist *L) //改变头指针
4 {
5 *L = (linklist)malloc(sizeof(struct DULNode));
6 if (*L == NULL)
7 exit(0);
8 (*L)->next = *L;
9 (*L)->prior = *L;
10 }

6,ClearList操作.

 1 #include"defs.h"
2
3 void ClearList(linklist L)
4 {
5 linklist p = L->next; //p指向链表第一个元素
6
7 while (p != L)
8 {
9 p = p->next; //p指向第二个元素
10 free(p->prior);
11 }
12 (*L)->next = *L;
13 (*L)->prior = *L;
14 }

ClearList.c

7,DestroyList操作

1 #include"defs.h"
2
3 void DestroyList(linklist *L)
4 {
5 ClearList(*L);
6 free(*L);
7 *L = NULL;
8 }

DestroyList.c

8,ListEmpty操作

1 #include"defs.h"
2
3 int ListEmpty(linklist L)
4 {
5 if (L->next == L && L->prior == L) //两个条件均要有
6 return 0;
7 else
8 return 1;
9 }

ListEmpty.c

9,ListLength操作

 1 #include"defs.h"
2
3 int ListLength(linklist L)
4 {
5 linklist p = L->next;
6 int j = 0;
7
8 while (p != L)
9 {
10 ++j;
11 p = p->next;
12 }
13 return j;
14 }

ListLength.c

10,GetElem操作

 1 #include"defs.h"
2
3 int GetElem(linklist L, int i, int *e)
4 {
5 linklist p = L;
6 int j = 0;
7 if (i<1 || i>ListLength(L))
8 exit(0);
9
10 while (j<i) //找到第i个结点
11 {
12 ++j;
13 p = p->next;
14 }
15 *e = p->data;
16 }

GetElem.c

11,LocateElem操作, 没有找到返回-1

 1 #include"defs.h"
2
3 int LocateElem(linklist L, int e)
4 {
5 linklist p = L->next;
6 int j = 0;
7
8 while (p != L)
9 {
10 ++j;
11 if (p->data == e)
12 return j;
13 }
14 return -1;
15 }

LocateElem.c

12,PriorElem操作

 1 #include"defs.h"
2
3 int PriorElem(linklist L, int cur_e, int *pri_e)
4 {
5 linklist p = L->next->next; //p指向第二个元素
6
7 while (p != L)
8 {
9 if (p->data == cur_e)
10 {
11 *pri_e = p->prior->data;
12 return 0;
13 }
14 }
15 return 0;
16 }

PriorElem.c

13,NextElem操作

 1 #include"defs.h"
2
3 int NextElem(linklist L, int cur_e, int *nex_e)
4 {
5 linklist p = L->next->next;
6
7 while (p != L)
8 {
9 if (p->prior->data == cur_e)
10 {
11 *nex_e = p->data;
12 return 0;
13 }
14 }
15 return 0;
16 }

NextElem.c

14,ListInsert操作

 1 #include"defs.h"
2
3 int ListInsert(linklist L, int i, int e)
4 {
5 linklist p = L; //p指向头结点
6 linklist q, s;
7 int j = 0;
8 if (i<1 || i>ListLength(L)+1)
9 exit(0);
10
11 while (j < i-1) //找到第i-1个结点
12 {
13 ++j;
14 p = p->next;
15 }
16 q = p->next; //q指向第i个结点
17
18 s = (linklist)malloc(sizeof(struct DULNode));
19 s->data = e;
20
21 s->next = q; //先改变向右的指针
22 p->next = s;
23 s->prior = p; //改变向左的指针
24 q->prior = s;
25 return 0;
26 }

ListInsert.c

15,ListDelete操作

 1 #include"defs.h"
2
3 int ListDelete(linklist L, int i, int *e)
4 {
5 linklist p = L;
6 linklist q;
7 int j = 0;
8 if (i<1 || i>ListLength(L)) //位置是否合理
9 exit(0);
10
11 while (j < i-1) //找到第i-1个元素
12 {
13 ++j;
14 p = p->next;
15 }
16 q = p->next; //p指向第i个结点
17 *e = q->data;
18
19 p->next = q->next;
20 q->next->prior = p;
21 free(q);
22 return 0;
23 }

ListDelete.c

16,TravelList操作

 1 #include"defs.h"
2
3 void TravelList(linklist L)
4 {
5 linklist p = L->next;
6 int j = 0;
7 while (p != L)
8 {
9 ++j;
10 printf("第%d个元素值为:%d\n", j, p->data);
11 p = p->next;
12 }
13 }

TravelList.c

17,TravelListBack操作,逆序输出表中元素

 1 #include"defs.h"
2
3 void TravelListBack(linklist L)
4 {
5 linklist p = L->prior;
6 while (p != L)
7 {
8 printf("%d ", p->data);
9 p = p->prior;
10 }
11 printf("\n");
12 }

TravelListBack.c

最新文章

  1. VMWare虚拟机实例拷贝到另一台服务器后出现Error in the RPC receive loop: RpcIn: Unable to send.错误的解决
  2. 解读浮动闭合最佳方案:clearfix
  3. bigautocomplete实现联想输入,自动补全
  4. NSTimer定时器的使用
  5. Caesar&#39;s Legions(三维dp)
  6. opencv的高斯混合模型
  7. SHA1加密C#
  8. bzoj4702: 装箱游戏
  9. svn不提交user文件
  10. 设计模式--静态工厂设计模式在android中的使用
  11. 腾讯云安全:开发者必看|Android 8.0 新特性及开发指南
  12. Golang开发环境搭建(Notepad++、LiteIDE两种方式以及martini框架使用)
  13. spring 事务隔离级别配置
  14. python 格式化输出日志记录
  15. 记一次重大生产事故,在那 0.1s 我想辞职不干了!
  16. docker pull报错failed to register layer: Error processing tar file(exit status 1): open permission denied
  17. 学习了clipboard复制剪切插件的使用
  18. Raphael.js--基础1
  19. QueryParser 是对一段话进行分词的 用于收集客户端发来的
  20. Spring mvc Json 的正确返回姿势

热门文章

  1. 如何实现CentOS服务器的扩容??
  2. stat filename
  3. C#使用OracleParameter操作数据库
  4. merge join pg伪代码
  5. 通过SE14重建数据库表
  6. CentOS 7 下安装 mysql ,以及用到的命令
  7. 指针锁定 Pointer Lock API 用法
  8. 30分钟带你理解 Raft 算法
  9. 【Android初级】利用startActivityForResult返回数据到前一个Activity(附源码+解析)
  10. pycharm2021永久激活