IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 【PAT(甲级)】1074 Reversing Linked List -> 正文阅读

[数据结构与算法]【PAT(甲级)】1074 Reversing Linked List

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

解题思路:

链表转置问题,首先我们需要定义一个结构来存储这些数据的Address,Data,Next;如果我们想要节约时间的话,可以把一开始对应数组的下标当作是他的地址,这样的话查找就会块很多,查找如下:

for(int i = address;i!=-1;i = number[i].next){
		if(num<K){
			t[tip].push_back(number[i]);
			num++;
		}else{
			num = 1;
			tip++;
			t[tip].push_back(number[i]);
		}
	}

每K个数字就转置一次,所以可以用vector把每K个数据存放一次,再分别转置后凭借在一起即可。

易错点:

1. 测试点1是因为有好几组的数据需要转置,每个数据的末尾,指向的都是后一个数组的首部,你要注意的就是,你指向的后一个数据是否已经转置过了;

2. 测试点5是需要注意每次放在数组里面数据的个数;

代码:

#include<bits/stdc++.h>
using namespace std;

typedef struct Node{
	int add;
	int weight;
	int next;
};

int main(){
	int address,N,K;
	cin>>address>>N>>K;
	Node number[100000];
	for(int i=0;i<N;i++){
		int index,w,next;
		cin>>index>>w>>next;
		number[index].add = index;
		number[index].weight = w;
		number[index].next = next;
	}
	
	vector<Node> t[N/K+1];
	vector<Node> result;
	int tip = 0;
	int num = 0;
	for(int i = address;i!=-1;i = number[i].next){
		if(num<K){
			t[tip].push_back(number[i]);
			num++;
		}else{
			num = 1;// num是重置为1的别搞错了!!!
			tip++;
			t[tip].push_back(number[i]);
		}
	}
	
	for(int i=0;i<=tip;i++){
		if(t[i].size()!=K){
			for(int j=0;j<t[i].size();j++){
				result.push_back(t[i][j]);
			}
		}else{
			if(i!=tip){
				for(int j=t[i].size()-1;j>=0;j--){
					Node k;
					if(j!=0){
						k.add = t[i][j].add;
						k.next = t[i][j-1].add;
						k.weight = t[i][j].weight;
					}else{
						k.add = t[i][j].add;
						k.next = t[i+1][0].add;
						k.weight = t[i][j].weight;
					}
					result.push_back(k);
				}
			}else{
				for(int j=t[i].size()-1;j>=0;j--){
					Node k;
					if(j!=0){
						k.add = t[i][j].add;
						k.next = t[i][j-1].add;
						k.weight = t[i][j].weight;
					}else{
						k.add = t[i][j].add;
						k.next = -1;
						k.weight = t[i][j].weight;
					}
					result.push_back(k);
				}
			}
		}
	}
	for(int i=0;i<tip;i++){ 
		result[i*K+K-1].next = result[i*K+K].add;
	}
	for(auto i = 0;i<result.size();i++){
		if(result[i].next!=-1){
			printf("%05d %d %05d\n",result[i].add,result[i].weight,result[i].next);
		}else{
			printf("%05d %d %d\n",result[i].add,result[i].weight,result[i].next);
		}
	}
	return 0;
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2022-09-24 21:19:25  更:2022-09-24 21:22:23 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 20:33:30-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码