链表(二) 双链表操作详解


什么是链表及单链表的实现请跳转: 链表(一) 单链表操作详解
在这里插入图片描述

四、双向带头循环链表的实现

在这里插入图片描述

在这里插入图片描述
代码结构设计:

  • List.h: 存放链表结构及需要用到的头文件,函数声明等
  • List.c: 各种操作函数的具体实现

List.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

// 带头+双向+循环链表增删查改实现

typedef int LTDataType;

typedef struct ListNode
{
	LTDataType data;
	struct ListNode* next;
	struct ListNode* prev;
}ListNode;

// 创建返回链表的头结点.
ListNode* ListCreate();
// 双向链表打印
void ListPrint(ListNode* pHead);
// 双向链表尾插
void ListPushBack(ListNode* pHead, LTDataType x);
// 双向链表尾删
void ListPopBack(ListNode* pHead);
// 双向链表头插
void ListPushFront(ListNode* pHead, LTDataType x);
// 双向链表头删
void ListPopFront(ListNode* pHead);
// 双向链表查找
ListNode* ListFind(ListNode* pHead, LTDataType x);
// 双向链表在pos的前面进行插入
void ListInsert(ListNode* pos, LTDataType x);
// 双向链表删除pos位置的节点
void ListErase(ListNode* pos);

List.c

#include "List.h"

//创建节点,此函数用于方便创建节点
ListNode* ListBuy(int x)
{
	ListNode* node = (ListNode*)malloc(sizeof(ListNode));
	if (node == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	node->data = x;
	node->next = NULL;
	node->prev = NULL;

	return node;
}

创建返回链表的头结点

ListNode* ListCreate()
{
	ListNode* head = ListBuy(0);

	head->next = head;
	head->prev = head;

	return head;
}

在这里插入图片描述

双向链表打印

void ListPrint(ListNode* pHead)
{
	assert(pHead);
	ListNode* cur = pHead->next;
	printf("head <=> ");

	while (cur != pHead)
	{
		printf("%d <=> ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

双向链表尾插

void ListPushBack(ListNode* pHead, LTDataType x)
{
	assert(pHead);

	ListNode* newNode = ListBuy(x);
	ListNode* tail = pHead->prev;

	tail->next = newNode;
	newNode->prev = tail;

	pHead->prev = newNode;
	newNode->next = pHead;
}

在这里插入图片描述

双向链表尾删

void ListPopBack(ListNode* pHead)
{
	assert(pHead);
	assert(pHead->next!=pHead);

	ListNode* tail = pHead->prev;
	ListNode* tailPrev = tail->prev;
	free(tail);

	tailPrev->next = pHead;
	pHead->prev = tailPrev;
}

在这里插入图片描述

双向链表头插

void ListPushFront(ListNode* pHead, LTDataType x)
{
	assert(pHead);

	ListNode* first = pHead->next;
	ListNode* newNode = ListBuy(x);

	pHead->next = newNode;
	newNode->prev = pHead;

	newNode->next = first;
	first->prev = newNode;
}

在这里插入图片描述

双向链表头删

void ListPopFront(ListNode* pHead)
{
	assert(pHead);
	assert(pHead->next!=pHead);

	ListNode* first = pHead->next;
	ListNode* second = pHead->next->next;
	free(first);

	pHead->next = second;
	second->prev = pHead;
}

在这里插入图片描述

双向链表查找

ListNode* ListFind(ListNode* pHead, LTDataType x)
{
	assert(pHead);

	ListNode* cur = pHead->next;

	while (cur != pHead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

双向链表在pos的前面进行插入

void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos);

	ListNode* posPrev = pos->prev;
	ListNode* newNode = ListBuy(x);

	posPrev->next = newNode;
	newNode->prev = posPrev;

	newNode->next = pos;
	pos->prev = newNode;
}

在这里插入图片描述

双向链表删除pos位置的节点

void ListErase(ListNode* pos)
{
	assert(pos);

	ListNode* posPrev = pos->prev;
	ListNode* posNext = pos->next;
	free(pos);

	posPrev->next = posNext;
	posNext->prev = posPrev;
}

在这里插入图片描述

五、单链表与双链表比较

单链表双链表
存储空间物理上一定连续逻辑上连续,物理上不一定
随机访问支持 :O(1)不支持 :O(n)
任意位置插入删除元素可能需要搬移元素,效率低:O(N)只需修改指针指向
插入动态顺序表,空间不够时需要扩容没有容量的概念
应用场景元素高效存储+频繁访问任意位置插入和删除频繁
缓存利用率
评论 24
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zcx-yyds

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值