1,双向链表相当于两个单向循环链表
2,双向链表的结点定义
struct DULNode
{
int data;
struct DULNode * prior;
struct DULNode * next;
};
typedef struct DULNode * linklist;
3,单循环链表的操作都适用于双循环链表
4,双循环链表的操作集合仍在头文件defs.h中
5,InitList操作,双循环链表初始化操作示意图
#include"defs.h"
void InitList(linklist *L) //改变头指针
{
*L = (linklist)malloc(sizeof(struct DULNode));
if (*L == NULL)
exit(0);
(*L)->next = *L;
(*L)->prior = *L;
}
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 (jnext;
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
希望对你有帮助!
自学C/C++编程难度很大,不妨和一些志同道合的小伙伴一起学习成长!
C语言C++编程学习交流圈子,【点击进入】微信公众号:C语言编程学习基地
有一些源码和资料分享,欢迎转行也学习编程的伙伴,和大家一起交流成长会比自己琢磨更快哦!