Home > Back-end >  PAT 1025 inversion list (25 points) using C language implementation, please help to find the problem
PAT 1025 inversion list (25 points) using C language implementation, please help to find the problem

Time:12-26

title:
Given a constant K and a singly linked list L, please write a program to L in each reverse K nodes, such as: given the L for 1-2-3-4-5-6, K is 3, the output should be 3-2-1-6-5 -> 4; If is 4 K, the output should be 4-3-2-1-5-6, last less than K element does not reverse,

Input format:
Each input contains one test case, each test case line 1 give the address of the first node, the total number of positive integer N (10 or less? 0000?? (N) or less), and the positive integer K, which requires inversion of sub chain node number, the address of the node is nonnegative integer 5, with a NULL address? 1, according to
The next N lines, each line format for:
The Address Data Next
The Address is a node Address, the Data is saved the nodes integer Data, Next is the Address of the Next node,

The output format:
Sequence of each test case, output the list after inversion, each node of a line, its format is the same as the input,

Input the sample:
00100 June 4
00000 4 99999
00100 1 12309
68237 6-1
33218 3 00000
99999 5 68237
12309 2 33218

The output sample:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6-1

I C code:
 # include & lt; stdio.h> 

Struct NODE {
Int the address;
The int data;
int next;
} the node [100000], temp;

Int main (void) {
Int init, N, K, I, j, m;
The scanf (" % d % d % d ", & amp; Init, & amp; N, & amp; K);

//the raw data to be included in the structure array
for(i=0; iThe scanf (" % d % d % d ", & amp; The node [I]. Address, & amp; The node [I]. The data, & amp; The node [I] next);
}

//structure array sort: 1. Put the same structure as the init address 0 position
for(i=0; iIf (init==node [I] address) {
Temp=node [0].
The node [0]=node [I];
The node [I]=temp;
break;
}
}

//structure array sort: 2. The rest of the bubble sort
for(i=0; iFor (j=I + 1; JIf (node [I] next==node [j]. Address) {
Temp=node [I + 1);
The node [I + 1)=node [j];
The node [j]=temp;
break;
}
}
}

//K node reversal
For (m=0; MFor (j=m * K; JTemp=node [j];
[j]=node node [(2 * m + 1) * K - j - 1].
Node [(2 * m + 1) * K - j - 1]=temp;
}
}

//print structure array
for(i=0; i05 05 printf (" % d % d % d \ n ", the node [I] address, node [I] data, node [I + 1] address);
}
05 printf (" % d % d - 1 \ n ", the node [I] address, node [I] data);

return 0;
}

The results of the run


The last test point error, but I couldn't find the problem in where, the great god, please help, thank you ~

CodePudding user response:

Singly linked list data structure on data sort http://bbs.csdn.net/topics/392201633

CodePudding user response:

reference 1/f, zhao teacher reply:
to singly linked list data structure for data sorting http://bbs.csdn.net/topics/392201633
thank you very much to teach me how to use singly linked lists sorting method, I also went to see, in addition, this topic I am made of struct array, can help to check the above code, thank you
  • Related