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

目录

基本函数实现

链表声明

总的函数实现声明

创建一个节点

初始化链表

打印

尾插

尾删

头插

头删

查找

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

顺序表和链表总结

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

相关推荐
灵晔君几秒前
【Linux】进程控制(一)——进程创建、进程终止与进程等待
linux·c语言·算法
特立独行的猫a2 分钟前
Tauri v2的Rust应用 → HarmonyOS(鸿蒙 PC)移植30分钟速成指南
开发语言·rust·harmonyos·tauri·移植·鸿蒙pc
迷迭香yy7 分钟前
大宗交易折溢价因子怎么挖掘本地化Python全流程实战
开发语言·人工智能·python
xieliyu.11 分钟前
UPD协议结构以及开发中注意事项
java·开发语言·笔记·java-ee
Yweir16 分钟前
AI大模型开发-Python介绍、版本说明
开发语言·人工智能·python
峥嵘life8 小时前
Android16 311Y3 EAP-TLS 网络连接失败分析与修复总结
android·开发语言·人工智能·php
mqiqe11 小时前
AgentScope Java Harness:4. 双层记忆系统 让 Agent 拥有真正的“长期大脑“
java·开发语言
gugucoding11 小时前
46. 【Java】JUC并发工具:让并发更简单
java·开发语言
hold?fish:palm11 小时前
24 回文链表
数据结构·c++·链表
Nebula_g12 小时前
JavaSE基础语法:面向对象高级(代码中的成分)
java·开发语言·编程·javase·技术栈·高级语法