Given a constant k and a singly linked list L, you is 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 the one test case. The first line contains the address of the first node, a positive N (<=) which are the total number O F nodes, and a positive K (<=n) which are 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
The where Address is the position of the node, Data are 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 was 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
1#include <stdio.h>2 #defineMAX 1000043 4typedefstructList {5 intData;6 intAddr;7 intnextaddr;8 structList *Next;9 }ilist;Ten One voidPrintlist (IList *a); AIList *listreversing (IList *p,intK); - - intMain () the { - intfirstaddr; - inti; - intK//anti-rotor chain length k + intN//The total number of nodes - intNum;//number of nodes after the linked list is built + intData[max]; A intNextaddress[max]; at inttemp; - -scanf" %d%d%d",&firstaddr,&n,&K); - -IList a[n+1]; - ina[0]. NEXTADDR =firstaddr; - to for(i =0; I<n; i++) + { -scanf"%d",&temp); thescanf"%d%d",&data[temp],&nextaddress[temp]); * } $ Panax Notoginsengi =1; - while(1) the { + if(a[i-1]. Nextaddr = =-1 ) A { thea[i-1]. Next =NULL; +Num = i1; - Break; $ } $ -A[i]. ADDR = a[i-1]. nextaddr; -A[i]. Data =Data[a[i]. ADDR]; theA[i]. NEXTADDR =Nextaddress[a[i]. ADDR]; -a[i-1]. Next = A +i;Wuyi thei++; - } Wu -IList *p = A;//p points to the node of the linked table head AboutIList *RP = NULL;//reverse the return value of a linked list function $ - if(K <=Num); - { - for(i =0; i < (num/k); i++) A { +RP =listreversing (p,k); theP-Next =RP; -P-nextaddr = RP-Addr; $ the intj =0; the while(J <K) the { thep = p->Next; -J + +; in } the } the } About the Printlist (a); the return 0; the } + -ilist* listreversing (IList *p,intK) the {Bayi intCount =1; theIList *New= P-Next; theIList *old =New-Next; -IList *temp =NULL; - while(Count <K) the { theTEMP = oldNext; theOld->next =New; theOld->nextaddr =New-Addr; - New=Old ; theOld =temp; thecount++; the }94P->next->next =Old ; the the if(Old! =NULL) the {98P->next->nextaddr = oldAddr; About - }101 Else102 {103P->next->nextaddr =-1;104 } the return New;106 }107 voidPrintlist (IList *a)108 {109IList *p =A; the while(P-Next! =NULL) {111p = PNext; the if(P->nextaddr! =-1 ){113 //format output,%.5 means that if an integer is less than 5 bits, the output is preceded by 0 such as: 22, output: 00022 theprintf"%.5d%d%.5d\n", P->addr, P->data, p->nextaddr); the}Else{ the //-1 does not need to be output in%.5 format117printf"%.5d%d%d\n", P->addr, P->data, p->nextaddr);118 }119 } -}
listreversing function not only to exchange pointers, but also to exchange addr.
There are 7 Test points: L represents the number of nodes in a single linked list, because the nodes that make up a single linked list are not necessarily in the N nodes of the input, i.e.:l<=n;
Case 0:l = N has node Address = 99999
Case 1:l = MK, L = N, (m = 2,3, 4,...) has node Address = 99999
Case 2:k = n, L = n has node address= 99999
Case 3:k = 1, L = MK has node address = 99999
Case 4:k = 1, L = N = 1 (very simple test point)
Case 5:k! = 1, L% K = (K-1) (lots of nodes, if the complexity of the list is O (n*n), the likelihood of timeouts is great)
Case 6:l > N (with redundant nodes) has node address = 99999
To consider the details: K=1 does not reverse, k=l full inversion, l%k = = 0, each segment is reversed, l%k = (K-1), redundant nodes do not reverse. l< N, the case of redundant nodes.
Thanks
Pat02-1reversing LinkedList (25) reverse order of single-linked list
Two methods of single-linked list inversion/reverse order
PAT 02-Linear structure 1. Reversing Linked List