鱼C论坛

 找回密码
 立即注册
查看: 1632|回复: 6

这个链表怎么做啊?

[复制链接]
发表于 2021-11-21 15:11:13 | 显示全部楼层 |阅读模式

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

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

x
数据结构测试要求与评分.pdf 和另外 13 个页面 - 个人 - Microsoft Edge 2021_11_21 1.png

head - Word 2021_11_21 15_08_47.png

我们老师c语言还没讲,这个链表我是真不会,我大致花了这个链表结构,不知道是不是对的,求大佬指导一下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-21 17:23:54 | 显示全部楼层

回帖奖励 +10 鱼币

本帖最后由 jhq999 于 2021-11-21 17:33 编辑

对,就是单链表的结构
src=http___res.youth.cn_img-detail_2522c33d063bc3f6ffb26e7c28efb0da_382_220.jpg.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-21 18:27:15 | 显示全部楼层
jhq999 发表于 2021-11-21 17:23
对,就是单链表的结构

那个head指针说要用二进制数表示,我们老师也没给数据,是不是自己随便编一个就行?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-21 18:34:32 | 显示全部楼层
gjfyyds 发表于 2021-11-21 18:27
那个head指针说要用二进制数表示,我们老师也没给数据,是不是自己随便编一个就行?

节点地址不是叫你自行定义了吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-21 18:39:16 | 显示全部楼层
本帖最后由 人造人 于 2021-11-21 18:41 编辑

静态链表?你画的图不是静态链表吧?
按你画的图写了一个(其实是直接复制我之前写好的,^_^)

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

  3. typedef struct {
  4.     char name[32];
  5.     size_t id;
  6.     double score;
  7. } student_t;

  8. int main(void) {
  9.     list_t *list = list_init();
  10.     student_t student;
  11.     for(size_t i = 0; i < 4; ++i) {
  12.         scanf("%s%lu%lf", student.name, &student.id, &student.score);
  13.         list_append(list, &student, sizeof(student));
  14.     }
  15.     for(size_t i = 0; i < list_size(list); ++i) {
  16.         list_get(list, i, &student, sizeof(student));
  17.         printf("name: %s\n", student.name);
  18.         printf("id: %lu\n", student.id);
  19.         printf("score: %.3lf\n", student.score);
  20.     }
  21.     list_deinit(list);
  22.     return 0;
  23. }
复制代码


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

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

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

  9. typedef struct {
  10.     struct list_node_tag *head;
  11.     size_t size;
  12. } list_t;

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

  23. #endif
复制代码


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

  4. list_t *list_init(void) {
  5.     list_t *list = malloc(sizeof(*list));
  6.     if(!list) return NULL;
  7.     list->head = NULL;
  8.     list->size = 0;
  9.     return list;
  10. }

  11. void list_deinit(list_t *list) {
  12.     if(!list) return;
  13.     list_clean(list);
  14.     free(list);
  15. }

  16. bool list_clean(list_t *list) {
  17.     if(!list) return false;
  18.     while(!list_empty(list)) list_delete(list, 0);
  19.     return true;
  20. }

  21. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  22.     if(!list) return false;
  23.     if(list_size(list) < index) return false;
  24.     if(!data) return false;
  25.     struct list_node_tag **current = &list->head;
  26.     while(index--) current = &(*current)->next;
  27.     struct list_node_tag *node = malloc(sizeof(*node));
  28.     if(!node) return false;
  29.     node->data = malloc(size);
  30.     if(!node->data) {free(node); return false;}
  31.     memcpy(node->data, data, size);
  32.     node->size = size;
  33.     node->next = *current;
  34.     *current = node;
  35.     ++list->size;
  36.     return true;
  37. }

  38. bool list_delete(list_t *list, size_t index) {
  39.     if(!list) return false;
  40.     if(list_size(list) <= index) return false;
  41.     struct list_node_tag **current = &list->head;
  42.     while(index--) current = &(*current)->next;
  43.     struct list_node_tag *temp = *current;
  44.     *current = temp->next;
  45.     free(temp->data); free(temp);
  46.     --list->size;
  47.     return true;
  48. }

  49. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  50.     if(!list) return false;
  51.     if(list_size(list) <= index) return false;
  52.     if(!data) return false;
  53.     struct list_node_tag *const *current = &list->head;
  54.     while(index--) current = &(*current)->next;
  55.     struct list_node_tag *temp = *current;
  56.     if(temp->size > size) return false;
  57.     memcpy(data, temp->data, temp->size);
  58.     return true;
  59. }

  60. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  61.     bool res = list_delete(list, index);
  62.     return res ? list_insert(list, index, data, size) : res;
  63. }

  64. bool list_append(list_t *list, const void *data, size_t size) {
  65.     if(!list) return false;
  66.     return list_insert(list, list_size(list), data, size);
  67. }

  68. size_t list_size(const list_t *list) {
  69.     if(!list) return 0;
  70.     return list->size;
  71. }

  72. bool list_empty(const list_t *list) {
  73.     if(!list) return true;
  74.     return list_size(list) == 0;
  75. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-21 19:51:52 | 显示全部楼层
人造人 发表于 2021-11-21 18:39
静态链表?你画的图不是静态链表吧?
按你画的图写了一个(其实是直接复制我之前写好的,^_^)

不是静态链表吗?我没学过,我也不知道
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-21 22:07:01 | 显示全部楼层
gjfyyds 发表于 2021-11-21 19:51
不是静态链表吗?我没学过,我也不知道

https://www.jianshu.com/p/0f05c2fb3d81
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 13:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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