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
调试有点麻烦,坑有点多:
每K个就要反转;有些时候不用反转
C语言代码:
#include<stdio.h> #include<stdlib.h> #define MAX 100004 typedef struct Node* List; struct Node{ int Data; int Next; }HeadList[MAX]; void Insert(int A, int D, int Ne, List HeadList); int Reverse(int K,int FA,List HeadList,int Rear); void Print(int FA,List HeadList); int FindAdress(int K,int FA, List HeadList); int FindPre(int K, int FA, List HeadList); int FindLength(int FA, List HeadList); int main(){ int N,K; int FA; int Temp; int Rear = 100004; int A, D, Ne; int i; scanf("%d %d %d",&FA, &N, &K); for( i = 0; i < N; i++){ scanf("%d %d %d",&A, &D, &Ne); Insert(A, D, Ne, HeadList); } N = FindLength(FA,HeadList); Temp = FA; FA = FindAdress(K,FA,HeadList); while(N >= K){ Temp = Reverse(K,Temp,HeadList,Rear); N -= K; Rear = FindAdress(K , Temp, HeadList); Temp = HeadList[Rear].Next; } //FA = Reverse(K,FA,HeadList); Print(FA,HeadList); } void Insert(int A, int D, int Ne, List HeadList){ HeadList[A].Data = D; HeadList[A].Next = Ne; } int FindPre(int K, int FA, List HeadList){ int Kadress; int count = 1; Kadress = FA; // printf("OK"); while(count < K - 1){ Kadress = HeadList[Kadress].Next; count++; } // printf("%d\n",Kadress); return Kadress; } int FindAdress(int K, int FA, List HeadList){ int Kadress; Kadress = FA; int count = 1; while(count < K){ Kadress = HeadList[Kadress].Next; count++; } return Kadress; } int Reverse(int K, int FA, List HeadList,int Rear){ int Temp; int PreAdress; int Kadress; int Re ; // printf("OK"); Kadress = FindAdress(K,FA,HeadList); if (Kadress == FA){ return FA; } Re = Kadress; Temp = HeadList[Kadress].Next; // printf("OK"); while(1){ PreAdress = FindPre(K, FA, HeadList); // printf("OK"); HeadList[Kadress].Next = PreAdress; K--; Kadress = PreAdress; // printf("OK"); if(PreAdress == FA) break; } // printf("OK"); HeadList[FA].Next = Temp; HeadList[Rear].Next = Re; return Re; } void Print(int FA,List HeadList){ while(1){ if(HeadList[FA].Next != -1) printf("%05d %d %05d\n",FA,HeadList[FA].Data,HeadList[FA].Next); else printf("%05d %d %d\n",FA,HeadList[FA].Data,HeadList[FA].Next); FA = HeadList[FA].Next; if(FA == -1) break; } } int FindLength(int FA, List HeadList){ int Count = 1; while(HeadList[FA].Next != -1){ Count++; FA = HeadList[FA].Next; } return Count; }