#include <stdio.h>
struct node {int elem;struct node *next;};
typedef struct node *list;
list midcell(list L) {
list m;
for (m = L; L && L->next; L = L->next->next) m = m->next;
return m;
}
list revfR_sub(list L, list M, list N) {
if (L == M) return N;
list r = revfR_sub(L->next, M, L);
L->next = N;
return r;
}
list revfR(list L, list M) {
return revfR_sub(L, M, NULL);
}
list revf(list L, list M) {
list p, n;
for (p = NULL; L != M;) {
n = L->next;
L->next = p;
p = L;
L = n;
}
return p;
}
struct node nodes[10];
list init() {
int i;
for (i = 0; i < 10; ++i) nodes[i].elem = i;
for (i = 0; i < 9; ++i) nodes[i].next = &nodes[i+1];
nodes[9].next = NULL;
return &nodes[0];
}
void print(char* s, list p) {
for (; p
; p
= p
->next
) printf("%d ", p
->elem
); }
int main () {
list m;
print("init ", init());
print("midcell ", m = midcell(init()));
print("revfR ", revfR(init(), m));
print("revfR(M)", m);
print("revf ", revf(init(), m));
print("revf(M) ", m);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiAKc3RydWN0IG5vZGUge2ludCBlbGVtO3N0cnVjdCBub2RlICpuZXh0O307CnR5cGVkZWYgc3RydWN0IG5vZGUgKmxpc3Q7CgpsaXN0IG1pZGNlbGwobGlzdCBMKSB7CglsaXN0IG07Cglmb3IgKG0gPSBMOyBMICYmIEwtPm5leHQ7IEwgPSBMLT5uZXh0LT5uZXh0KSBtID0gbS0+bmV4dDsKCXJldHVybiBtOwp9CgpsaXN0IHJldmZSX3N1YihsaXN0IEwsIGxpc3QgTSwgbGlzdCBOKSB7CglpZiAoTCA9PSBNKSByZXR1cm4gTjsKCWxpc3QgciA9IHJldmZSX3N1YihMLT5uZXh0LCBNLCBMKTsKCUwtPm5leHQgPSBOOwoJcmV0dXJuIHI7Cn0KCmxpc3QgcmV2ZlIobGlzdCBMLCBsaXN0IE0pIHsKCXJldHVybiByZXZmUl9zdWIoTCwgTSwgTlVMTCk7Cn0KCmxpc3QgcmV2ZihsaXN0IEwsIGxpc3QgTSkgewoJbGlzd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