Работа со списками - C (СИ)

Узнай цену своей работы

Формулировка задачи:

Всем привет, прошу помочь мне. Вот такая задача. Работа со списком. Мне нужно работать с двома экземплярами.
Листинг программы
  1. #ifndef LINKED_LIST_H_
  2. #define LINKED_LIST_H_
  3. struct node {
  4. char* data;
  5. int key;
  6. struct node *next;
  7. };
  8. void print_list(struct node* head);
  9. void insert_first(struct node* head, int key, char* data);
  10. _Bool is_empty(struct node* head);
  11. int length(struct node* head);
  12. struct node* find(struct node* head, int key);
  13. struct node* delete_first(struct node* head);
  14. struct node* delete(struct node* head, int key);
  15. void sort(struct node* head);
  16. void reverse(struct node** head_ref);
  17. #endif /* LINKED_LIST_H_ */
Листинг программы
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include "linked_list.h"
  6. struct node* current = NULL;
  7. void print_list(struct node* head) {
  8. struct node *current = head;
  9. printf("\n[ ");
  10. while (current != NULL) {
  11. printf("(%d,%s) ", current->key, current->data);
  12. current = current->next;
  13. }
  14. printf(" ]");
  15. }
  16. void insert_first(struct node* head, int key, char* data) {
  17. struct node *link = (struct node*) malloc(sizeof(struct node));
  18. link->key = key;
  19. link->data = data;
  20. /* point it to old first node */
  21. link->next = head;
  22. /* point first to new first node */
  23. head = link;
  24. }
  25. struct node* delete_first(struct node* head) {
  26. struct node *temp_link = head;
  27. /* mark next to first link as first */
  28. head = head->next;
  29. return temp_link;
  30. }
  31. _Bool is_empty(struct node* head) {
  32. return head == NULL;
  33. }
  34. int length(struct node* head) {
  35. int length = 0;
  36. struct node *current;
  37. for (current = head; current != NULL; current = current->next) {
  38. length++;
  39. }
  40. return length;
  41. }
  42. struct node* find(struct node* head, int key) {
  43. struct node* current = head;
  44. if (head == NULL) {
  45. return NULL;
  46. }
  47. while (current->key != key) {
  48. if (current->next == NULL) {
  49. return NULL;
  50. } else {
  51. current = current->next;
  52. }
  53. }
  54. return current;
  55. }
  56. struct node* delete(struct node* head, int key) {
  57. struct node* current = head;
  58. struct node* previous = NULL;
  59. if (head == NULL) {
  60. return NULL;
  61. }
  62. while (current->key != key) {
  63. if (current->next == NULL) {
  64. return NULL;
  65. } else {
  66. previous = current;
  67. current = current->next;
  68. }
  69. }
  70. if (current == head) {
  71. head = head->next;
  72. } else {
  73. previous->next = current->next;
  74. }
  75. current->next = NULL;
  76. free(current);
  77. current = NULL;
  78. return current;
  79. }
Я создаю два head, но при добавлении узлов, список остается пустой.
Листинг программы
  1. struct node* first_list = NULL;
  2. struct node* second_list = NULL;
  3. insert_first(first_list, 1, "node_1");
  4. insert_first(first_list, 2, "node_2");

Решение задачи: «Работа со списками»

textual
Листинг программы
  1. insert_first(first_list, 1, "node_1");
  2. insert_first(first_list, 2, "node_2");

Объяснение кода листинга программы

  1. В начале кода объявлены три переменные: first_list, value и node_name.
  2. Затем происходит вызов функции insert_first, которая принимает три аргумента: first_list, value и node_name.
  3. В первом вызове функции передаются значения first_list равное 1, value равное 2 и node_name равное node_1.
  4. Во втором вызове функции передаются значения first_list равное 2, value равное 2 и node_name равное node_2.
  5. Функция insert_first вставляет новый элемент в начало списка, используя переданное значение value для определения позиции вставки.
  6. Вставленный элемент содержит значение node_name.
  7. Возвращаемое значение функции не используется в данном коде.

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 4.2 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы