数据结构day4作业

1.单链表任意位置删除

cs 复制代码
	datetype pos;
	printf("please input pos");
	scanf("%d",&pos);
	head=delete_all(head,pos);
	Output(head);
cs 复制代码
Linklist delete_all(Linklist head,datetype pos)
{
	if(pos<1||pos>length(head)||head==NULL)
		return head;
	if(head->next==NULL)
	{
		free(head);
		head=NULL;
	}
	else
	{
		if(pos==1)
		{
			head=delete_head(head);
			return head;
		}
		else
		{
		Linklist p=head;
		for(int i=1;i<pos-1;i++)
		{
			p=p->next;
		}
		Linklist q=p->next;
		p->next=q->next;

		free(q);
		p=NULL;
		return head;
		}
	}
}

2.单链表任意位置修改

cs 复制代码
	datetype pos;
	printf("please input pos");
	scanf("%d",&pos);
	printf("please input element");
	scanf("%d",&element);
	mod_all(head,pos,element);
	Output(head);
cs 复制代码
void mod_all(Linklist head,datetype pos,datetype element)
{
	Linklist p=head;
	if(head==NULL||pos<1||pos>length(head))
		return;
	for(int i=1;i<pos;i++)
	{
		p=p->next;
	}
	p->date=element;
}

3.单链表任意位置查找

cs 复制代码
	datetype pos;
	printf("please input pos");
	scanf("%d",&pos);
	find_all(head,pos);
	Output(head);
cs 复制代码
void find_all(Linklist head,datetype pos)
{
	Linklist p=head;
	if(head==NULL||pos<1||pos>length(head))
		return;
	for(int i=1;i<pos;i++)
	{
		p=p->next;
	}
	printf("%d\n",p->date);
}

4.单链表任意元素查找

cs 复制代码
	datetype key;
	printf("please input key");
	scanf("%d",&key);
	datetype index=find_ele(head,key);
	if(index==-1)
		printf("不存在\n");
	else{
		printf("在第%d的位置\n",index);
	}
	
cs 复制代码
int find_ele(Linklist head,datetype key)
{
	if(head==NULL)
		return -1;
	datetype count=0;
	Linklist p=head;
	for(int i=1;i<=length(head);i++)
	{
		count++;
		if(p->date==key)
			return count;
		p=p->next;
	}
	return -1;
}

5.单链表任意元素修改

cs 复制代码
	datetype key;
	printf("please input key");
	scanf("%d",&key);
	printf("please input element");
	scanf("%d",&element);
	mod_ele(head,key,element);
	Output(head);
cs 复制代码
int mod_ele(Linklist head,datetype key,datetype element)
{
	if(head==NULL)
		return -1;
	datetype pos=find_ele(head,key);
	Linklist p=head;
	for(int i=1;i<pos;i++){
		p=p->next;
	}
	p->date=element;
}

6.单链表任意元素删除

cs 复制代码
	datetype key;
	printf("please input key");
	scanf("%d",&key);
	delete_ele(head,key);
	Output(head);
cs 复制代码
int delete_ele(Linklist head,datetype key)
{
	if(head==NULL)
		return -1;
	Linklist p=head;
	datetype pos=find_ele(head,key);
	for(int i=1;i<pos-1;i++)
	{
		p=p->next;
	}
	Linklist q=p->next;
	p->next=q->next;
	free(q);
	q=NULL;
	return 0;
}

7.单链表逆置(面试)

cs 复制代码
	head=nizhi(head);
	Output(head);
cs 复制代码
Linklist nizhi(Linklist head)
{
	if(head==NULL)
		return head;
	Linklist p=head->next;
	head->next=NULL;
	while(p)
	{
		Linklist t=p;
		p=p->next;
		t->next=head;
		head=t;
	}
	return head;
}

8.单链表查找倒数第n个节点(面试)

cs 复制代码
	printf("please input n:");
	scanf("%d",&n);
	back_node(head,n);
cs 复制代码
datetype back_node(Linklist head, datetype n)
{
	if(head==NULL||n<1||n>length(head))
		return -1;
	datetype pos=length(head)-n+1;
	find_all(head,pos);
	return 0;

}

9.单链表排序(面试)

cs 复制代码
	datetype flag;
	printf("printf input flag:(0|1)");
	scanf("%d",&flag);
	if(flag==1|flag==0){
		sort_list(head,flag);
		Output(head);
	}
	else{
		printf("input error\n");
	}
cs 复制代码
datetype sort_list(Linklist head,datetype flag)
{
	if(head==NULL)
		return -1;
	Linklist p=head;
	if(flag==1){
	for(int i=0;i<length(head)-1;i++){
		p=head;
		for(int j=0;j<length(head)-i-1;j++){
			if(p->date > p->next->date){
				p->date ^= p->next->date;
				p->next->date ^= p->date;
				p->date ^= p->next->date;
			}
			p=p->next;
		}
	}
	}
	else{
	for(int i=0;i<length(head)-1;i++){
		p=head;
		for(int j=0;j<length(head)-i-1;j++){
			if(p->date < p->next->date){
				p->date ^= p->next->date;
				p->next->date ^= p->date;
				p->date ^= p->next->date;
			}
			p=p->next;
		}
	}
		
	}
	return 0;
}

10.单链表释放内存

cs 复制代码
 	head=free_list(head);
cs 复制代码
Linklist free_list(Linklist head)
{
	if(head==NULL)
		return head;
	Linklist p=head;
	while(head)
	{
		p=head;
		head=head->next;
		free(p);
	}
	p=NULL;
	return head;
}
相关推荐
夏末秋也凉3 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
王老师青少年编程3 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛
liuyuzhongcc6 小时前
List 接口中的 sort 和 forEach 方法
java·数据结构·python·list
计算机小白一个8 小时前
蓝桥杯 Java B 组之背包问题、最长递增子序列(LIS)
java·数据结构·蓝桥杯
卑微的小鬼8 小时前
数据库使用B+树的原因
数据结构·b树
cookies_s_s8 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法
醉城夜风~10 小时前
[数据结构]双链表详解
数据结构
gyeolhada10 小时前
2025蓝桥杯JAVA编程题练习Day5
java·数据结构·算法·蓝桥杯
阿巴~阿巴~10 小时前
多源 BFS 算法详解:从原理到实现,高效解决多源最短路问题
开发语言·数据结构·c++·算法·宽度优先
刃神太酷啦12 小时前
堆和priority_queue
数据结构·c++·蓝桥杯c++组