fork download
  1. /** http://h...content-available-to-author-only...e.ru/questions/378459/c-%D0%BD%D0%B5-%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%B0%D0%B5%D1%82-%D1%84%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D1%8F-%D0%BA%D0%BE%D0%BF%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F-%D1%81%D0%BF%D0%B8%D1%81%D0%BA%D0%B0
  2.  */
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h> /* malloc */
  6.  
  7. #define PRINT(M) do { \
  8.   fputs(#M " ", stdout); \
  9.   List_print(M); \
  10.   } while (0)
  11.  
  12. typedef char ListCargo;
  13. typedef struct node {
  14. ListCargo cargo;
  15. struct node* next;
  16. } *List;
  17.  
  18. static List
  19. List_new(ListCargo cargo) {
  20. List head = malloc(sizeof *head);
  21. if (!head) { /* fatal error for the example */
  22. fputs("malloc", stderr);
  23. exit(EXIT_FAILURE);
  24. }
  25. head->cargo = cargo;
  26. head->next = NULL;
  27. return head;
  28. }
  29.  
  30. static void
  31. List_print(List head) {
  32. for ( ; head; head = head->next)
  33. putchar(head->cargo);
  34. putchar('\n');
  35. }
  36.  
  37. static List
  38. List_insert_after(List head, ListCargo cargo) {
  39. List node = List_new(cargo);
  40. if (head) {
  41. node->next = head->next;
  42. head->next = node;
  43. }
  44. return node;
  45. }
  46.  
  47. static List
  48. List_copy0(List head) {
  49. if (!head)
  50. return NULL; /* empty list */
  51. else {
  52. List copy = List_insert_after(NULL, head->cargo), tail = copy;
  53. for (head = head->next; head; head = head->next)
  54. tail = List_insert_after(tail, head->cargo);
  55. return copy;
  56. }
  57. }
  58.  
  59.  
  60. static List
  61. List_push_front(List head, ListCargo cargo) {
  62. List node = List_new(cargo);
  63. node->next = head;
  64. return node;
  65. }
  66.  
  67. static List
  68. List_reverse_inplace(List head) {
  69. List node, tail = NULL;
  70. while (head) {
  71. node = head;
  72. head = head->next; /* pop head node */
  73. node->next = tail; /* prepend it to tail */
  74. tail = node;
  75. }
  76. return tail;
  77. }
  78.  
  79. static List
  80. List_copy(List head, List copy) {
  81. return head ? List_copy(head->next, List_push_front(copy, head->cargo)) :
  82. List_reverse_inplace(copy);
  83. }
  84.  
  85. static List
  86. List_from_string(const char *s) {
  87. List head = NULL;
  88. assert(s);
  89. for ( ; *s; ++s)
  90. head = List_push_front(head, *s);
  91. return List_reverse_inplace(head);
  92. }
  93.  
  94. int main(void) {
  95. List M, M0, M1, M2;
  96. M = List_from_string("abcde");
  97. PRINT(M);
  98. M0 = List_copy0(M);
  99. PRINT(M);
  100. PRINT(M0);
  101. M1 = List_copy(M, NULL);
  102. PRINT(M);
  103. PRINT(M1);
  104. M2 = List_reverse_inplace(M);
  105. PRINT(M); /* *_inplace changes M */
  106. PRINT(M1);
  107. PRINT(M2);
  108. return 0;
  109. }
Success #stdin #stdout 0s 2380KB
stdin
Standard input is empty
stdout
M abcde
M abcde
M0 abcde
M abcde
M1 abcde
M a
M1 abcde
M2 edcba