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

目录

基本函数实现

链表声明

总的函数实现声明

创建一个节点

初始化链表

打印

尾插

尾删

头插

头删

查找

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 小时前
JavaScript实现教培行业意向登记系统
开发语言·javascript·ecmascript
zzzll11116 小时前
Claude Code离线安装方案揭秘
开发语言·php
wabs6666 小时前
关于图论【卡码网117.软件构建的思考】
数据结构·算法·软件构建·图论·卡码网
wling03016 小时前
vasp虚频计算-python脚本
开发语言·windows·python·vasp计算
郝学胜-神的一滴7 小时前
Qt 高级编程 040:按钮悬浮弹出滑块弹窗的完整攻略
开发语言·c++·qt·软件工程·用户界面
Tongzhi20267 小时前
从部署到运维:通芝科技无感考勤一体机的全流程效率解析
运维·数据结构·科技·算法·贪心算法
xrandzj7 小时前
Python面向对象编程入门:类、实例、初始化与封装实践
开发语言·python
DLYSB_8 小时前
API 网关流量洪峰与突发 CC 攻击:我用 Go 写了个“现场物理防御哨兵”,把故障响应压缩到秒级
开发语言·后端·golang·报警灯
雨田言炎10 小时前
四、关于Qt项目需要知道的
开发语言·笔记·qt
程序喵大人11 小时前
【C++进阶】STL算法与函数对象 - 02 sort为什么需要随机访问迭代器
开发语言·c++·算法