【寒假作业】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.堆区释放内存时,指针未指向释放内存首地址

相关推荐
前端双越老师13 分钟前
前端面试常见的 10 个场景题
前端·面试·求职
孟祥_成都1 小时前
【全网最通俗!新手到AI全栈开发必读】 AI 是如何进化到大模型的
前端·人工智能·全栈
Seven971 小时前
剑指offer-79、最⻓不含重复字符的⼦字符串
java
牛奶1 小时前
AI辅助开发的基础概念
前端·人工智能·ai编程
摸鱼的春哥1 小时前
Agent教程15:认识LangChain,Agent框架的王(上)
前端·javascript·后端
明月_清风2 小时前
自定义右键菜单:在项目里实现“选中文字即刻生成新提示”
前端·javascript
明月_清风2 小时前
告别后端转换:高质量批量导出实战
前端·javascript
刘发财7 小时前
弃用html2pdf.js,这个html转pdf方案能力是它的几十倍
前端·javascript·github
牛奶10 小时前
2026年大模型怎么选?前端人实用对比
前端·人工智能·ai编程
牛奶10 小时前
前端人为什么要学AI?
前端·人工智能·ai编程