Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node {
- int value; //значение
- struct node *next; //ссылка на след элемент списка
- } linked_list;
- void print(linked_list *head_node) {
- linked_list *current = head_node;
- while (current != NULL) { //итерация вплоть до конца
- printf("%d\n", current->value); //вывел значение
- current = current->next; //сменил указатель
- }
- }
- void add(linked_list *head_node,
- int value) { //пихает значение value в конец списка
- linked_list *current = head_node;
- while (current->next != NULL) { //итерация до конечного элемента
- current = current->next;
- }
- current->next = malloc(sizeof(linked_list)); //создали новую оюласть памяти для ноды
- current->next->value = value; //инициализации
- current->next->next = NULL;
- }
- void combine(linked_list *head_node_to, linked_list *head_node_from) {
- linked_list *current = head_node_from;
- while (current != NULL) { //итерация вплоть до конца
- add(head_node_to, current->value); //вписал значение
- current = current->next; //сменил указатель
- }
- }
- int main() {
- linked_list *head_node = NULL;
- head_node = malloc(sizeof(linked_list));
- head_node->value = 123;
- head_node->next = malloc(sizeof(linked_list));
- head_node->next->value = 234;
- linked_list *head_node2 = NULL;
- head_node = malloc(sizeof(linked_list));
- head_node->value = 456;
- head_node->next = malloc(sizeof(linked_list));
- head_node->next->value = 789;
- combine(head_node, head_node2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement