【寒假作业】day2.4

1.

复制代码
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;
    }
}
复制代码
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;
}

3.栈是先进后出的思想,队列是先进先出的思想

4.堆区释放内存时,指针未指向释放内存首地址

相关推荐
wuhen_n42 分钟前
案例分析:从“慢”到“快”,一个后台管理页面的优化全记录
前端·javascript·vue.js
人道领域2 小时前
Day | 11 【苍穹外卖统计业务的实现:含详细思路分析】
java·数据库·后端·苍穹外卖
xiaoye37087 小时前
Java 自动装箱 / 拆箱 原理详解
java·开发语言
YDS8298 小时前
黑马点评 —— 分布式锁详解加源码剖析
java·spring boot·redis·分布式
迷藏4948 小时前
**发散创新:基于 Rust的开源权限管理系统设计与实战**在现代软件架构中,**权限控制**早已不
java·开发语言·rust·开源
升鲜宝供应链及收银系统源代码服务9 小时前
《IntelliJ + Claude Code + Gemini + ChatGPT 实战配置手册升鲜宝》
java·前端·数据库·chatgpt·供应链系统·生鲜配送
daidaidaiyu9 小时前
Nacos实例一则及其源码环境搭建
java·spring
i建模9 小时前
将Edge浏览器的标签页从顶部水平排列**移至左侧垂直侧边栏
前端·edge
跟着珅聪学java9 小时前
js编写中文转unicode 教程
前端·javascript·数据库
小江的记录本9 小时前
【Redis】Redis全方位知识体系(附《Redis常用命令速查表(完整版)》)
java·数据库·redis·后端·python·spring·缓存