双向链表基本操作及顺序和链表总结

目录

基本函数实现

链表声明

总的函数实现声明

创建一个节点

初始化链表

打印

尾插

尾删

头插

头删

查找

pos前插入

删除pos位置

销毁链表

顺序表和链表总结


基本函数实现

链表声明

cpp 复制代码
typedef int DLTDataType;

typedef struct DListNode
{
	struct DListNode* next;
	struct DListNode* prev;
	DLTDataType val;
}DLTNode;

总的函数实现声明

cpp 复制代码
//申请新的节点
DLTNode* CreateLTNode(DLTDataType x);
//初始化
DLTNode* DLTInit();
//打印
void DLTPrint(DLTNode* phead);
//头插头删尾插尾删
void DLTPushBack(DLTNode* phead, DLTDataType x);
void DLTPopBack(DLTNode* phead);
void DLTPushFront(DLTNode* phead, DLTDataType x);
void DLTPopFront(DLTNode* phead);
//找x的位置
DLTNode* DLTFind(DLTNode* phead, DLTDataType x);
//在pos前面插入
void DLTInsert(DLTNode* pos, DLTDataType x);
//删除pos位置
void DLTErase(DLTNode* pos);
//销毁链表
void DLTDestroy(DLTNode* phead);

创建一个节点

cpp 复制代码
DLTNode* CreateLTNode(DLTDataType x)
{
	DLTNode* newnode = (DLTNode*)malloc(sizeof(DLTNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	newnode->val = x;
	newnode->prev = NULL;
	newnode->next = NULL;
	return newnode;
}

初始化链表

cpp 复制代码
DLTNode* DLTInit()
{
	DLTNode* phead = CreateLTNode(-1);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}

打印

cpp 复制代码
void DLTPrint(DLTNode* phead)
{
	assert(phead);
	printf("哨兵卫<=>");

	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d<=>", cur->val);
		cur = cur->next;
	}
	printf("\n");
}

尾插

cpp 复制代码
//第一种尾插方式
//void DLTPushBack(DLTNode* phead, DLTDataType x)
//{
//	assert(phead);
//	DLTNode* tail = phead->prev;
//	DLTNode* newnode = CreateLTNode(x);
//
//	tail->next = newnode;
//	newnode->prev = tail;
//	newnode->next = phead;
//	phead->prev = newnode;
//}

//第二种尾插方式
void DLTPushBack(DLTNode* phead, DLTDataType x)
{
	assert(phead);
	DLTInsert(phead, x);
}

尾删

cpp 复制代码
//第一种尾删
//void DLTPopBack(DLTNode* phead)
//{
//	assert(phead);
//	assert(phead->next != phead);
//
//	DLTNode* tail = phead->prev;
//	DLTNode* tailPrev = tail->prev;
//	free(tail);
//	tailPrev->next = phead;
//	phead->prev = tailPrev;
//}

//第二种尾删
void DLTPopBack(DLTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
	
	DLTErase(phead->prev);
}

头插

cpp 复制代码
//第一种头插方式
//void DLTPushFront(DLTNode* phead, DLTDataType x)
//{
//	assert(phead);
//	DLTNode* newnode = CreateLTNode(x);
//
//	newnode->next = phead->next;
//	phead->next->prev = newnode;
//	phead->next = newnode;
//	newnode->prev = phead;
//}

//第二种头插方式
//void DLTPushFront(DLTNode* phead, DLTDataType x)
//{
//	assert(phead);
//	DLTNode* newnode = CreateLTNode(x);
//	DLTNode* first = phead->next;
//
//	phead->next = newnode;
//	newnode->prev = phead;
//	newnode->next = first;
//	first->prev = newnode;
//}

//第三种头插方式
void DLTPushFront(DLTNode* phead, DLTDataType x)
{
	assert(phead);
	
	DLTInsert(phead->next, x);
}

头删

cpp 复制代码
第一种头删
//void DLTPopFront(DLTNode* phead)
//{
//	assert(phead);
//	assert(phead->next != phead);
//
//	DLTNode* first = phead->next;
//	DLTNode* second = first->next;
//	phead->next = second;
//	second->prev = phead;
//	free(first);
//	first = NULL;
//}

//第二种头删
void DLTPopFront(DLTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);

	DLTErase(phead->next);
}

查找

cpp 复制代码
DLTNode* DLTFind(DLTNode* phead, DLTDataType x)
{
	assert(phead);

	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->val == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

pos前插入

cpp 复制代码
//在pos前面插入
void DLTInsert(DLTNode* pos, DLTDataType x)
{
	assert(pos);

	DLTNode* posPrev = pos->prev;
	DLTNode* newnode = CreateLTNode(x);

	posPrev->next = newnode;
	newnode->prev = posPrev;
	newnode->next = pos;
	pos->prev = newnode;
}

删除pos位置

cpp 复制代码
//删除pos位置
void DLTErase(DLTNode* pos)
{
	assert(pos);

	DLTNode* posNext = pos->next;
	DLTNode* posPrev = pos->prev;

	posPrev->next = posNext;
	posNext->prev = posPrev;
	free(pos);
	pos = NULL;
}

销毁链表

cpp 复制代码
void DLTDestroy(DLTNode* phead)
{
	assert(phead);

	DLTNode* cur = phead->next;
	while (cur != phead)
	{
		DLTNode* next = cur->next;
		free(cur);
		cur = next;
	}
	free(phead);
}

顺序表和链表总结

上方的链表指的是双向链表,顺序表指的是数组顺序表。

相关推荐
Boilermaker19925 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
MM_MS5 小时前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
꧁Q༒ོγ꧂6 小时前
LaTeX 语法入门指南
开发语言·latex
njsgcs6 小时前
ue python二次开发启动教程+ 导入fbx到指定文件夹
开发语言·python·unreal engine·ue
alonewolf_996 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
古城小栈6 小时前
Rust 迭代器产出的引用层数——分水岭
开发语言·rust
ghie90907 小时前
基于MATLAB的TLBO算法优化实现与改进
开发语言·算法·matlab
恋爱绝缘体17 小时前
2020重学C++重构你的C++知识体系
java·开发语言·c++·算法·junit
wuk9987 小时前
VSC优化算法MATLAB实现
开发语言·算法·matlab
AI小怪兽7 小时前
基于YOLOv13的汽车零件分割系统(Python源码+数据集+Pyside6界面)
开发语言·python·yolo·无人机