02-线性结构3 Reversing Linked List (25分)
来源:互联网 发布:免费工程造价软件 编辑:程序博客网 时间:2024/06/05 09:04
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 400000 4 9999900100 1 1230968237 6 -133218 3 0000099999 5 6823712309 2 33218
Sample Output:
00000 4 3321833218 3 1230912309 2 0010000100 1 9999999999 5 6823768237 6 -1
#include <iostream>#include <algorithm>#include <vector>#include <stdio.h>using namespace std;#define maxlen 100001struct node{int add;int data;int next;};typedef struct node Node;Node nodes[maxlen];vector<Node> nodeList;int main(){int firstAdd, N, K;cin>>firstAdd>>N>>K;while(N--){Node n;cin>>n.add>>n.data>>n.next;nodes[n.add] = n;}int address = firstAdd;while(address != -1){nodeList.push_back(nodes[address]);address = nodes[address].next;}//到此nodeList存储的就是顺序的列表int len = nodeList.size();int period = len/K;for(int i = 1; i <= period; i++){int head = (i-1)*K;int end = i*K;reverse(nodeList.begin()+head, nodeList.begin()+end);}//reverse complete//print nodeListfor(int i = 0; i < len-1; i++){//cout<<nodeList[i].add<<" "<<nodeList[i].data<<" "<<nodeList[i+1].add<<endl;printf("%05d %d %05d\n", nodeList[i].add, nodeList[i].data, nodeList[i+1].add);}//cout<<nodeList[len-1].add<<" "<<nodeList[len-1].data<<" "<<-1;printf("%05d %d %d\n",nodeList[len-1].add, nodeList[len-1].data, -1);return 0;}
2017-4-16 待续
00000 4 3321833218 3 1230912309 2 0010000100 1 9999999999 5 6823768237 6 -1
- 02-线性结构3 Reversing Linked List (25分)
- 02-线性结构3 Reversing Linked List (25分)
- 02-线性结构3 Reversing Linked List (25分)
- 02-线性结构3 Reversing Linked List (25分)
- 02-线性结构3 Reversing Linked List (25分)
- 02-线性结构3 Reversing Linked List (25分)
- [pta]02-线性结构3 Reversing Linked List (25分)
- 02-线性结构3 Reversing Linked List (25分)
- 02-线性结构3 Reversing Linked List (25分)
- 02-线性结构3 Reversing Linked List (25分)
- 02-线性结构3 Reversing Linked List(25 分)
- 02-线性结构2 Reversing Linked List (25分)
- 02-线性结构3 Reversing Linked List
- 02-线性结构3 Reversing Linked List
- 02-线性结构3 Reversing Linked List
- 02-线性结构3 Reversing Linked List (25分)
- PAT数据结构_02-线性结构3 Reversing Linked List (25分)
- 02-线性结构2 Reversing Linked List
- java对象和类
- 1094. The Largest Generation (25)
- 数组类型 初始化
- Div+CSS两种盒子模型
- 表的主键与外键
- 02-线性结构3 Reversing Linked List (25分)
- 欢迎使用CSDN-markdown编辑器
- 判断一个串包含的字符串是否在另外个串中出现。
- js中arguments理解
- 九度oj 题目1007:奥运排序问题(但无法通过)
- AC 自动机(模板)
- P3389 【模板】高斯消元法
- RandomAccessFile 类
- Spark入门