【数据结构学习笔记】二、线性表之链表篇(3)(双向链表)

链表根据带头不带头、单向或双向、循环非循环三个方面可组成成八种类型。这八种类型中仅有两种是我们常用的。
第一种是单向不带头非循环链表,也就是单链表。单链表因为结构简单,但是实际操作比较复杂,所以在面试当中经常被用到,(因为可以考察出同学们的水平和能力呀~)。如果我们经常在leetcode或者牛客网等刷题网站上面玩耍,就可以遇到大量与单链表相关的题目。如果想复习或进一步学习单链表的内容,可以参考文章【数据结构学习笔记】二、线性表—链表篇(2)
第二种是双向带头循环链表,也被叫做双链表。这种链表虽然结构比较复杂,(又是双向,又是带头,又是循环,看起来结构好像很复杂的样子)但是实际操作却非常简单。因此在我们的实际工作或项目中常使用的链表是这种。
在这里插入图片描述

头文件 DList.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//双向带头循环链表
typedef int LTDatatype;
typedef struct DList
{
	LTDatatype data;
	struct DList* next;
	struct DList* prev;
}DList,DNode;

//创建一个节点
DNode* CreateDNode(LTDatatype x);
//链表初始化
DList* DListInit();
//销毁
void DListDestory(DList** pphead);
//打印
void DListPrint(DList* phead);
//尾插
void DListPushBack(DList* phead,LTDatatype x);
//头插
void DListPushFront(DList* phead, LTDatatype x);
//尾删
void DListPopBack(DList* phead);
//头删
void DListPopFront(DList* phead);
//查找
DNode* DListFind(DList* phead, LTDatatype x);
//在任意位置插入
void DListInsert(DNode* pos, LTDatatype x);
//在任意位置删除
void DListErase(DNode* pos);
//判空 1-为空 0-非空
int DListEmpty(DList* phead);
//求长度
int DListLength(DList* phead);

源文件 DList.c

#include"DList.h"
//创建一个节点
DNode* CreateDNode(LTDatatype x){
	DNode* node = (DNode*)malloc(sizeof(DNode));
	if (node == NULL)
	{
		printf("malloc fail!\n");
		return NULL;
	}
	node->data = x;
	node->next = NULL;
	node->prev = NULL;
	return node;
}
//链表初始化
DList* DListInit(){
	DNode* phead = CreateDNode(0);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}
//销毁
void DListDestory(DList** pphead){
	assert(pphead);
	DNode* cur = (*pphead)->next;
	DNode* next = NULL;
	while (cur != *pphead)
	{
		next = cur->next;
		free(cur);
		cur = next;
	}
	*pphead = NULL;
}
//打印
void DListPrint(DList* phead){
	if (phead == NULL)
	{
		printf("The DList is empty!\n");
		return;
	}
	DNode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n------------------------\n");
}
//尾插
void DListPushBack(DList* phead, LTDatatype x){
	1.找到尾巴
	//DNode* tail = phead->prev;
	2.申请一个节点
	//DNode* newnode = CreateDNode(x);
	3.进行尾插操作
	//tail->next = newnode;
	//newnode->prev = tail;
	//newnode->next = phead;
	//phead->prev = newnode;

	DListInsert(phead, x);
}
//头插
void DListPushFront(DList* phead, LTDatatype x){
	//assert(phead);
	//DNode* newnode = CreateDNode(x);
	//DNode* pnext = phead->next;
	//newnode->next = pnext;
	//newnode->prev = phead;
	//pnext->prev = newnode;
	//phead->next = newnode;
	DListInsert(phead->next, x);
}
//尾删
void DListPopBack(DList* phead){
	链表为空,或链表仅有头节点
	//if (phead == NULL || phead->next == phead)
	//{
	//	printf("No element to pop!\n");
	//	return;
	//}
	//DNode* tail = phead->prev;
	//DNode* prev_tail = tail -> prev;
	尾删操作
	//prev_tail->next = phead;
	//phead->prev = prev_tail;
	//free(tail);
	//tail = NULL;

	DListErase(phead->prev);//接口复用
}
//头删
void DListPopFront(DList* phead){
	//assert(phead && (phead->next != phead));
	//DNode* first = phead->next;
	//DNode* second = first->next;
	头插操作
	//second->prev = phead;
	//phead->next = second;
	//free(first);
	//first = NULL;

	DListErase(phead->next);//接口复用
}
//查找
DNode* DListFind(DList* phead, LTDatatype x){
	assert(phead);
	DNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
			return cur;
		cur = cur->next;
	}
	return NULL;//查找不到就返回NULL
}
//在任意位置插入--前面插入
void DListInsert(DNode* pos, LTDatatype x){
	assert(pos);
	DNode* prev = pos->prev;
	DNode* newnode = CreateDNode(x);
	newnode->next = pos;
	newnode->prev = prev;
	pos->prev = newnode;
	prev->next = newnode;
}
//在任意位置删除
void DListErase(DNode* pos){
	assert(pos && pos->next != pos);
	DNode* prev = pos->prev;
	DNode* next = pos->next;

	prev->next = next;
	next->prev = prev;
	free(pos);
	pos = NULL;
}
//判空 1-为空 0-非空
int DListEmpty(DList* phead){
	return phead->next == phead ? 1 : 0;
}
//求长度
int DListLength(DList* phead){
	int length = 0;
	DNode* cur = phead->next;
	while (cur != phead)
	{
		cur = cur->next;
		length++;
	}
}


测试模块 test.c

#define _CRT_SECURE_NO_WARNINGS 1 
#include"DList.h"
void TestDList1(){
	DList* pList = DListInit();
	DListPrint(pList);

	DListPushBack(pList, 0);
	DListPushBack(pList, 1);
	DListPushBack(pList, 2);
	DListPushBack(pList, 3);
	DListPushBack(pList, 4);
	DListPushBack(pList, 5);
	DListPrint(pList);
	DListPushFront(pList, 1);
	DListPushFront(pList, 2);
	DListPushFront(pList, 3);
	DListPushFront(pList, 4);
	DListPushFront(pList, 5);
	DListPrint(pList);

	DListPopBack(pList);
	DListPopBack(pList);
	DListPopBack(pList);
	DListPopBack(pList);
	DListPopBack(pList);
	DListPrint(pList);

	int ret = DListEmpty(pList);
	printf("ret == %d\n", ret);
	int length = DListLength(pList);
	printf("length == %d\n", length);

	DListPopFront(pList);
	DListPopFront(pList);
	DListPopFront(pList);
	DListPrint(pList);

	DNode* pos = DListFind(pList, 1);
	if (pos)
	{
		DListInsert(pos, 40);
	}
	DListPrint(pList);

	DListDestory(&pList);
	DListPrint(pList);

}
int main()
{
	TestDList1();
	return 0;
}

运行截图

在这里插入图片描述

  • 12
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大家好我叫张同学

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

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

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

打赏作者

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

抵扣说明:

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

余额充值