Работа со списками - C (СИ)
Формулировка задачи:
Всем привет, прошу помочь мне. Вот такая задача. Работа со списком. Мне нужно работать с двома экземплярами.
Я создаю два head, но при добавлении узлов, список остается пустой.
#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_
struct node {
char* data;
int key;
struct node *next;
};
void print_list(struct node* head);
void insert_first(struct node* head, int key, char* data);
_Bool is_empty(struct node* head);
int length(struct node* head);
struct node* find(struct node* head, int key);
struct node* delete_first(struct node* head);
struct node* delete(struct node* head, int key);
void sort(struct node* head);
void reverse(struct node** head_ref);
#endif /* LINKED_LIST_H_ */#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "linked_list.h"
struct node* current = NULL;
void print_list(struct node* head) {
struct node *current = head;
printf("\n[ ");
while (current != NULL) {
printf("(%d,%s) ", current->key, current->data);
current = current->next;
}
printf(" ]");
}
void insert_first(struct node* head, int key, char* data) {
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
/* point it to old first node */
link->next = head;
/* point first to new first node */
head = link;
}
struct node* delete_first(struct node* head) {
struct node *temp_link = head;
/* mark next to first link as first */
head = head->next;
return temp_link;
}
_Bool is_empty(struct node* head) {
return head == NULL;
}
int length(struct node* head) {
int length = 0;
struct node *current;
for (current = head; current != NULL; current = current->next) {
length++;
}
return length;
}
struct node* find(struct node* head, int key) {
struct node* current = head;
if (head == NULL) {
return NULL;
}
while (current->key != key) {
if (current->next == NULL) {
return NULL;
} else {
current = current->next;
}
}
return current;
}
struct node* delete(struct node* head, int key) {
struct node* current = head;
struct node* previous = NULL;
if (head == NULL) {
return NULL;
}
while (current->key != key) {
if (current->next == NULL) {
return NULL;
} else {
previous = current;
current = current->next;
}
}
if (current == head) {
head = head->next;
} else {
previous->next = current->next;
}
current->next = NULL;
free(current);
current = NULL;
return current;
}struct node* first_list = NULL; struct node* second_list = NULL; insert_first(first_list, 1, "node_1"); insert_first(first_list, 2, "node_2");
Решение задачи: «Работа со списками»
textual
Листинг программы
insert_first(first_list, 1, "node_1"); insert_first(first_list, 2, "node_2");
Объяснение кода листинга программы
- В начале кода объявлены три переменные:
first_list,valueиnode_name. - Затем происходит вызов функции
insert_first, которая принимает три аргумента:first_list,valueиnode_name. - В первом вызове функции передаются значения
first_listравное 1,valueравное 2 иnode_nameравноеnode_1. - Во втором вызове функции передаются значения
first_listравное 2,valueравное 2 иnode_nameравноеnode_2. - Функция
insert_firstвставляет новый элемент в начало списка, используя переданное значениеvalueдля определения позиции вставки. - Вставленный элемент содержит значение
node_name. - Возвращаемое значение функции не используется в данном коде.