鱼C论坛

 找回密码
 立即注册
查看: 1604|回复: 3

求大神帮忙编程完成合并两循环单链表

[复制链接]
发表于 2022-3-21 23:28:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用c语言
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-22 03:26:19 | 显示全部楼层
估计你要的是单向循环链表,但是因为我不认真看题,我写完了双向循环链表后才发现这个问题
不管怎么说,双向循环链表也是循环链表
参考下面的这个代码自己完成单向循环链表吧

list.h
  1. #ifndef _LIST_H_
  2. #define _LIST_H_

  3. #include <stddef.h>
  4. #include <stdbool.h>

  5. typedef struct list_node_tag {
  6.     struct list_node_tag *prev;
  7.     struct list_node_tag *next;
  8.     void *data;
  9.     size_t size;
  10. } list_node_t;

  11. typedef struct {
  12.     list_node_t *node;
  13.     size_t size;
  14. } list_t;

  15. list_t *list_init(void);
  16. void list_deinit(list_t *list);
  17. void list_clean(list_t *list);
  18. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  19. bool list_delete(list_t *list, size_t index);
  20. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  21. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  22. bool list_append(list_t *list, const void *data, size_t size);
  23. bool list_empty(const list_t *list);
  24. size_t list_size(const list_t *list);

  25. list_t *list_union(const list_t *a, const list_t *b);

  26. #endif
复制代码


list.c
  1. #include "list.h"
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define GET_NODE(list, index) ({ \
  5.         const list_t *this_list = list; \
  6.         size_t this_index = (index) + 1; \
  7.         list_node_t *node = this_list->node; \
  8.         while(this_index--) node = node->next; \
  9.         node; \
  10.     })

  11. static list_node_t *new_node(size_t size) {
  12.     list_node_t *node = malloc(sizeof(*node));
  13.     if(!node) return node;
  14.     node->data = malloc(size);
  15.     if(size && !node->data) {free(node); return NULL;}
  16.     node->size = size;
  17.     node->next = node;
  18.     node->prev = node;
  19.     return node;
  20. }

  21. static void delete_node(list_node_t *node) {
  22.     if(!node) return;
  23.     free(node->data);
  24.     free(node);
  25. }

  26. list_t *list_init(void) {
  27.     list_t *list = malloc(sizeof(*list));
  28.     if(!list) return list;
  29.     list->node = new_node(0);
  30.     if(!list->node) {free(list); return NULL;}
  31.     list->size = 0;
  32.     return list;
  33. }

  34. void list_deinit(list_t *list) {
  35.     if(!list) return;
  36.     list_clean(list);
  37.     delete_node(list->node);
  38.     free(list);
  39. }

  40. void list_clean(list_t *list) {
  41.     if(!list) return;
  42.     while(!list_empty(list)) list_delete(list, 0);
  43. }

  44. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  45.     if(!list) return false;
  46.     if(index > list_size(list)) return false;
  47.     list_node_t *node = new_node(size);
  48.     if(!node) return false;
  49.     memcpy(node->data, data, node->size);
  50.     list_node_t *current = list->node;
  51.     while(index--) current = current->next;
  52.     node->next = current->next;
  53.     node->prev = current;
  54.     current->next = node;
  55.     node->next->prev = node;
  56.     ++list->size;
  57.     return true;
  58. }

  59. bool list_delete(list_t *list, size_t index) {
  60.     if(!list) return false;
  61.     if(index >= list_size(list)) return false;
  62.     list_node_t *current = list->node;
  63.     while(index--) current = current->next;
  64.     list_node_t *temp = current->next;
  65.     current->next = temp->next;
  66.     temp->next->prev = current;
  67.     delete_node(temp);
  68.     --list->size;
  69.     return true;
  70. }

  71. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  72.     if(!list) return false;
  73.     bool res = list_delete(list, index);
  74.     if(!res) return res;
  75.     return list_insert(list, index, data, size);
  76. }

  77. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  78.     if(!list) return false;
  79.     if(index >= list_size(list)) return false;
  80.     list_node_t *current = GET_NODE(list, index);
  81.     if(!data) return false;
  82.     if(size < current->size) return false;
  83.     memcpy(data, current->data, current->size);
  84.     return true;
  85. }

  86. bool list_append(list_t *list, const void *data, size_t size) {
  87.     if(!list) return false;
  88.     return list_insert(list, list_size(list), data, size);
  89. }

  90. bool list_empty(const list_t *list) {
  91.     if(!list) return true;
  92.     return list->size == 0;
  93. }

  94. size_t list_size(const list_t *list) {
  95.     if(!list) return 0;
  96.     return list->size;
  97. }

  98. list_t *list_union(const list_t *a, const list_t *b) {
  99.     list_t *list = list_init();
  100.     for(size_t i = 0; i < list_size(a); ++i) {
  101.         list_node_t *current = GET_NODE(a, i);
  102.         list_append(list, current->data, current->size);
  103.     }
  104.     for(size_t i = 0; i < list_size(b); ++i) {
  105.         list_node_t *current = GET_NODE(b, i);
  106.         list_append(list, current->data, current->size);
  107.     }
  108.     return list;
  109. }
复制代码


main.c
  1. #include "list.h"
  2. #include <stdio.h>

  3. typedef struct {
  4.     char c;
  5.     int i;
  6.     double d;
  7. } test_t;

  8. int main(void) {
  9.     list_t *list = list_init();
  10.     {
  11.         test_t t = {'x', 123, 3.14};
  12.         list_append(list, &t.c, sizeof(t.c));
  13.         list_append(list, &t.i, sizeof(t.i));
  14.         list_append(list, &t.d, sizeof(t.d));
  15.         list_append(list, &t, sizeof(t));
  16.     }
  17.     {
  18.         test_t t;
  19.         list_get(list, 0, &t.c, sizeof(t.c));
  20.         list_get(list, 1, &t.i, sizeof(t.i));
  21.         list_get(list, 2, &t.d, sizeof(t.d));
  22.         printf("%c %d %lf\n", t.c, t.i, t.d);
  23.     }
  24.     {
  25.         test_t t;
  26.         list_get(list, 3, &t, sizeof(t));
  27.         printf("%c %d %lf\n", t.c, t.i, t.d);
  28.     }
  29.     {
  30.         double d;
  31.         list_delete(list, 1);
  32.         list_get(list, 1, &d, sizeof(d));
  33.         printf("%lf\n", d);
  34.     }
  35.     {
  36.         char c;
  37.         list_delete(list, 1);
  38.         list_get(list, 0, &c, sizeof(c));
  39.         printf("%c\n", c);
  40.     }
  41.     {
  42.         list_delete(list, 1);
  43.         list_delete(list, 0);
  44.         for(size_t i = 0; i < 10; ++i) {
  45.             list_append(list, &i, sizeof(i));
  46.         }
  47.     }
  48.     {
  49.         size_t i;
  50.         list_get(list, 9, &i, sizeof(i));
  51.         printf("%lu\n", i);
  52.     }
  53.     list_deinit(list);
  54.     {
  55.         list_t *a = list_init();
  56.         list_t *b = list_init();
  57.         for(size_t i = 0; i < 10; ++i) {
  58.             list_append(a, &i, sizeof(i));
  59.         }
  60.         for(size_t i = 100; i < 107; ++i) {
  61.             list_append(b, &i, sizeof(i));
  62.         }
  63.         list_t *c = list_union(a, b);
  64.         for(size_t i = 0; i < list_size(c); ++i) {
  65.             size_t value;
  66.             list_get(c, i, &value, sizeof(value));
  67.             printf("%lu ", value);
  68.         }
  69.         puts("");
  70.         list_deinit(c);
  71.         list_deinit(b);
  72.         list_deinit(a);
  73.     }
  74.     return 0;
  75. }
复制代码

  1. $ gcc-debug -o main main.c list.c
  2. $ ./main
  3. x 123 3.140000
  4. x 123 3.140000
  5. 3.140000
  6. x
  7. 9
  8. 0 1 2 3 4 5 6 7 8 9 100 101 102 103 104 105 106
  9. $
复制代码


1.jpg
2.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-22 09:09:18 From FishC Mobile | 显示全部楼层
来学习学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-22 11:32:55 | 显示全部楼层
单向循环链表

list.h
  1. #ifndef _LIST_H_
  2. #define _LIST_H_

  3. #include <stddef.h>
  4. #include <stdbool.h>

  5. typedef struct list_node_tag {
  6.     struct list_node_tag *next;
  7.     void *data;
  8.     size_t size;
  9. } list_node_t;

  10. typedef struct {
  11.     list_node_t *node;
  12.     size_t size;
  13. } list_t;

  14. list_t *list_init(void);
  15. void list_deinit(list_t *list);
  16. void list_clean(list_t *list);
  17. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  18. bool list_delete(list_t *list, size_t index);
  19. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  20. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  21. bool list_append(list_t *list, const void *data, size_t size);
  22. bool list_empty(const list_t *list);
  23. size_t list_size(const list_t *list);

  24. list_t *list_union(const list_t *a, const list_t *b);

  25. #endif
复制代码


list.c
  1. #include "list.h"
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define GET_NODE(list, index) ({ \
  5.         const list_t *this_list = list; \
  6.         size_t this_index = (index) + 1; \
  7.         list_node_t *node = this_list->node; \
  8.         while(this_index--) node = node->next; \
  9.         node; \
  10.     })

  11. static list_node_t *new_node(size_t size) {
  12.     list_node_t *node = malloc(sizeof(*node));
  13.     if(!node) return node;
  14.     node->data = malloc(size);
  15.     if(size && !node->data) {free(node); return NULL;}
  16.     node->size = size;
  17.     node->next = node;
  18.     return node;
  19. }

  20. static void delete_node(list_node_t *node) {
  21.     if(!node) return;
  22.     free(node->data);
  23.     free(node);
  24. }

  25. list_t *list_init(void) {
  26.     list_t *list = malloc(sizeof(*list));
  27.     if(!list) return list;
  28.     list->node = new_node(0);
  29.     if(!list->node) {free(list); return NULL;}
  30.     list->size = 0;
  31.     return list;
  32. }

  33. void list_deinit(list_t *list) {
  34.     if(!list) return;
  35.     list_clean(list);
  36.     delete_node(list->node);
  37.     free(list);
  38. }

  39. void list_clean(list_t *list) {
  40.     if(!list) return;
  41.     while(!list_empty(list)) list_delete(list, 0);
  42. }

  43. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  44.     if(!list) return false;
  45.     if(index > list_size(list)) return false;
  46.     list_node_t *node = new_node(size);
  47.     if(!node) return false;
  48.     memcpy(node->data, data, node->size);
  49.     list_node_t *current = list->node;
  50.     while(index--) current = current->next;
  51.     node->next = current->next;
  52.     current->next = node;
  53.     ++list->size;
  54.     return true;
  55. }

  56. bool list_delete(list_t *list, size_t index) {
  57.     if(!list) return false;
  58.     if(index >= list_size(list)) return false;
  59.     list_node_t *current = list->node;
  60.     while(index--) current = current->next;
  61.     list_node_t *temp = current->next;
  62.     current->next = temp->next;
  63.     delete_node(temp);
  64.     --list->size;
  65.     return true;
  66. }

  67. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  68.     if(!list) return false;
  69.     bool res = list_delete(list, index);
  70.     if(!res) return res;
  71.     return list_insert(list, index, data, size);
  72. }

  73. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  74.     if(!list) return false;
  75.     if(index >= list_size(list)) return false;
  76.     list_node_t *current = GET_NODE(list, index);
  77.     if(!data) return false;
  78.     if(size < current->size) return false;
  79.     memcpy(data, current->data, current->size);
  80.     return true;
  81. }

  82. bool list_append(list_t *list, const void *data, size_t size) {
  83.     if(!list) return false;
  84.     return list_insert(list, list_size(list), data, size);
  85. }

  86. bool list_empty(const list_t *list) {
  87.     if(!list) return true;
  88.     return list->size == 0;
  89. }

  90. size_t list_size(const list_t *list) {
  91.     if(!list) return 0;
  92.     return list->size;
  93. }

  94. list_t *list_union(const list_t *a, const list_t *b) {
  95.     list_t *list = list_init();
  96.     for(size_t i = 0; i < list_size(a); ++i) {
  97.         list_node_t *current = GET_NODE(a, i);
  98.         list_append(list, current->data, current->size);
  99.     }
  100.     for(size_t i = 0; i < list_size(b); ++i) {
  101.         list_node_t *current = GET_NODE(b, i);
  102.         list_append(list, current->data, current->size);
  103.     }
  104.     return list;
  105. }
复制代码


main.c
  1. #include "list.h"
  2. #include <stdio.h>

  3. typedef struct {
  4.     char c;
  5.     int i;
  6.     double d;
  7. } test_t;

  8. int main(void) {
  9.     list_t *list = list_init();
  10.     {
  11.         test_t t = {'x', 123, 3.14};
  12.         list_append(list, &t.c, sizeof(t.c));
  13.         list_append(list, &t.i, sizeof(t.i));
  14.         list_append(list, &t.d, sizeof(t.d));
  15.         list_append(list, &t, sizeof(t));
  16.     }
  17.     {
  18.         test_t t;
  19.         list_get(list, 0, &t.c, sizeof(t.c));
  20.         list_get(list, 1, &t.i, sizeof(t.i));
  21.         list_get(list, 2, &t.d, sizeof(t.d));
  22.         printf("%c %d %lf\n", t.c, t.i, t.d);
  23.     }
  24.     {
  25.         test_t t;
  26.         list_get(list, 3, &t, sizeof(t));
  27.         printf("%c %d %lf\n", t.c, t.i, t.d);
  28.     }
  29.     {
  30.         double d;
  31.         list_delete(list, 1);
  32.         list_get(list, 1, &d, sizeof(d));
  33.         printf("%lf\n", d);
  34.     }
  35.     {
  36.         char c;
  37.         list_delete(list, 1);
  38.         list_get(list, 0, &c, sizeof(c));
  39.         printf("%c\n", c);
  40.     }
  41.     {
  42.         list_delete(list, 1);
  43.         list_delete(list, 0);
  44.         for(size_t i = 0; i < 10; ++i) {
  45.             list_append(list, &i, sizeof(i));
  46.         }
  47.     }
  48.     {
  49.         size_t i;
  50.         list_get(list, 9, &i, sizeof(i));
  51.         printf("%lu\n", i);
  52.     }
  53.     list_deinit(list);
  54.     {
  55.         list_t *a = list_init();
  56.         list_t *b = list_init();
  57.         for(size_t i = 0; i < 10; ++i) {
  58.             list_append(a, &i, sizeof(i));
  59.         }
  60.         for(size_t i = 100; i < 107; ++i) {
  61.             list_append(b, &i, sizeof(i));
  62.         }
  63.         list_t *c = list_union(a, b);
  64.         for(size_t i = 0; i < list_size(c); ++i) {
  65.             size_t value;
  66.             list_get(c, i, &value, sizeof(value));
  67.             printf("%lu ", value);
  68.         }
  69.         puts("");
  70.         list_deinit(c);
  71.         list_deinit(b);
  72.         list_deinit(a);
  73.     }
  74.     return 0;
  75. }
复制代码


1.jpg
2.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-12 01:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表