华清远见作业第十六天

思维导图:

双向循环链表头插入:

代码:

cpp 复制代码
Doublelist insert_head(Doublelist head,datatype element)
{
	//创建新节点s
	Doublelist s=create_node();
	if(NULL==s)
	{
		return head;
	}
	s->data=element;//数据存储
	//判断链表是否为空
	if(NULL==head)
	{
		head=s;
		return head;
	}
	else
	{	
		Doublelist rear=head->priv;
		//开始插入
		s->next=head;//把原来的头放到s的后面
		head->priv=s;//原来的head前指针指向s
		head=s;//s成为新的头
		//形成两个环
		head->priv=rear;
		rear->next=head;
		return head;
		
	}
	
}

画图:

运行效果:

双向循环链表尾插入:

代码:

cpp 复制代码
//尾插
Doublelist insert_rear(Doublelist head,datatype element)
{
	//创建新节点s
	Doublelist s=create_node();
	if(NULL==s)
	{
		return head;
	}
	s->data=element;//数据存储
	//判断链表是否为空
	if(NULL==head)
	{
		head=s;
		return head;
	}
	else
	{	
		Doublelist p=head;		
		//找最后一个节点
		while(p->next!=head)
		{
			p=p->next;
		}
		//开始插入
		p->next=s;//把数据存放s放到的最后一位的后面
		s->priv=p;//s的前一个是原来的最后一个成为新的最后一个s
		s->next=head;
		head->priv=s;
		return head;
		
	}
	
}

画图:

运行效果:

双向循环链表头删除

代码:

cpp 复制代码
//头删除
Doublelist del_head(Doublelist head)
{
	if(NULL==head)
	{
		return head;
	}
	 if(head->next==head)
	{
		free(head);
		head=NULL;
		return head;
	}
	else
	{
		
		Doublelist rear=head->priv;//定位到最后一个节点,并存储
		Doublelist del=head;//要删除的头标志到del
		head=head->next;//头节点变成原来的下一个

		free(del);//释放del
		del=NULL;//防止del
		//形成环
		rear->next=head;
		head->priv=rear;
		

		return head;
		
	
	}

}

画图:

运行效果:

双向循环链表尾删除

代码:

cpp 复制代码
//尾巴删除
Doublelist del_rear(Doublelist head)
{
	if(NULL==head)
	{
		return head;
	}
	else if(head->next==NULL)
	{
		free(head);
		head=NULL;
		return head;
	}
	else
	{
		Doublelist p=head;		
		//找倒数第二个节点
		while(p->next->next!=head)
		{
			p=p->next;
		}
		//标记倒数第一个
		Doublelist del=p->next;
		free(del);//释放最后一个
		del=NULL;//防止野指针
		p->next=head;//新的最后一个指向空
		head->priv=p;
		return head;
			
	}
}

画图:

运行效果:

相关推荐
空中海10 分钟前
Redis 从零到精通:9大数据结构 × 11个高频工程实战场景完全手册
数据结构·数据库·redis
地球资源数据云1 小时前
1951-2025年中国逐年1千米逐月总降水量区域统计数据集_年表_县
大数据·数据结构·数据库·数据仓库·人工智能
Tel199253080041 小时前
ENDAT2.2 协议信号转 SSI /BISS-C转换卡 ENDAT2.2 协议信号转DMC多摩川高速协议转换器 互转卡
c语言·开发语言·网络
三品吉他手会点灯2 小时前
C语言学习笔记 - 12.C语言简介 - 一元二次方程详解
c语言·笔记·学习
记录无知岁月2 小时前
【C/C++】头文件包含问题分析
c语言·开发语言·c++
cpp_25012 小时前
P2639 [USACO09OCT] Bessie‘s Weight Problem G
数据结构·算法·动态规划·题解·洛谷·背包dp
郝学胜-神的一滴2 小时前
[力扣 227] 双栈妙解表达式计算:从思维逻辑到C++实战,吃透反向波兰式底层原理
java·前端·数据结构·c++·算法
菜鸟丁小真3 小时前
LeetCode hot100 -131.分割回文串
数据结构·算法·leetcode·知识点总结
数智化精益手记局3 小时前
8d报告案例分析:拆解8d报告案例分析的8个步骤,解决生产现场重复发生的质量难题
大数据·数据结构·数据库·人工智能·精益工程
笨笨饿3 小时前
66_C语言与微控制器底层开发
linux·c语言·网络·数据结构·算法·机器人·个人开发