首页 > 其他分享 >02-线性结构3 Reversing Linked List(C)

02-线性结构3 Reversing Linked List(C)

时间:2024-07-11 12:29:19浏览次数:10  
标签:02 Reversing int List Next PtrL link data

日常,21分共25分

1. 有多余结点不在链表上.............................................段错误

2.最大N,最后剩K-1不反转 ......................................... 运行超时

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer, and Next is the position of the next node.

Output Specification:

For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218

Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

 我的AC:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

typedef struct LNode *List;
struct LNode{
	int fadr; //first adress
	int data;
	int nadr;
	List Next;
};

List read_link();
void attach(int f, int d, int n, List* PtrL);
List reverse_link(List PtrL);
void Reverse(List *PtrL, List *rear, int *a);
List search_adress(int adress, List PtrL);
void Print_link(List PtrL);
void Free_link(List PtrL);

int main()
{
	List P1, P;
	P1 = read_link();
	P = reverse_link(P1);
	Print_link(P);
	Free_link(P1);
	Free_link(P);
	return 0;
}
List read_link()
{
	int first, length, key;
	int fadr, data, nadr;
	List P, rear;
	scanf("%d%d%d", &first, &length, &key);
	P = (List)malloc(sizeof(struct LNode));
	P ->Next = NULL;
	P ->fadr = first;
	P ->data = length;
	P ->nadr = key;
	rear = P;
	while(length--){
		scanf("%d%d%d", &fadr, &data, &nadr);
		attach(fadr, data, nadr, &rear);
	}
	return P;
}
void attach(int f, int d, int n, List* PtrL)
{
	List A1;
	A1 = (List)malloc(sizeof(struct LNode));
	A1 ->fadr = f;
	A1 ->data = d;
	A1 ->nadr = n;
	A1 ->Next = NULL;
	(*PtrL) ->Next = A1;
	*PtrL = A1;
}
List reverse_link(List PtrL)
{
	List Link, P;
	List new_link, rear, T;
	int adress = PtrL ->fadr;
	int a[PtrL ->data];
	                                                                                                                                                                                                                                                                                                                                         	
	new_link = (List)malloc(sizeof(struct LNode));
	new_link ->Next = NULL;
	rear = new_link;
	
	Link = PtrL ->Next;
	for(int i = 0; i < PtrL ->data; i++)
	{
		P = search_adress(adress, Link);
		a[i] = P ->fadr;
		adress = P ->nadr; 
	}
	Reverse(&PtrL, &rear, a);
	T = new_link;
	new_link = new_link ->Next;
	free(T);
	return new_link;
}
void Reverse(List *PtrL, List *rear, int *a)
{
	List P;
	int flag = 0, Temp;
	int j = (*PtrL)->nadr -1;
	bool sign = false;
	
	while(flag != (*PtrL)->data - 1){
		if((*PtrL)->nadr != 1 && j % (*PtrL)->nadr == 0){
			P = search_adress(a[j], (*PtrL)->Next);
			Temp = j;
			if((*PtrL) ->data - (flag + 1) >= (*PtrL)->nadr){
				j = flag + ((*PtrL)->nadr);
			}else{
				j = flag + 1;
				sign = true;
			}
			attach(a[Temp], P->data, a[j], rear);
			flag++;
		}
		P = search_adress(a[j], (*PtrL)->Next);
		if(sign || (*PtrL)->nadr == 1){
			attach(a[j], P->data, a[j + 1], rear);
			j ++;
		}else{
			attach(a[j], P->data, a[j-1], rear);
			j --;
		}
		flag++;
	}
	P = search_adress(a[j], (*PtrL)->Next);
	attach(a[j], P->data, -1, rear);
}
List search_adress(int adress, List PtrL)
{
	if(!PtrL){
		return NULL;
	}
	while(PtrL)
	{
		if(PtrL ->fadr == adress)
			return PtrL;
		PtrL = PtrL ->Next;
	}
	return NULL;
}
void Print_link(List PtrL)
{
	if(!PtrL){
		return ;
	}
	while(PtrL->Next){
		printf("%05d %d %05d\n", PtrL->fadr, PtrL ->data, PtrL ->nadr);
		PtrL = PtrL ->Next;
	}
	printf("%05d %d %d\n", PtrL->fadr, PtrL ->data, PtrL ->nadr);
	return ;
}
void Free_link(List PtrL)
{
	List T;
	if(!PtrL){
		return ;
	}
	while(PtrL){
		T = PtrL;
		PtrL = PtrL ->Next;
		free(T);
	}
}

标签:02,Reversing,int,List,Next,PtrL,link,data
From: https://blog.csdn.net/2301_80474443/article/details/140348579

相关文章

  • 2024最新,李宏毅深度学习!绝对值得反复阅读!
    介绍    李宏毅老师是台湾大学的教授,他的《机器学习》(2021年春)视频课程是深度学习领域中文视频中的经典之一。李老师风趣幽默的授课风格深受学生喜爱,他以很多动漫相关的有趣例子来解释深度学习理论,让原本晦涩难懂的内容变得轻松易懂。这门课程内容涵盖了深度学习中必须......
  • 上交2024最新-动手学大模型
    介绍  今天分享一个上海交大的免费的大模型,有相关文档和Slides,目前是2.2K星标,还是挺火的!获取:上交2024最新-《动手学大模型》实战分享!  《动手学大模型》系列编程实践,由上海交通大学2024年春季《人工智能安全技术》(NIS3353)讲义拓展而来(教师:张倬胜),旨在提供大模型相......
  • 上海月赛2020年4月
    丙组T1:https://www.iai.sh.cn/problem/24#include<bits/stdc++.h>usingnamespacestd;intmain(){inta,b,c,d;cin>>a>>b>>c>>d;intcnt=0;if(a>=90)cnt++;if(b>=90)cnt++;if(c......
  • 界面组件Kendo UI for React 2024 Q2亮点 - 生成式AI集成、设计系统增强
    随着最新的2024年第二季度发布,KendoUIforReact为应用程序开发设定了标准,包括生成式AI集成、增强的设计系统功能和可访问的数据可视化。新的2024年第二季度版本为应用程序界面提供了人工智能(AI)提示,从设计到代码的生产力增强、可访问性改进、一系列新的UI组件等。KendoUI致力......
  • 【2024-07-10】我们的过夜菜
    20:00事情都是想起来千难万险,但事到临头总有办法。                                                 ——约翰.缪尔何太的部门领导还有两年退休,在这位女领导的带领下,......
  • 论文分享|KDD2024‘北航|平等对待每种语言:CCRK—1对K对比学习一致提升跨语言跨模态检
    推荐一篇笔者参与的KDD2024工作,面向多语言场景下的图文检索的CCRK。核心创新点可以用下图总结:我们提出了一种新颖的1对K对比学习损失,同时解决了CCR任务中模态内错误传播和模态间优化偏差问题,在训练中平等对待各种语言。我们改进了现有的预训练中M-ITM和M-MLM任务的难负例采......
  • 论文分享|ACL2024|RAG相关论文简读
    本文通过简读ACL2024中RAG和检索相关且在谷歌学术已公开的21篇论文,追踪RAG的研究热点这里附上ACL2024论文列表链接:https://2024.aclweb.org/program/main_conference_papers/1.UnsupervisedInformationRefinementTrainingofLargeLanguageModelsforRetrieval-Augm......
  • Linux学习笔记(02)——文件相关知识
    文件系统结构/bin存放二进制可执行文件,这些命令在单用户模式下也能够使用。可以被root和一般的账号使用。/bootUbuntu内核和启动文件,比如vmlinuz-xxx。gurb引导装载程序。/dev设备驱动文件/etc存放一些系统配置文件,比如用户账号和密码文件,各种服务的起始地址。/h......
  • 2024 数码折腾
    笔记本IdeaPad310 7200u进bios必须 专门usb旁边重置小孔 F2这些传统案件都不行 网卡也是要联想专用   搞好准备上LMDE6  LXQT桌面还不成熟研究微pe下如何像安装win镜像一样不要刻录到U盘  win10ltsc还行,win7也继续用 卡贴14/15plus  折腾半天,贴吧......
  • 2024最适合小白的Midjourney教程,值得收藏!
    一、Midjourney的提示词1、提示可以包括一个或多个图像URL、多个文本短语以及一个或多个参数1)ImagePrompts(图像提示):可以将图像URL添加到提示中以影响最终结果的样式和内容。图像URL始终出现在提示的前面。文件应以.png、.gif、.webp、.jpg或.jpeg结尾。2)Text......