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
做了好久 = = 纪念一下 调bug太长时间了 一开始用链表 有个超时 用数据就快了 牺牲空间换时间。。。。
#include <stdio.h> #define MaxSize 100001 struct Node{ int data; int next; int pre; }; typedef struct Node Node; void Read(int n,Node* List); void Print(int firstnode,Node* List); void reverse(Node* List,int k,int firstnode); void Addpre(Node* List, int firstnode); void reversePrint(Node* List,int firstnode); int calculateTail(int head,Node* List,int k); // 给出头 计算包括头的第k个元素 即连续k个元素的尾部下标 int main(){ Node List[MaxSize]; int firstnode,n,k; scanf("%d %d %d",&firstnode,&n,&k); Read(n, List); // Print(firstnode, List); Addpre(List, firstnode); // reversePrint(List,firstnode); reverse(List, k, firstnode); return 0; } void Read(int n,Node* List){ int a; while (n) { scanf("%d",&a); scanf("%d %d",&List[a].data,&List[a].next); a = List[a].next; --n; } } void Print(int firstnode,Node* List){ int a = firstnode; while (a != -1) { if(List[a].next != -1){ printf("%05d %d %05d\n",a,List[a].data,List[a].next); }else{ printf("%05d %d %d",a,List[a].data,List[a].next); } a = List[a].next; } } void reverse(Node* List,int k,int firstnode){ int head, tail, a; int i = k; head = firstnode; tail = calculateTail(head, List, k); // printf("%d\n",List[tail].data); while (tail != -1) { head = List[tail].next; // printf("head is %d\n",head); a = tail; while (i) { if(i == 1 && head == -1){ // printf("flag = 0\n"); printf("%05d %d %d",a,List[a].data,head); return; }else if(i == 1 && calculateTail(head, List, k) != -1){ // printf("flag = 1\n"); printf("%05d %d %05d\n",a,List[a].data,calculateTail(head, List, k)); }else if(i == 1 && calculateTail(head, List, k) == -1){ printf("%05d %d %05d\n",a,List[a].data,head); }else{ // printf("flag = 2\n"); printf("%05d %d %05d\n",a,List[a].data,List[a].pre); } a = List[a].pre; --i; } tail = calculateTail(head, List, k); // printf("tail is %d\n",tail); i = k; } a = head; while (List[a].next != -1) { // printf("flag = 3\n"); printf("%05d %d %05d\n",a,List[a].data,List[a].next); a = List[a].next; } printf("%05d %d %d",a,List[a].data,List[a].next); } void Addpre(Node* List, int firstnode){ int a = firstnode; int t = a; List[a].pre = -1; a = List[a].next; while (a != -1) { List[a].pre = t; t = a; a = List[a].next; } } void reversePrint(Node* List,int firstnode){ int a = firstnode; while (List[a].next != -1) { a = List[a].next; } while (List[a].pre != -1) { printf("%05d %d %05d\n",a,List[a].data,List[a].pre); a = List[a].pre; } printf("%05d %d %d",a,List[a].data,List[a].pre); } int calculateTail(int head,Node* List,int k){ int a = head; int i = k - 1; while (i && a != -1) { a = List[a].next; --i; } return a; }
标签:Reversing,int,printf,List,head,next,firstnode,Linked From: https://www.cnblogs.com/xinrenbool/p/16900028.html