fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct Node {
  4. int t;
  5. struct Node *n;
  6. } N;
  7.  
  8. void plist(N* a) {
  9. while (a) {
  10. printf("%d ", a->t);
  11. a = a->n;
  12. }
  13. printf("\n");
  14. }
  15.  
  16.  
  17. N* rev(N* a) {
  18. if (a->n == NULL) return a;
  19. N* head = rev(a->n);
  20. a->n->n = a;
  21. return head;
  22. }
  23.  
  24. N* reverse(N* a) {
  25. N* head = rev(a);
  26. a->n = NULL;
  27. return head;
  28. }
  29.  
  30. int main(void) {
  31. N *a = malloc(sizeof(N));
  32. N *b = malloc(sizeof(N));
  33. N *c = malloc(sizeof(N));
  34. N *d = malloc(sizeof(N));
  35. N *e = malloc(sizeof(N));
  36. a->t = 1; a->n = b;
  37. b->t = 2; b->n = c;
  38. c->t = 3; c->n = d;
  39. d->t = 4; d->n = e;
  40. e->t = 5; e->n = NULL;
  41. plist(a);
  42. N* x = reverse(a);
  43. plist(x);
  44. free(a);
  45. free(b);
  46. free(c);
  47. free(d);
  48. free(e);
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 2244KB
stdin
Standard input is empty
stdout
1 2 3 4 5 
5 4 3 2 1