-
Notifications
You must be signed in to change notification settings - Fork 0
/
1025. 反转链表 (25).c
64 lines (59 loc) · 1.11 KB
/
1025. 反转链表 (25).c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
struct node{
int Data;
int Next;
}a[100005];
int main()
{
int p,num,k;
int address,data,next,flag,i;
scanf("%d %d %d",&p,&num,&k);
while(num--){
scanf("%d %d %d",&address,&data,&next);
a[address].Data=data;
a[address].Next=next;
}
int t=p;
num=0;
for(i=0;t!=-1;i++){
num++;
t=a[t].Next;
}
int maxn=num/k;
if(maxn*k==num) flag=1;
else flag=0;
int tmp,_new,_old,new_p,tmp_begin,tmp_end=p;
int cnt=1;
int n0=0;
while(n0<maxn){
cnt=1;
_new=p;
_old=a[p].Next;
while(cnt<k){
cnt++;
tmp=a[_old].Next;
a[_old].Next=_new;
_new=_old;
_old=tmp;
}
a[p].Next=_old;
tmp_begin=_new;
if(n0!=0){
a[tmp_end].Next=tmp_begin;
tmp_end=p;
}
else new_p=tmp_begin;
n0++;
p=_old;
}
if(flag==1) a[tmp_end].Next=-1;
t=new_p;
for(i=0;t!=-1;i++){
if(a[t].Next!=-1)
printf("%05d %d %05d\n",t,a[t].Data,a[t].Next);
else
printf("%05d %d -1\n",t,a[t].Data);
t=a[t].Next;
}
return 0;
}