fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Node {
  5. int value;
  6. struct Node *next;
  7. } Node;
  8.  
  9. int main(void)
  10. {
  11. Node *head = malloc(sizeof(Node));
  12. Node *second = malloc(sizeof(Node));
  13. Node *third = malloc(sizeof(Node));
  14.  
  15. head->value = 10;
  16. head->next = second;
  17.  
  18. second->value = 20;
  19. second->next = third;
  20.  
  21. third->value = 30;
  22. third->next = NULL;
  23.  
  24.  
  25. // Создаём новый узел
  26. Node *new_node = malloc(sizeof(Node));
  27. new_node->value = 25;
  28. // Вставляем его между second и third
  29. new_node->next = second->next;
  30. second->next = new_node;
  31.  
  32.  
  33. // Проходим по списку
  34. Node *current = head;
  35.  
  36. while (current != NULL) {
  37. printf("%d\n", current->value);
  38. current = current->next;
  39. }
  40.  
  41. // Освобождаем память
  42. free(third);
  43. free(second);
  44. free(head);
  45.  
  46. return 0;
  47. }
  48.  
  49.  
  50.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
10
20
25
30