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

目录

基本函数实现

链表声明

总的函数实现声明

创建一个节点

初始化链表

打印

尾插

尾删

头插

头删

查找

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);
}

顺序表和链表总结

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

相关推荐
郝学胜-神的一滴3 小时前
Effective Python 条款44:善用属性注解
开发语言·c++·vscode·python·算法·stl
嵌入式 Linux手记14 小时前
柔性数组:为什么写 data[] 而不是 data[0]
linux·c语言·面试·职场和发展·嵌入式·柔性数组
两点王爷16 小时前
Java 面试高频:JWT、Cookie、Session、Token 鉴权详解
java·开发语言·面试
微笑星河q17 小时前
C++高阶之【多态】
开发语言·c++
ClkLog-开源埋点用户分析20 小时前
【产品说明】【埋点分析】ClkLog 商业版 Java 运行环境安全更新说明
java·开发语言
yuzhiboyouye21 小时前
《阿里巴巴 Java 开发手册》的 MySQL 核心规范
java·开发语言·mysql
郝学胜-神的一滴1 天前
Effective STL 条款12:对容器线程安全性的现实期望
c语言·开发语言·c++·qt·网络协议·rpc·numpy
geovindu1 天前
python: KNeighborsClassifier K
开发语言·后端·python·算法
让我们一起加油好吗1 天前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷