带头结点双链表【详细解析+完整代码】

双链表的认识

双链表的定义

单链表的结点中只有一个指向其后继的指针,使得单链表要访问某个结点的前驱结点时,只能从头开始遍历,访问后驱结点的复杂度为O(1),访问前驱结点的复杂度为O(n)。为了克服上述缺点,引入了双链表。

双链表的结点中有两个指针prior和next,分别指向前驱结点和后继结点。

结点结构:
在这里插入图片描述

带头结点链表结构:
在这里插入图片描述

typedef struct LNode {
	ElemType data;
	struct LNode* next,*prior;
}LNode, * LinkList;

带头结点优点

  • 保证了每个结点都有前驱结点,因此在链表上第一个结点的操作与其他结点操作一致,无需特殊处理
  • 无论链表是否为空,其头指针都是指向头结点的非空指针。可以对空表,非空表的情况进行统一处理。

双链表特点

  • 每个节点都由三部分组成:
    前一个节点的指针prior
    保存的元素data
    后一个节点的指针next

  • 双向链表的第一个节点的prior以及最后节点的next都是NULL


双的操作

初始化

双链表与单链表基本一致,为了操作方便加入头结点,那么初始化双链表其实就是定义一个头结点,然后将前后指针域置空。

bool InitList(LinkList& L) {
	L = (LNode*)malloc(sizeof(LNode));
	if (L == NULL)
	{
		printf("内存不足,分配空间失败");
		return false;
	}
	L->next = NULL;
	L->prior = NULL;
	return true;
}

建立双链表

头插法

思路和单链表头插建表大同小异。创建一个新结点,与头结点想连接。需要注意的是一开始时HeadNode->next = NULL,那么HeadNode->next就没有prior指针域,因此HeadNode->next->prior不用指向NewNode的地址。经过建立第一个结点后,HeadNode->next存在了,因此HeadNode->next->prior指向NewNode的地址。
在这里插入图片描述
如果没考虑到第一个结点的特殊情况,编译器会报错引发了异常: 写入访问权限冲突,
HeadNode->next 是 nullptr。

LinkList List_HeadInsert(LinkList& L) {
	InitList(L);
	int x;
	printf("输入链表元素(以0结束):\n");
	scanf_s("%d", &x);
	while (x != 0) {
		LNode* head = (LNode*)malloc(sizeof(LNode));
		head->data = x;
		head->next = L->next;
		if(L->next!=NULL)
		   L->next->prior = head;
		head->prior = L;
		L->next = head;
		scanf_s("%d", &x);
	}
	return L;
}

头插法的特点:单链表读入数据的顺序和输出的顺序是相反的,是逆序输出,时间复杂度是o(n)。
但是双链表的遍历输出可以正序也可以逆序,这就不管建表是头插还是尾插都能正序或者逆序输出。


尾插法

尾插法相对于头插法,更简单,因为不用考虑建立第一个结点的特殊情况。方法和单链表的尾插法一直,只是多了结点的prior指针的连接。

在这里插入图片描述

注意:TailNode = NewNode 不要写成 NewNode = TailNode 详情解释参考上一篇单链表的博客

LinkList List_TailInsert(LinkList& L) {
	InitList(L);
	LNode* NewNode, * TailNode = L;
	int x;
	printf("输入链表元素(以0结束):\n");
	scanf_s("%d", &x);
	while (x != 0) {
		NewNode = (LNode*)malloc(sizeof(LNode));
		NewNode->data = x;
		TailNode->next = NewNode;
		NewNode->prior = TailNode;
		TailNode = NewNode;
		scanf_s("%d", &x);
	}
	TailNode->next = NULL;
	return L;
}

遍历双链表

这是带头结点的链表,所以先找到第一个结点p,p为空则为空链表,然后遍历到尾结点就行。
遍历到尾结点后,又可以利用prior,向前遍历。

void PrintList(LinkList L) {
	LNode* p = L;
	if (!L->next)
		printf("空链表");
	else {
		printf("顺序打印:");
		while (p->next) {
			p = p->next;
			printf("%d ", p->data);
		}
		printf("\n逆序打印:");
		while (p->prior)
		{
			printf("%d ", p->data);
			p = p->prior;
		}
		printf("\n");
	}
}

双链表的长度

思路就是遍历链表,记录遍历次数就是长度。

int Length(LinkList L) {
	LNode* p = L->next;
	int len = 0;
	while (p) {
		p = p->next;
		len++;
	}
	return len;
}

查找操作

按值查找

从链表的第一个结点开始,依次比较表中各个结点的数据域的值,若某结点数据域的值等于x,则返回该结点的指针;若整个单链表中没有这样的结点,则返回空。

LNode* LocateElem(LinkList L, int x) {
	LNode* p = L->next;
	while (p && p->data != x) {
		p = p->next;
	}
	return p;
}

按位查找

从链表的头结点开始,顺着指针域逐个往下搜索,直到找到第 i 个结点为止,否则返回最后一个结点的指针域NULL。

LNode* GetElem(LinkList L, int i) {
	LNode* p = L;
	int j = 0;
	if (i<0 || i>Length(L))
		return NULL;
	else {
		while (j < i)
		{
			p = p->next;
			j++;
		}
	}
	return p;
}

当然你也可以从第一个结点开始,往下搜索。我为了方便后续操作找到头结点,所以是从头结点往下搜索。


插入操作

将值为x的新结点插入到单链表L的第i个位置上。从表头开始遍历,查找第 i-1个结点,即插入位置的前驱结点为p,然后利用前插操作,令新结点s的指针域指向p的后继结点,再令p的后继结点的prior指针域指向新结点s,s的prior指针指向i位置结点的前驱结点,最后p的next指针指向s结点完成插入。

bool Insert(LinkList& L, int i, int x) {
	if (i < 1 || i>Length(L))
		return false;
	LNode* p = GetElem(L, i - 1);
	LNode* s = (LNode*)malloc(sizeof(LNode));
	s->data = x;
	s->next = p->next;
	p->next->prior = s;
	s->prior = p;
	p->next = s;
	return true;
}

在这里插入图片描述

从插入操作就能体现出GetElem函数从头结点往下搜索的好处,它能找到第一个结点的前驱结点(头结点),因此就不用特殊处理第一个结点了。


删除操作

将单链表的第 i 个结点删除。先检查删除位置的合法性,然后从头开始遍历,找到i结点,可以直接访问前驱和后继结点。修改前驱结点next指向以及后继结点prior的指向最后再释放结点p的存储空间。
在这里插入图片描述

bool Delete(LinkList& L, int i)
{
	if (i<1 || i>Length(L)) {
		printf("输入不合法\n");
		return false;
	}
	LNode* p = GetElem(L, i);
	if (!p) 
		return false;
	p->prior->next = p->next;
	p->next->prior = p->prior;
	free (p);
	return true;
}

完整代码及实例

完整代码

#include<stdio.h>
#include<stdlib.h>

typedef int ElemType;

typedef struct LNode {
	ElemType data;
	struct LNode* next,*prior;
}LNode, * LinkList;

bool InitList(LinkList& L) {
	L = (LNode*)malloc(sizeof(LNode));
	if (L == NULL)
	{
		printf("内存不足,分配空间失败");
		return false;
	}
	L->next = NULL;
	L->prior = NULL;
	return true;
}

LinkList List_HeadInsert(LinkList& L) {
	InitList(L);
	LNode* NewNode, * HeadNode = L;
	int x;
	printf("输入链表元素(以0结束):\n");
	scanf_s("%d", &x);
	while (x != 0) {
		NewNode = (LNode*)malloc(sizeof(LNode));
		NewNode->data = x;
		NewNode->next = HeadNode->next;
		if(HeadNode->next!=NULL)
			HeadNode->next->prior = NewNode;
		NewNode->prior = HeadNode;
		HeadNode->next = NewNode;
		scanf_s("%d", &x);
	}
	return L;
}


LinkList List_TailInsert(LinkList& L) {
	InitList(L);
	LNode* NewNode, * TailNode = L;
	int x;
	printf("输入链表元素(以0结束):\n");
	scanf_s("%d", &x);
	while (x != 0) {
		NewNode = (LNode*)malloc(sizeof(LNode));
		NewNode->data = x;
		TailNode->next = NewNode;
		NewNode->prior = TailNode;
		TailNode = NewNode;
		scanf_s("%d", &x);
	}
	TailNode->next = NULL;
	return L;
}


//遍历操作
void PrintList(LinkList L) {
	LNode* p = L;
	if (!L->next)
		printf("空链表");
	else {
		printf("顺序打印:");
		while (p->next) {
			p = p->next;		//  头结点数据域为空,因此直接从头结点的下一结点开始遍历
			printf("%d ", p->data);
		}
		printf("\n逆序打印:");
		while (p->prior)
		{
			printf("%d ", p->data);
			p = p->prior;

		}
		printf("\n");
	}
	
}

int Length(LinkList L) {
	LNode* p = L->next;
	int len = 0;
	while (p) {
		p = p->next;
		len++;
	}
	return len;
}

LNode* LocateElem(LinkList L, int x) {
	LNode* p = L->next;
	while (p && p->data != x) {
		p = p->next;
	}
	return p;
}

LNode* GetElem(LinkList L, int i) {
	LNode* p = L;
	if (i<0 || i>Length(L))
		return NULL;
	else {
		for (int j = 0; j < i; j++) {
			p = p->next;
		}
	}
	return p;
}

//将x插入到单链表L的第i个位置上
bool Insert(LinkList& L, int i, int x) {
	if (i < 1 || i>Length(L))
		return false;
	LNode* p = GetElem(L, i - 1);
	LNode* s = (LNode*)malloc(sizeof(LNode));
	s->data = x;
	s->next = p->next;
	p->next->prior = s;
	s->prior = p;
	p->next = s;
	return true;
}

bool Delete(LinkList& L, int i)
{
	if (i<1 || i>Length(L)) {
		printf("输入不合法\n");
		return false;
	}
	LNode* p = GetElem(L, i);
	if (!p) 
		return false;
	p->prior->next = p->next;
	p->next->prior = p->prior;
	free (p);
	return true;
}


int main()
{
	LinkList L;
	InitList(L);
	printf("头插建立链表\n");
	List_HeadInsert(L);
	PrintList(L);
	printf("插入99到第2位\n");
	Insert(L, 2, 99);
	PrintList(L);
	printf("删除第1位\n");
	Delete(L, 1);
	PrintList(L);

	printf("\n\n尾插建立链表\n");
	List_TailInsert(L);
	PrintList(L);
	printf("插入99到第1位\n");
	Insert(L, 1, 99);
	PrintList(L);
	printf("删除第1位\n");
	Delete(L, 1);
	PrintList(L);
}


测试结果

头插建立链表
输入链表元素(以0结束):
1 2 3 4 5 6 7 0
顺序打印:7 6 5 4 3 2 1
逆序打印:1 2 3 4 5 6 7
插入99到第2位
顺序打印:7 99 6 5 4 3 2 1
逆序打印:1 2 3 4 5 6 99 7
删除第1位
顺序打印:99 6 5 4 3 2 1
逆序打印:1 2 3 4 5 6 99


尾插建立链表
输入链表元素(以0结束):
1 2 3 4 5 6 7 0
顺序打印:1 2 3 4 5 6 7
逆序打印:7 6 5 4 3 2 1
插入99到第1位
顺序打印:99 1 2 3 4 5 6 7
逆序打印:7 6 5 4 3 2 1 99
删除第1位
顺序打印:1 2 3 4 5 6 7
逆序打印:7 6 5 4 3 2 1

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值