fork download
  1. #include <stdio.h>
  2.  
  3. struct node {int elem;struct node *next;};
  4. typedef struct node* List;
  5.  
  6. List f4_1(List L)
  7. {
  8. List p; int i;
  9. for (p = L, i = 0; p; i++, p = p->next);
  10. for (p = L, i /= 2; i; i--, p = p->next);
  11. return p;
  12. }
  13. List f4_2(List L, List M)
  14. {
  15. List end = M;
  16. if (L) {
  17. for (; L->next != M; L = L->next);
  18. L->next = NULL;
  19. }
  20. if (M->next) {
  21. end = f4_2(NULL, M->next);
  22. M->next->next = M;
  23. M->next = NULL;
  24. }
  25. return end;
  26. }
  27. List f4_3(List L, List M)
  28. {
  29. List p, S[100];
  30. int i = 0, j;
  31.  
  32. for (; L->next != M; L = L->next);
  33. L->next = NULL;
  34.  
  35. for (; M->next; S[i++] = M, M = M->next);
  36.  
  37. for (p = M; i; p->next = S[--i], p = p->next);
  38. p->next = NULL;
  39. return M;
  40. }
  41.  
  42. void show_list(char *msg, List L)
  43. {
  44. printf(msg);
  45. printf("[ ");
  46. for (; L; L = L->next)
  47. printf("%d ", L->elem);
  48. printf("]\n");
  49. }
  50. void init_list(struct node nodes[], int len)
  51. {
  52. int i;
  53. for (i = 0; i < len; i++) {
  54. nodes[i].elem = i;
  55. nodes[i].next = (List)NULL;
  56. }
  57. for (i = 0; i < len-1; i++) {
  58. nodes[i].next = &nodes[i+1];
  59. }
  60. }
  61.  
  62. int main(void)
  63. {
  64. struct node nodes[10];
  65.  
  66. init_list(nodes, sizeof(nodes)/sizeof(nodes[0]));
  67. show_list("L = ", nodes);
  68.  
  69. printf("Q1\n");
  70. show_list("L = ", f4_1(nodes));
  71.  
  72. printf("Q2\n");
  73. show_list("L = ", nodes);
  74. show_list("M = ", f4_2(nodes, &nodes[4]));
  75. show_list("L = ", nodes);
  76.  
  77. printf("Q3\n");
  78. init_list(nodes, sizeof(nodes)/sizeof(nodes[0]));
  79. show_list("L = ", nodes);
  80. show_list("M = ", f4_3(nodes, &nodes[4]));
  81. show_list("L = ", nodes);
  82. }
  83.  
  84.  
Runtime error #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
L = [ 0 1 2 3 4 5 6 7 8 9 ]
Q1
L = [ 5 6 7 8 9 ]
Q2
L = [ 0 1 2 3 4 5 6 7 8 9 ]
M = [ 9 8 7 6 5 4 ]
L = [ 0 1 2 3 ]
Q3
L = [ 0 1 2 3 4 5 6 7 8 9 ]
M = [ 9 8 7 6 5 4 ]
L = [ 0 1 2 3 ]