【无标题】

cpp 复制代码
linklist create_node()
{
	linklist s=(linklist)malloc(sizeof(struct Node));
	if(s==NULL)
		return s;
	s->data=0;
	s->next=NULL;
	s->prev=NULL;
	return s;
}
int length(linklist head)
{
	int len=0;
	linklist p=head;
	while(p)
	{
		p=p->next;
		len++;
	}
	return len;
	
}
void output(linklist head)
{
	if(head==NULL)
	{
		puts("EMPTY");
		return;
	}
	linklist p=head;
	while(p)
	{
		printf("%-5d",p->data);
		p=p->next;
	}
	puts("");
}
linklist insert_head(linklist head,datatype element)
{
	linklist s=create_node();
	s->data=element;
	if(head==NULL)
		head=s;
	else
	{
		s->next=head;
		s->prev=NULL;
		head=s;
	}
	return head; 
}
linklist insert_rear(linklist head,datatype element)
{
	linklist s=create_node();
	s->data=element;
	if(head==NULL)
		head=s;
	else
	{
		linklist p=head;
		while(p->next)
		{
			p=p->next;
		}
		p->next=s;
		s->next=NULL;
		s->prev=p;
	}
	return head;
}
linklist del_head(linklist head)
{
	if(head==NULL)
		return head;
	else if(head->next==NULL)
	{
		linklist del=head;
		head=head->next;
		head->prev=NULL;
		free(del);
		del=NULL;
		return head;
	}
}
linklist del_rear(linklist head)
{
	if(head==NULL)
	return head;
	else if(head->next==NULL)
	{
		free(head);
		head=NULL;
		return head;
	}
	else
	{
		linklist del=head;
		while(del->next)
		{
		del=del->next;
		}
		del->prev->next=NULL;
		free(del);
		del=NULL;
		return head;
	}
cpp 复制代码
linklist insert_pos(linklist head,int pos,datatype element)
{
	linklist s=create_node();
	s->data=element;
	if(head==NULL || pos==1)
		head=insert_head(head,element);
	    return head;
	if(pos<1 || pos>length(head)+1)
	{
		puts("ERROR");
		return head;
	}
	else
	{
		linklist p=head;
		for(int i=1;i<pos-1;i++)
		{
			p=p->next;
		}
		s->next=p->next;
		s->prev=p;
		p->next->prev=s;
		p->next=s;
		return head;
	}
}
linklist del_pos(linklist head,int pos)
{
	if(head==NULL)
		return head;
	if(pos<1 || pos>length(head)+1)
	{
		puts("ERROR");
		return head;
	}
	else
	{
		if(pos==1)
		{
			head=del_head(head);
			return head;
		}
		else if(pos==length(head))
		{
			head=del_rear(head);
			return head;
		}
		else
		{
			linklist p=head;
			for(int i=1;i<pos;i++)
			{
				p=p->next;
			}
			p->prev->next=p->next;
			p->next->prev=p->prev;
			free(p);
			p=NULL;
			return head;
		}
	}
}
linklist change_pos(linklist head,int pos,datatype element)
{

	if(pos<1 || pos>length(head)+1)
	{
		puts("ERROR");
		return head;
	}
	linklist p=head;
	for(int i=1;i<pos;i++)
	{
		p=p->next;
	}
	p->data=element;
	return head;


}
void find_pos(linklist head,int pos)
{
	if(pos<1 || pos>length(head)+1)
	{
		puts("ERROR");
		return;
	}	
	linklist p=head;
	for(int i=1;i<pos;i++)
	{
		p=p->next;
	}
	printf("%d\n",p->data);
	return;
}

栈:在一段(栈尾)实现插入和删除,先进后出;

队列:在两端(队头、队尾)实现插入(队尾)和删除(队头),先进先出;

由于程序的运行导致指针的位置变化,在释放地址时,指针未指向首地址,导致部分地址没有得到正确释放且丢失寻找能力。

相关推荐
夏梦春蝉22 分钟前
ES6从入门到精通:模块化
前端·ecmascript·es6
云资源服务商1 小时前
解锁阿里云日志服务SLS:云时代的日志管理利器
服务器·阿里云·云计算
拓端研究室1 小时前
视频讲解:门槛效应模型Threshold Effect分析数字金融指数与消费结构数据
前端·算法
朱包林2 小时前
day45-nginx复杂跳转与https
linux·运维·服务器·网络·云计算
工一木子2 小时前
URL时间戳参数深度解析:缓存破坏与前端优化的前世今生
前端·缓存
孞㐑¥4 小时前
Linux之Socket 编程 UDP
linux·服务器·c++·经验分享·笔记·网络协议·udp
半点寒12W4 小时前
微信小程序实现路由拦截的方法
前端
某公司摸鱼前端5 小时前
uniapp socket 封装 (可拿去直接用)
前端·javascript·websocket·uni-app
要加油哦~5 小时前
vue | 插件 | 移动文件的插件 —— move-file-cli 插件 的安装与使用
前端·javascript·vue.js
小林学习编程5 小时前
Springboot + vue + uni-app小程序web端全套家具商场
前端·vue.js·spring boot