正常输入题目给的例子也能输出和答案一样的output,那不给分是哪里有问题呢?
题目
02-线性结构3 Reversing Linked List
分数 25
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 (≤10
5
) 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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node *PtrToNode;
struct Node{
char vorads[5];
int data;
char nachads[5];
PtrToNode Next;
};
typedef PtrToNode List;
List order(List L, char X[5]);
List sort(List L, int K);
void print(List L);
int main()
{
List L, LL, r;
PtrToNode Rear;
int N, K;
char X[5];
L = (List)malloc(sizeof(struct Node));
L->Next = NULL;
Rear = L;
scanf("%s", X);//起始地址
scanf("%d", &N);//Node个数
scanf("%d", &K);//RE节点
int i; //读入并建立乱序表
for( i = 0; i < N; i++ ){
r = (List)malloc(sizeof(struct Node));
scanf("%s %d %s", (r->vorads), &(r->data), (r->nachads));
Rear->Next = r;
Rear = r;
}
Rear->Next = NULL;
L = order(L, X); //排正序
LL = sort(L, K); //排RE序
print(LL);
return 0;
}
List order(List L, char X[5])
{
List L1;
L1 = (List)malloc(sizeof(struct Node));
PtrToNode front, Rear, P, tmp;
P = L1;
for( Rear = L; Rear && strcmp((Rear->Next->vorads), X); Rear = Rear->Next );
if(!Rear) return NULL;
tmp = Rear->Next;
Rear->Next = tmp->Next;
P->Next = tmp;
front = tmp;
P = P->Next;
while( strcmp((front->nachads), "-1") ){
for( Rear = L; Rear && strcmp((Rear->Next->vorads), (front->nachads)); Rear = Rear->Next );
if(!Rear) return NULL;
tmp = Rear->Next;
Rear->Next = tmp->Next;
P->Next = tmp;
front = tmp;
P = P->Next;
}
P->Next = NULL;
return L1->Next;
}
List sort(List L, int K)
{
PtrToNode Rear, tag;
List head = L;
Rear = L;
while( Rear->Next && (Rear->Next->data) <= K ){
tag = Rear->Next;
Rear->Next = tag->Next;
tag->Next = head;
strcpy(tag->nachads, head->vorads);
head = tag; //更新head结点
}
strcpy(Rear->nachads, Rear->Next->vorads);
if( !(Rear->Next) ) return NULL;
return head;
}
void print(List L)
{
PtrToNode Rear;
if( L ){
for( Rear = L; Rear; Rear = Rear->Next ){
printf("%s %d %s", Rear->vorads, Rear->data, Rear->nachads);
if( Rear->Next ) printf("\n");
}
}
else{
printf("-1\n");
}
return;
}