PTA1074 Reversing Linked List

1074 Reversing Linked List (25 分)
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 (≤10
​5
​​ ) 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

这个题我刚开始看的时候,说实话 是一点思路没有 太弱了 QWQ~~;
后来在网上翻了翻大佬的思路;
他们的思路是用 一个数组比如a[100000]专门记录位置;
让后输入这个位置的数据时用的是 date[a[]] 这样就把对应的数字输入到了对应的位置上;
记录下一个位置 同理;

我也是算法萌新一枚;
如果有意愿互相交流 我QQ: 2759723679;

看代码吧

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const int max1=100000;

struct node{
	int address;
	int date;
	int next;
}list[max1];//构造出这个链表;
//这里当然不是直接用指针链表;

int main()
{
	int first,n,k;
	cin>>first>>n>>k;
	int address[max1];
	int date[max1];
	int next[max1]; 
	for(int i=0;i<n;i++){
		scanf("%d",&address[i]);//先输入位置
		scanf("%d%d",&date[address[i]],&next[address[i]]);//以这个位置为下标,记录数据,
	} 
	//int m=0;
	int sum=0;;
	for(int i=0;i<n;i++){//先找到这个链表的头节点;
		if(first ==address[i]){
			list[sum].address=first;
			list[sum].date=date[first];
			list[sum].next=next[first];
			sum++;
			break;
		}
	}
	//cout<<"hhh"<<endl;
	
	/*for(int i=0;i<n;i++){
		printf("%.5d %d %.5d\n",address[i],date[address[i]],next[address[i]]);
	}*/
	while(next[first]!=-1){//构造这个链表;
		list[sum].address=next[first];
		list[sum].date=date[next[first]];
		list[sum].next=next[next[first]];
		first=next[first];//把next[first] 赋值给first 就可以直接找下一个位置了;
		//cout<<first<<" ";
		sum++;
	}
//从这链表就构造完成了   下面我写的比较乱  就是让链表转向了;
	int m=k;
	int k2=k;
	//cout<<"sum="<<sum<<endl;
	int num=sum/k;
	int num2=num;
	int e=1;
	int e2=1;
	//cout<<"num="<<num<<endl;
	while(num--){//一共需要转向num段子链表;(子链表   就是比如 6个节点 每3个转向一次 子链表长度就是3   hahah) 
		if(num>0){
		int i;
		for(i=k-1;i>k-1-m;i--){//从数组后向前遍历每小段子链表;
			list[i].next=list[i-1].address;//他位置的下一个位置就是前一个节点;
		} 
	//	cout<<k-1<<"   "<<k-m<<endl;
		e++;
		list[k-m].next=list[e*m-1].address;//这里自己画图想一想 ;
		k=m*e;
	}else{
		int i;
		for(i=k-1;i>k-1-m;i--){
			list[i].next=list[i-1].address;
		} 
		///cout<<k-1<<"   "<<k-m<<endl;
		e++;
		if(sum%m!=0)
		list[k-m].next=list[sum-sum%m].address;
		else
		list[k-m].next=-1;
		k=m*e;
	}
	}
	
	while(num2--){
		for(int i=k2-1;i>k2-1-m;i--){
			if(list[i].next!=-1)
			printf("%.5d %d %.5d\n",list[i].address,list[i].date,list[i].next);
			else
			printf("%.5d %d %d\n",list[i].address,list[i].date,list[i].next);
		}
		e2++;
		k2=m*e2;
	}
	
	for(int i=sum-sum%m;i<sum;i++){//剩下的不足以反转的链表 顺序输出;
		if(list[i].next!=-1)
		printf("%.5d %d %.5d\n",list[i].address,list[i].date,list[i].next);
		else
		printf("%.5d %d %d\n",list[i].address,list[i].date,list[i].next);
	}
	
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值