fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Node {
  5. int value;
  6.  
  7. struct Node *prev;
  8. struct Node *next;
  9. } Node;
  10.  
  11. typedef struct {
  12. Node *head;
  13. Node *tail;
  14. } List;
  15.  
  16.  
  17. // Создание нового узла
  18. Node *create_node(int value)
  19. {
  20. Node *node = malloc(sizeof(Node));
  21.  
  22. if (node == NULL) {
  23. return NULL;
  24. }
  25.  
  26. node->value = value;
  27. node->prev = NULL;
  28. node->next = NULL;
  29.  
  30. return node;
  31. }
  32.  
  33.  
  34. // Добавление в конец
  35. void push_back(List *list, int value)
  36. {
  37. Node *node = create_node(value);
  38.  
  39. if (node == NULL) {
  40. return;
  41. }
  42.  
  43. if (list->tail == NULL) {
  44. // Список пуст
  45. list->head = node;
  46. list->tail = node;
  47. return;
  48. }
  49.  
  50. node->prev = list->tail;
  51. list->tail->next = node;
  52. list->tail = node;
  53. }
  54.  
  55.  
  56. // Вывод слева направо
  57. void print_forward(const List *list)
  58. {
  59. Node *current = list->head;
  60.  
  61. while (current != NULL) {
  62. printf("%d ", current->value);
  63. current = current->next;
  64. }
  65.  
  66. printf("\n");
  67. }
  68.  
  69.  
  70. // Вывод справа налево
  71. void print_backward(const List *list)
  72. {
  73. Node *current = list->tail;
  74.  
  75. while (current != NULL) {
  76. printf("%d ", current->value);
  77. current = current->prev;
  78. }
  79.  
  80. printf("\n");
  81. }
  82.  
  83. // Удаление узла
  84. void remove_node(List *list, Node *node)
  85. {
  86. if (node == NULL) {
  87. return;
  88. }
  89.  
  90. // Если есть предыдущий узел,
  91. // связываем его со следующим.
  92. if (node->prev != NULL) {
  93. node->prev->next = node->next;
  94. } else {
  95. // Удаляется head
  96. list->head = node->next;
  97. }
  98.  
  99. // Если есть следующий узел,
  100. // связываем его с предыдущим.
  101. if (node->next != NULL) {
  102. node->next->prev = node->prev;
  103. } else {
  104. // Удаляется tail
  105. list->tail = node->prev;
  106. }
  107.  
  108. free(node);
  109. }
  110.  
  111. // Поиск узла
  112. Node *find_node(const List *list, int value)
  113. {
  114. Node *current = list->head;
  115.  
  116. while (current != NULL) {
  117. if (current->value == value) {
  118. return current;
  119. }
  120.  
  121. current = current->next;
  122. }
  123.  
  124. return NULL;
  125. }
  126.  
  127. // Освобождение списка
  128. void free_list(List *list)
  129. {
  130. Node *current = list->head;
  131.  
  132. while (current != NULL) {
  133. Node *next = current->next;
  134. free(current);
  135. current = next;
  136. }
  137.  
  138. list->head = NULL;
  139. list->tail = NULL;
  140. }
  141.  
  142.  
  143. int main(void)
  144. {
  145. List list = {
  146. .head = NULL,
  147. .tail = NULL
  148. };
  149.  
  150. push_back(&list, 10);
  151. push_back(&list, 20);
  152. push_back(&list, 30);
  153. push_back(&list, 40);
  154.  
  155. printf("Forward: ");
  156. print_forward(&list);
  157.  
  158. // Удаление узла из середины
  159. // находим узел 30
  160. Node *node = find_node(&list, 30);
  161. // удаляем его
  162. remove_node(&list, node);
  163.  
  164. printf("Backward: ");
  165. print_backward(&list);
  166.  
  167. free_list(&list);
  168.  
  169. return 0;
  170. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Forward:  10 20 30 40 
Backward: 40 20 10