Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node {
  5.     int value; //значение
  6.     struct node *next; //ссылка на след элемент списка
  7. } linked_list;
  8.  
  9. void print(linked_list *head_node) {
  10.         linked_list *current = head_node;
  11.  
  12.         while (current != NULL) { //итерация вплоть до конца
  13.                 printf("%d\n", current->value); //вывел значение
  14.                 current = current->next; //сменил указатель
  15.         }
  16. }
  17.  
  18. void add(linked_list *head_node,
  19.          int value) { //пихает значение value в конец списка
  20.         linked_list *current = head_node;
  21.         while (current->next != NULL) { //итерация до конечного элемента
  22.                 current = current->next;
  23.         }
  24.  
  25.         current->next = malloc(sizeof(linked_list)); //создали новую оюласть памяти для ноды
  26.         current->next->value = value; //инициализации
  27.         current->next->next = NULL;
  28. }
  29.  
  30. void combine(linked_list *head_node_to, linked_list *head_node_from) {
  31.         linked_list *current = head_node_from;
  32.  
  33.         while (current != NULL) { //итерация вплоть до конца
  34.                 add(head_node_to, current->value); //вписал значение
  35.                 current = current->next; //сменил указатель
  36.         }
  37. }
  38.  
  39.  
  40. int main() {
  41.         linked_list *head_node = NULL;
  42.         head_node = malloc(sizeof(linked_list));
  43.         head_node->value = 123;
  44.         head_node->next = malloc(sizeof(linked_list));
  45.         head_node->next->value = 234;
  46.  
  47.         linked_list *head_node2 = NULL;
  48.         head_node = malloc(sizeof(linked_list));
  49.         head_node->value = 456;
  50.         head_node->next = malloc(sizeof(linked_list));
  51.         head_node->next->value = 789;
  52.  
  53.         combine(head_node, head_node2);
  54.         return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement