上一篇[数据结构](4)单链表讲到了单链表,也更新了一道关于链表的经典题目。单链表由于结构上的缺陷也比较好出题目,在笔试面试中出现很多。实际用链表单独存储数据,都是带头双向循环链表。
与单链表的区别
逻辑上基本没有区别,都是通过指针连接完成线性表的内容。
结构上,单链表每个节点只有一个指针next
,指向后继节点。单链表的遍历只能从前到后。
而双链表每个节点上都有两个指针,分别指向后继节点和前驱节点。所以,从双向链表中的任意一个节点开始,都可以很方便地访问它的前驱节点和后继节点。
双向链表的实现
头文件List.h
结构的定义多了个prev
,用来指向前驱节点。
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int LTDataType;
typedef struct ListNode
{
LTDataType data;
struct ListNode* next;
struct ListNode* prev;
}LTNode;
void ListPrint(LTNode* phead);
LTNode* ListInit();
LTNode* BuyLTNode(LTDataType x);
void ListPushBack(LTNode* phead, LTDataType x);
void ListPopBack(LTNode* phead);
void ListPushFront(LTNode* phead, LTDataType x);
void ListPopFront(LTNode* phead);
LTNode* ListFind(LTNode* phead, LTDataType x);
void ListInsert(LTNode* pos, LTDataType x);
void ListErase(LTNode* pos);
void ListDestory(LTNode* phead);
函数实现List.c
申请节点
LTNode* BuyLTNode(LTDataType x)
{
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
初始化
这次直接写带虚拟头节点的,上次写单链表已经说了,带头的要初始化,因为就算一个数据也没有,也要有一个虚拟头节点。
LTNode* ListInit()
{
LTNode* phead = BuyLTNode(0);
phead->next = phead;
phead->prev = phead;
return phead;
}
插入
怎么插?多画画图就明白了
void ListInsert(LTNode* pos, LTDataType x)
{
assert(pos);
LTNode* newnode = BuyLTNode(x);
newnode->next = pos;
newnode->prev = pos->prev;
pos->prev->next = newnode;
pos->prev = newnode;
}
是不是比单链表还简单呢?没什么要判断的条件。
说起判断条件,这么写在特殊条件下可不可以呢?比如只有一个虚拟头节点的情况。话不多说,看图!
完全没有问题,虽然逻辑是插在前面,但是对于循环链表来说,插在最前面和插在最后面没有区别。
删除
这个也很好写,用prev
和next
分别记录前后节点,free(pos)
之后再把前后两个连起来就行了。
void ListErase(LTNode* pos)
{
assert(pos);
LTNode* prev = pos->prev;
LTNode* next = pos->next;
free(pos);
pos = NULL;
prev->next = next;
next->prev = prev;
}
别把虚拟头节点删了哦~
尾插 尾删 头插 头删
写完上面的,这几个就很简单了。
void ListPushBack(LTNode* phead, LTDataType x)
{
assert(phead);
ListInsert(phead, x);
}
void ListPopBack(LTNode* phead)
{
assert(phead);
// 链表为空
assert(phead->next != phead);
ListErase(phead->prev);
}
void ListPushFront(LTNode* phead, LTDataType x)
{
assert(phead);
ListInsert(phead->next, x);
}
void ListPopFront(LTNode* phead)
{
assert(phead);
// 链表为空
assert(phead->next != phead);
ListErase(phead->next);
}
删除都要检查链表是否为空。虚拟头节点自己指向自己就说明为空。
如果你不想用ListInsert函数,只是单独写一个尾插也可以:
void ListPushBack(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* tail = phead->prev;
LTNode* newnode = BuyLTNode(x);
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
}
找尾不需要像单链表那样从头遍历到尾了。
打印
void ListPrint(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
查找
LTNode* ListFind(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
销毁
void ListDestory(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
}
完整代码
#include"List.h"
LTNode* BuyLTNode(LTDataType x)
{
LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
void ListPrint(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
//void ListInit(LTNode** pphead)
//{
// assert(pphead);
// *pphead = BuyLTNode(0);
// (*pphead)->next = *pphead;
// (*pphead)->prev = *pphead;
//}
LTNode* ListInit()
{
LTNode* phead = BuyLTNode(0);
phead->next = phead;
phead->prev = phead;
return phead;
}
void ListPushBack(LTNode* phead, LTDataType x)
{
assert(phead);
//LTNode* tail = phead->prev;
//LTNode* newnode = BuyLTNode(x);
//tail->next = newnode;
//newnode->prev = tail;
//newnode->next = phead;
//phead->prev = newnode;
ListInsert(phead, x);
}
void ListPopBack(LTNode* phead)
{
assert(phead);
// 链表为空
assert(phead->next != phead);
/* LTNode* tail = phead->prev;
LTNode* tailPrev = tail->prev;
free(tail);
tail = NULL;
tailPrev->next = phead;
phead->prev = tailPrev;*/
ListErase(phead->prev);
}
void ListPopFront(LTNode* phead)
{
assert(phead);
// 链表为空
assert(phead->next != phead);
ListErase(phead->next);
}
void ListPushFront(LTNode* phead, LTDataType x)
{
assert(phead);
ListInsert(phead->next, x);
}
LTNode* ListFind(LTNode* phead, LTDataType x)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void ListInsert(LTNode* pos, LTDataType x)
{
assert(pos);
LTNode* newnode = BuyLTNode(x);
newnode->next = pos;
newnode->prev = pos->prev;
pos->prev->next = newnode;
pos->prev = newnode;
}
void ListErase(LTNode* pos)
{
assert(pos);
LTNode* prev = pos->prev;
LTNode* next = pos->next;
free(pos);
pos = NULL;
prev->next = next;
next->prev = prev;
}
void ListDestory(LTNode* phead)
{
assert(phead);
LTNode* cur = phead->next;
while (cur != phead)
{
LTNode* next = cur->next;
free(cur);
cur = next;
}
free(phead);
}
测试test.c
#include "List.h"
void menu()
{
printf("***********************\n");
printf("**** 1.头插 2.头删 ****\n");
printf("**** 3.尾插 4.尾删 ****\n");
printf("**** 5.插入 6.删除 ****\n");
printf("**** 7.打印 8.查找 ****\n");
printf("**** 0.退出 ****\n");
printf("***********************\n");
}
int main()
{
LTNode* pList = ListInit();
int option = 0;
LTDataType x;
LTDataType pos;
do
{
menu();
printf("请选择:");
scanf("%d", &option);
switch (option)
{
case 1:
printf("输入要插入的值:");
scanf("%d", &x);
ListPushFront(pList, x);
printf("插入成功\n");
break;
case 2:
ListPopFront(pList);
printf("删除成功\n");
break;
case 3:
printf("输入要插入的值:");
scanf("%d", &x);
ListPushBack(pList, x);
printf("插入成功\n");
break;
case 4:
ListPopBack(pList);
printf("删除成功\n");
break;
case 5:
printf("输入要插入的位置后一个结点的值:");
scanf("%d", &pos);
printf("输入要插入的值:");
scanf("%d", &x);
ListInsert(ListFind(pList, pos), x);
printf("插入成功\n");
break;
case 6:
printf("输入要删除的位置:");
scanf("%d", &pos);
ListErase(ListFind(pList, pos));
printf("删除成功\n");
break;
case 7:
ListPrint(pList);
break;
case 8:
printf("输入要查找的值:");
scanf("%d", &x);
LTNode* p = ListFind(pList, x);
if (p == NULL)
{
printf("未找到\n");
}
else
{
printf("地址为:%p\n", p);
}
break;
}
} while (option);
printf("退出程序\n");
ListDestory(pList);
return 0;
}
总结
通过以上关于双链表的实现,我们发现,对比单链表,双链表的结构复杂了一点,实现反而简单了。
双链表的更多优势我们可以再后续使用中慢慢体会。