fork download
  1. #include <stdio.h>
  2.  
  3. struct node {int elem;struct node *next;};
  4. typedef struct node *list;
  5.  
  6. list midcell(list L) {
  7. list m;
  8. for (m = L; L && L->next; L = L->next->next) m = m->next;
  9. return m;
  10. }
  11.  
  12. list revfR_sub(list L, list M, list N) {
  13. if (L == M) return N;
  14. list r = revfR_sub(L->next, M, L);
  15. L->next = N;
  16. return r;
  17. }
  18.  
  19. list revfR(list L, list M) {
  20. return revfR_sub(L, M, NULL);
  21. }
  22.  
  23. list revf(list L, list M) {
  24. list p, n;
  25. for (p = NULL; L != M;) {
  26. n = L->next;
  27. L->next = p;
  28. p = L;
  29. L = n;
  30. }
  31. return p;
  32. }
  33.  
  34. struct node nodes[10];
  35.  
  36. list init() {
  37. int i;
  38. for (i = 0; i < 10; ++i) nodes[i].elem = i;
  39. for (i = 0; i < 9; ++i) nodes[i].next = &nodes[i+1];
  40. nodes[9].next = NULL;
  41. return &nodes[0];
  42. }
  43.  
  44. void print(char* s, list p) {
  45. printf("%s: ", s);
  46. for (; p; p = p->next) printf("%d ", p->elem);
  47. printf("\n");
  48. }
  49.  
  50. int main () {
  51. list m;
  52. print("init ", init());
  53. print("midcell ", m = midcell(init()));
  54. print("revfR ", revfR(init(), m));
  55. print("revfR(M)", m);
  56. print("revf ", revf(init(), m));
  57. print("revf(M) ", m);
  58. return 0;
  59. }
  60.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
init    : 0 1 2 3 4 5 6 7 8 9 
midcell : 5 6 7 8 9 
revfR   : 4 3 2 1 0 
revfR(M): 5 6 7 8 9 
revf    : 4 3 2 1 0 
revf(M) : 5 6 7 8 9