1 #include <stdio.h>
2 #include <malloc.h>
3
4 typedef struct _node
5 {
6 int data;
7 int address;
8 int back;
9 int next;
10 // int flag;
11 } NODE;
12 NODE* List[100001]={0};
13 int main(void)
14 {
15 int FirstAddr,TotalNode,NumberToReverse,i;
16 int AddrTemp,DataTemp,NextAddrTemp;
17 int Loop,Mod;
18 int count=0;
19 int Boundary;
20 NODE* LinkFix=NULL;
21 NODE* EachNode=NULL;
22 scanf("%d %d %d",&FirstAddr,&TotalNode,&NumberToReverse);
23
24 int* AddrInOrder=(int *)calloc(TotalNode+1,sizeof(int));
25 int* LinkReverse=(int *)calloc(TotalNode+1,sizeof(int));
26 for (i=0; i<TotalNode; i++)
27 {
28 scanf("%d %d %d",&AddrTemp,&DataTemp,&NextAddrTemp);
29 EachNode=(NODE*)calloc(1,sizeof(NODE));
30 *(List+AddrTemp) = EachNode;
31 EachNode -> address = AddrTemp;
32 EachNode -> data = DataTemp;
33 EachNode -> next = NextAddrTemp;
34 }
35
36 i=0;
37 LinkFix = *(List+FirstAddr);
38 while (1)
39 {
40 *(AddrInOrder+i) = LinkFix -> address;
41 if (LinkFix -> next == -1) break;
42 // (*(List+(LinkFix->next)))->back = LinkFix -> address;
43 LinkFix = *(List+(LinkFix -> next));
44 i++;
45 }
46 TotalNode=i+1;
47 // printf("\n");
48 // for (i=0; i<TotalNode; i++) printf("%05d\n",*(AddrInOrder+i));
49 Mod = TotalNode % NumberToReverse;
50 // Multily = TotalNode / NumberToReverse;
51 Loop=NumberToReverse;
52 if (NumberToReverse != 1)
53 {
54 for (i=0;i<=TotalNode-Mod-1;)
55 {
56 if ( count == Loop )
57 {
58 count=0;
59 i=Loop+i;
60 }
61 if (i >= TotalNode-Mod-1) break;
62 *(LinkReverse+i+count) = *(AddrInOrder+i+(Loop-count-1));
63 count++;
64 }
65 Boundary=i;
66 }
67 else for (i=0;i<=TotalNode-1;i++) *(LinkReverse+i) = *(AddrInOrder+i);
68
69
70
71 if (Mod)
72 for (i=Boundary; i<TotalNode; i++)
73 {
74 *(LinkReverse+i)=*(AddrInOrder+i);
75 if ( i == TotalNode-1 ) (*(List+*(LinkReverse+i)))->next = -1;
76
77 }
78
79 else
80 {
81 // for (i= TotalNode/2 -1 ; i<TotalNode; i++)
82 if (TotalNode == 1) ;
83 else
84 {
85 (*(List+*(LinkReverse+TotalNode-Loop)))->next = (*(List+*(LinkReverse+TotalNode-Loop+1)))->address;
86 (*(List+*(LinkReverse+TotalNode-1)))->next = -1;
87 (*(List+*(LinkReverse)))->next = (*(List+*(LinkReverse+1)))->address;
88 }
89 }
90
91 // printf("\n");
92 // for (k=0; k<TotalNode; k++) printf("%05d\n",*(LinkReverse+k));
93
94 if (TotalNode>1)
95 for (i=0; i<TotalNode; i++)
96 {
97 if ((*(List+*(LinkReverse+i)))->next == -1)
98 {
99 printf("%.5d %d -1\n",*(LinkReverse+i),(*(List+*(LinkReverse+i)))->data);
100 }
101 else
102 {
103 printf("%.5d %d %.5d\n",*(LinkReverse+i),(*(List+*(LinkReverse+i)))->data,(*(List+*(LinkReverse+i+1)))->address);
104 }
105
106 }
107 else printf("%.5d %d -1\n",*(LinkReverse),(*(List+*(LinkReverse)))->data);
108
109
110 // free(AddrInOrder);
111 // free(LinkReverse);
112 return 0;
113
114 }