鱼C论坛

 找回密码
 立即注册
查看: 2650|回复: 5

[已解决]创建字符型的单链表

[复制链接]
发表于 2022-10-16 19:30:33 | 显示全部楼层 |阅读模式

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

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

x
有大佬知道怎么创建一个储存字符串的单链表吗?比如我要存储一个姓氏的链表zhao qian sun li zhou然后进行插入删除其他的姓氏
最佳答案
2022-10-16 20:29:03
耀耀切克闹 发表于 2022-10-16 20:25
请问大佬这个是用c实现的吗,一开头的符号识别不了

一开始的那些是linux下的指令,复制粘贴的时候把那些去掉
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-16 20:18:37 | 显示全部楼层
  1. $ ls
  2. list.c  list.h  main  main.c
  3. $ cat main.c
  4. #include "list.h"
  5. #include <stdio.h>

  6. void output(const list_t *list, size_t index) {
  7.     if(index >= list_size(list)) return;
  8.     int temp; list_get(list, index, &temp, sizeof(temp));
  9.     printf("%c", temp);
  10.     output(list, index + 1);
  11. }

  12. int main(void) {
  13.     const char *str = "hello world!";
  14.     list_t *list = list_init();
  15.     for(size_t i = 0; str[i]; ++i) {
  16.         list_append(list, &str[i], sizeof(str[i]));
  17.     }
  18.     output(list, 0); puts("");
  19.     list_deinit(list);
  20.     return 0;
  21. }
  22. $ cat list.h
  23. #ifndef _LIST_H_
  24. #define _LIST_H_

  25. #include <stddef.h>
  26. #include <stdbool.h>

  27. struct list_node_tag {
  28.     void *data; size_t size;
  29.     struct list_node_tag *next;
  30. };

  31. typedef struct {
  32.     struct list_node_tag *head;
  33.     size_t size;
  34. } list_t;

  35. list_t *list_init(void);
  36. void list_deinit(list_t *list);
  37. bool list_clean(list_t *list);
  38. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  39. bool list_delete(list_t *list, size_t index);
  40. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  41. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  42. bool list_append(list_t *list, const void *data, size_t size);
  43. size_t list_size(const list_t *list);
  44. bool list_empty(const list_t *list);

  45. #endif
  46. $ cat list.c
  47. #include "list.h"
  48. #include <stdlib.h>
  49. #include <memory.h>

  50. list_t *list_init(void) {
  51.     list_t *list = malloc(sizeof(*list));
  52.     if(!list) return NULL;
  53.     list->head = NULL;
  54.     list->size = 0;
  55.     return list;
  56. }

  57. void list_deinit(list_t *list) {
  58.     if(!list) return;
  59.     list_clean(list);
  60.     free(list);
  61. }

  62. bool list_clean(list_t *list) {
  63.     if(!list) return false;
  64.     while(!list_empty(list)) list_delete(list, 0);
  65.     return true;
  66. }

  67. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  68.     if(!list) return false;
  69.     if(list_size(list) < index) return false;
  70.     if(!data) return false;
  71.     struct list_node_tag **current = &list->head;
  72.     while(index--) current = &(*current)->next;
  73.     struct list_node_tag *node = malloc(sizeof(*node));
  74.     if(!node) return false;
  75.     node->data = malloc(size);
  76.     if(!node->data) {free(node); return false;}
  77.     memcpy(node->data, data, size);
  78.     node->size = size;
  79.     node->next = *current;
  80.     *current = node;
  81.     ++list->size;
  82.     return true;
  83. }

  84. bool list_delete(list_t *list, size_t index) {
  85.     if(!list) return false;
  86.     if(list_size(list) <= index) return false;
  87.     struct list_node_tag **current = &list->head;
  88.     while(index--) current = &(*current)->next;
  89.     struct list_node_tag *temp = *current;
  90.     *current = temp->next;
  91.     free(temp->data); free(temp);
  92.     --list->size;
  93.     return true;
  94. }

  95. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  96.     if(!list) return false;
  97.     if(list_size(list) <= index) return false;
  98.     if(!data) return false;
  99.     struct list_node_tag *const *current = &list->head;
  100.     while(index--) current = &(*current)->next;
  101.     struct list_node_tag *temp = *current;
  102.     if(temp->size > size) return false;
  103.     memcpy(data, temp->data, temp->size);
  104.     return true;
  105. }

  106. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  107.     bool res = list_delete(list, index);
  108.     return res ? list_insert(list, index, data, size) : res;
  109. }

  110. bool list_append(list_t *list, const void *data, size_t size) {
  111.     if(!list) return false;
  112.     return list_insert(list, list_size(list), data, size);
  113. }

  114. size_t list_size(const list_t *list) {
  115.     if(!list) return 0;
  116.     return list->size;
  117. }

  118. bool list_empty(const list_t *list) {
  119.     if(!list) return true;
  120.     return list_size(list) == 0;
  121. }
  122. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-10-16 20:25:41 | 显示全部楼层

请问大佬这个是用c实现的吗,一开头的符号识别不了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-16 20:29:03 | 显示全部楼层    本楼为最佳答案   
耀耀切克闹 发表于 2022-10-16 20:25
请问大佬这个是用c实现的吗,一开头的符号识别不了

一开始的那些是linux下的指令,复制粘贴的时候把那些去掉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-16 20:30:11 | 显示全部楼层
耀耀切克闹 发表于 2022-10-16 20:25
请问大佬这个是用c实现的吗,一开头的符号识别不了

还有,这不是一个文件,这是3个文件
list.c  list.h main.c
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-16 20:32:00 | 显示全部楼层
人造人 发表于 2022-10-16 20:30
还有,这不是一个文件,这是3个文件
list.c  list.h main.c

感谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 07:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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