目录
双向链表——顾名思义,两个方向
我们一般使用指针遍历来对我们所指定的位置或者值进行增删改查等操作,然而前面的单链表和循环链表我们是只有一个指针域的,并且这个指针指向的是下一个结点的地址,也就是说我们只有一个方向,那就是只能next,而不能return。双向链表的区别就在于此了,双向链表它有两个指针域,一个保存后面节点的地址,一个保存前面结点的地址,即我们不仅可以next,还可以return。
我们有特定的名称来说明双向链表的这两个指针域:
直接前驱:保存前面结点的地址
直接后继:保存后面结点的地址
结构体定义
typedef int ElemType;
typedef struct Dlist {
ElemType data; //数据域
struct Dlist* next; //指针域——直接后继
struct Dlist* prior; //指针域——直接前驱
}Dlist,*PDlist;
初始化
assert在debug 版本有用,在release 版本就被忽视掉了,所以后面最好再加个if
插入
插入的话就要考虑到插入位置的上一个结点的后继指针和插入位置下一个结点的前驱指针
还要判断插入位置结点的指针域是否为空,如果是空的话,那我们就不用管下一个结点的前驱指针,只需要修改插入位置结点的后驱指针指向我们插入结点的地址就可以了,但是插入位置的后驱指针是不为空的,也就是有指向的,那么我们就要修改其指向结点的前驱指针指向我们插入结点的地址。当然也是要对我们插入节点的前驱指向和后驱指向进行修改的。
所以完整的插入过程应该是:插入结点的后驱指向待插入位置结点的后驱指向的地址
插入结点的前驱指向待插入位置结点的地址
再判断待插入结点的后驱指针是否为空
是空的,只修改待插入结点的后驱指向
不是空的,修改待插入结点的后驱指向(修改为保存插入结点的地址)和待插入结点后驱指向结点的前驱指向(修改为保存插入结点的地址)
头插
//头插
bool Insert_head(PDlist plist, ElemType val) {
assert(plist != NULL);
if (plist == NULL) return false;
struct Dlist* pnewnode = (struct Dlist*)malloc(sizeof(struct Dlist));
assert(pnewnode != NULL);
if (pnewnode == NULL) return false;
pnewnode->data = val;
pnewnode->next = plist->next;
pnewnode->prior = plist;
if (!IsEmpty(plist)) {
plist->next->prior = pnewnode;
}
plist->next = pnewnode;
return true;
}
尾插
尾插也就是遍历到链表结尾,即待插入结点的后驱指针为空
//尾插
bool Insert_tail(PDlist plist, ElemType val) {
assert(plist != NULL);
if (plist == NULL) return false;
struct Dlist* pnewnode = (struct Dlist*)malloc(sizeof(struct Dlist));
assert(pnewnode != NULL);
if (pnewnode == NULL) return false;
pnewnode->data = val;
struct Dlist* p = plist;
for (p; p->next != NULL; p = p->next);
pnewnode->next = p->next;
pnewnode->prior = p;
p->next = pnewnode;
return true;
}
按位置插
如果插入下标为0或者是链表长度的话,那么我们直接调用头删或者尾删就可以了
如果不是头插或者尾插,我们就遍历到待插入位置的结点那里,按步骤走就行了
//按位置插
bool Insert_pos(PDlist plist, int pos, ElemType val) {
assert(plist != NULL);
if (plist == NULL) return false;
if (pos == 0) {
return Insert_head(plist, val);
}
if (pos == Get_length(plist)) {
return Insert_tail(plist, val);
}
struct Dlist* pnewnode = (struct Dlist*)malloc(sizeof(struct Dlist));
assert(pnewnode != NULL);
if (pnewnode == NULL) return false;
pnewnode->data = val;
struct Dlist* p = plist;
for (int i = 0; i < pos; i++) { //p走到了待插入位置的上一个结点
p = p->next;
}
pnewnode->next = p->next; //新结点指向p的下一个结点
pnewnode->prior = p;
p->next->prior = pnewnode;
p->next = pnewnode;
return true;
}
删除
同样是要记得修改相关结点的两个指针指向就可以了
头删
//头删
bool Del_head(PDlist plist) {
assert(plist != NULL);
if (plist == NULL) return false;
if (IsEmpty(plist)) return false;
struct Dlist* p = plist->next;
plist->next = p->next;
if (p->next != NULL) {
p->next->prior = plist;
}
free(p);
p = NULL;
return true;
}
尾删
//尾删
bool Del_tail(PDlist plist) {
assert(plist != NULL);
if (plist == NULL) return false;
if (IsEmpty(plist)) return false;
struct Dlist* p = plist->next;
for (; p->next != NULL; p = p->next);
struct Dlist* q = plist;
for (; q->next != p; q = q->next);
q->next = p->next;
free(p);
p = NULL;
return true;
}
按位置删
//按位置删
bool Del_pos(PDlist plist, int pos) {
assert(plist != NULL);
if (plist == NULL) return false;
if (IsEmpty(plist)) return false;
if (pos == 0) {
return Del_head(plist);
}
if (pos == Get_length(plist)) {
return Del_tail(plist);
}
struct Dlist* p = plist;
for (int i = 0; i < pos; i++) {
p = p->next;
}
struct Dlist* q = p->next;
p->next = q->next;
q->next->prior = p;
free(q);
q = NULL;
return true;
}
按值删
//按值删
bool Del_val(PDlist plist, int val) {
assert(plist != NULL);
if (plist == NULL) return false;
if (IsEmpty(plist)) return false;
struct Dlist* p = Search(plist, val);
if (p == NULL) {
return false;
}
struct Dlist* q = plist;
for (; q->next != p; q = q->next);
q->next = p->next;
if (p->next != NULL) {
p->next->prior = q;
}
free(p);
p = NULL;
return true;
}
查找——根据值查找位置
//查找
struct Dlist* Search(PDlist plist, ElemType val) {
assert(plist != NULL);
if (plist == NULL) return NULL;
for (struct Dlist *p = plist->next; p != NULL; p = p->next) {
if (p->data == val) {
return p;
}
}
return NULL;
}
判空
//判空
bool IsEmpty(PDlist plist) {
assert(plist != NULL);
if (plist == NULL) return false;
return plist->next == NULL;
}
链表长度——获取有效值个数
//获取有效值个数
int Get_length(PDlist plist) {
assert(plist != NULL);
if (plist == NULL) return -1;
int count = 0;
struct Dlist* p = plist->next;
for (; p != NULL; p = p->next) {
count++;
}
return count;
}
清空
//清空
void Clear(PDlist plist) {
assert(plist != NULL);
if (plist == NULL) return;
Destroy1(plist);
}
//销毁1 —— 一直尾删
void Destroy1(PDlist plist) {
assert(plist != NULL);
if (plist == NULL) return;
while (!IsEmpty(plist)) {
Del_head(plist);
}
}
//销毁2 —— 借用指针
void Destroy2(PDlist plist) {
assert(plist != NULL);
if (plist == NULL) return;
struct Dlist* p = plist->next;
struct Dlist* q;
plist->next = plist->prior = NULL;
while (p != NULL) {
q = p->next;
free(p);
p = q;
}
}
打印
//打印
void Show(PDlist plist) {
assert(plist != NULL);
if (plist == NULL) return;
for (struct Dlist* p = plist->next; p != NULL; p = p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
测试用例
//双向链表测试用例
int main() {
struct Dlist head;
Init_Dlist(&head);
for(int i=0; i<30; i++){
Insert_pos(&head, i, i+1);
}
Show(&head);
Insert_head(&head, 100);
Insert_tail(&head, 200);
Show(&head);
Del_head(&head);
Del_tail(&head);
Show(&head);
Del_pos(&head, 4);
Del_val(&head, 19);
Show(&head);
printf("length= %d\n", Get_length(&head));
/*Destroy1(&head);*/
Destroy2(&head);
return 0;
}