华清远见作业第十六天

思维导图:

双向循环链表头插入:

代码:

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

画图:

运行效果:

相关推荐
Fish41747 小时前
《C语言程序设计》琐碎知识点总结笔记
c语言·命令行参数·c语言程序设计·变量存储类型·函数存储类型·编译预处理
长安第一美人8 小时前
C 语言可变参数(...)实战:从 logger_print 到通用日志函数
c语言·开发语言·嵌入式硬件·日志·工业应用开发
superman超哥8 小时前
仓颉语言中基本数据类型的深度剖析与工程实践
c语言·开发语言·python·算法·仓颉
不爱吃糖的程序媛9 小时前
Ascend C开发工具包(asc-devkit)技术解读
c语言·开发语言
李绍熹10 小时前
c语言字符串操作示例
c语言
Damon_X10 小时前
extern “C“
c语言
GoWjw11 小时前
在C&C++指针的惯用方法
c语言·开发语言·c++
Coding Peasant11 小时前
GD32E230 I2C从机功能深度解析与实现指南
c语言·stm32·单片机·mcu·arm
superman超哥11 小时前
仓颉语言中错误恢复策略的深度剖析与工程实践
c语言·开发语言·c++·python·仓颉
玖剹11 小时前
记忆化搜索题目(二)
c语言·c++·算法·leetcode·深度优先·剪枝·深度优先遍历