Topic:
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
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 was 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
Analysis: mainly examine the reverse order of the list. If you sort through an array and then output every k in reverse order, you will not be able to pass the test for the redundant nodes, so the implementation must use the linked list.
Code:
typedefstructReversenode {Longaddress; intvalue; LongNextadd; structReversenode *Next; } Reversenode; Reversenode*reverselink (Reversenode *head,intReverselen) {Reversenode*New= head->Next; Reversenode*old =New-Next; intCount =1; while(Count <Reverselen) {Reversenode*temp = old->Next; old->next =New; old->nextadd =New-address; New=Old ; old=temp; Count++; } head->next->next =Old ; if(!Old ) {Head->next->nextadd =-1; } Else{Head->next->nextadd = old->address; } Reversenode*temp = head->Next; Head->next =New; Head->nextadd =New-address; returntemp;}intMain () {//Read Input Longbeginaddress; intNumber , Reverselen; scanf ("%ld%d%d", &beginaddress, &number, &Reverselen); Reversenode*head = (Reversenode *)malloc(sizeof(Reversenode)); Reversenode*A[number]; for(inti =0; I < number; i++) { Longaddress, Nextadd; intvalue; scanf ("%ld%d%ld", &address, &value, &Nextadd); Reversenode*node = (Reversenode *)malloc(sizeof(Reversenode)); Node->address =address; Node->value =value; Node->nextadd =Nextadd; Node->next =0; A[i]=node; if(Beginaddress = =address) {Head->next =node; } } //connecting the input data through a linked listReversenode *temp = head->Next; intActualnumber =1; intRecycount =Number ; while(Temp->nextadd! =-1&& recycount-->0) { for(inti =0; I < number; i++) {Reversenode*tempnode =A[i]; if(Tempnode->address = = temp->Nextadd) {Temp->next =Tempnode; Temp->nextadd = tempnode->address; Temp= temp->Next; Actualnumber++; Break; } } } //reversal if(Reverselen >1) { intReversecount = Actualnumber/reverselen;//number of times a reversal is requiredReversenode *temphead =Head; while(reversecount-->0) {Temphead=Reverselink (Temphead, Reverselen); }} Reversenode*ptr = head->Next; while(PTR) {if(Ptr->nextadd = =-1) {printf ("%.5ld%d-1\n", Ptr->address, ptr->value); } Else{printf ("%.5ld%d%.5ld\n", Ptr->address, Ptr->value, ptr->Nextadd); } ptr= ptr->Next; }}
Operation Result:
[Test point 5, is the use of super-large amounts of data, because I am connecting the input data through the linked list of the step is a nested loop, the complexity O (n^2), may be caused by this, indeterminate. But the overall reversal mentality is this, and later found the reason for the update]
PAT002 Reversing Linked List