02-线性结构2 Reversing Linked List
2016-05-31 15:38
387 查看
Given a constant KK and a singly linked list LL, you are supposed to reverse the links of every KK elements on LL. For example, given LL being 1→2→3→4→5→6, if K = 3K=3, then you must output 3→2→1→6→5→4; if K = 4K=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 NN (\le 10^5≤10
5
) which is the total number of nodes, and a positive KK (\le N≤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 NN 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
思路:建立双向链表,依次访问直到数目为倒序的次数,若能访问,则用双向链表倒序输出,否则就正序输出。
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive NN (\le 10^5≤10
5
) which is the total number of nodes, and a positive KK (\le N≤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 NN 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
思路:建立双向链表,依次访问直到数目为倒序的次数,若能访问,则用双向链表倒序输出,否则就正序输出。
#include <iostream> #include <vector> using namespace std; typedef struct link { int rank; int base_add; int next_add; link* parent; link* child; }link; int main() { int begin_add, N, listnum; cin >> begin_add; cin >> N; cin >> listnum; vector<pair<int, int>> address(N);//first is base address,second is next address int base_add, next_add, rank; for (int i = 0; i < N; i++) { cin >> base_add; cin >> rank; cin >> next_add; address[rank - 1].first = base_add; address[rank - 1].second = next_add; } link* top; link* current; link* child; top = (link*)malloc(sizeof(link)); current = top; top->parent = NULL; for (int i = 0; i < N; i++) { current->base_add = address[i].first; current->next_add = address[i].second; current->rank = i+1; if (i != N - 1)//not the last node { child = (link*)malloc(sizeof(link)); current->child = child; child->parent = current; current = child; } else { current->child = NULL; } } int count; current = top; bool flag = false; while (current != NULL) { count = 1; child = current; if (flag) printf("%05d\n", child->base_add); while (current != NULL&&count!=listnum) { current = current->child; count++; } if (count == listnum) { child = current; for (int j = 0; j < listnum-1; j++) { //if (child->parent != NULL) printf("%05d %d %05d\n", child->base_add, child->rank, child->parent->base_add); child = child->parent; } printf("%05d %d ", child->base_add, child->rank); current = current->child; flag = true; } else { for (int j = 0; j < count-2; j++) { printf("%05d %d %05d\n", child->base_add, child->rank, child->child->base_add); child = child->child; } printf("%05d %d %d\n", child->base_add, child->rank, -1); break; } } return 0; }
相关文章推荐
- Public Function Merge2(rng As Range, splitter As String) As String
- JavaScript学习--Item22 Date对象全解析
- 【Leetcode】Longest Increasing Path in a Matrix
- 使用google的zxing生成二维码
- Live Demo - jQuery EasyUI
- 人月神话阅读笔记02
- java多线程
- linux流程控制及变量比较
- io
- android 游戏 实战 打飞机 实现主角以及主角相关元素(3)
- 进程与线程的一个简单解释
- 中兴历年笔试题自己收藏用01
- activeMQ
- 序列帧动画
- 泡泡窗口实现下拉菜单,以及改变布局高度
- MySQL学习第六天 学习MySQL基本数据类型
- java常用排序算法
- JSP数据显示在页面一行内
- Android Invalid maximum heap size: -Xmx 解决方法
- centos6.7上使用nginx实现负载均衡!