鱼C论坛

 找回密码
 立即注册
查看: 1336|回复: 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语言还没讲,这个链表我是真不会,我大致花了这个链表结构,不知道是不是对的,求大佬指导一下
想知道小甲鱼最近在做啥?请访问 -> 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

那个head指针说要用二进制数表示,我们老师也没给数据,是不是自己随便编一个就行?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

节点地址不是叫你自行定义了吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

typedef struct {
    char name[32];
    size_t id;
    double score;
} student_t;

int main(void) {
    list_t *list = list_init();
    student_t student;
    for(size_t i = 0; i < 4; ++i) {
        scanf("%s%lu%lf", student.name, &student.id, &student.score);
        list_append(list, &student, sizeof(student));
    }
    for(size_t i = 0; i < list_size(list); ++i) {
        list_get(list, i, &student, sizeof(student));
        printf("name: %s\n", student.name);
        printf("id: %lu\n", student.id);
        printf("score: %.3lf\n", student.score);
    }
    list_deinit(list);
    return 0;
}

list.h
#ifndef _LIST_H_
#define _LIST_H_

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

struct list_node_tag {
    void *data; size_t size;
    struct list_node_tag *next;
};

typedef struct {
    struct list_node_tag *head;
    size_t size;
} list_t;

list_t *list_init(void);
void list_deinit(list_t *list);
bool list_clean(list_t *list);
bool list_insert(list_t *list, size_t index, const void *data, size_t size);
bool list_delete(list_t *list, size_t index);
bool list_get(const list_t *list, size_t index, void *data, size_t size);
bool list_set(list_t *list, size_t index, const void *data, size_t size);
bool list_append(list_t *list, const void *data, size_t size);
size_t list_size(const list_t *list);
bool list_empty(const list_t *list);

#endif

list.c
#include "list.h"
#include <stdlib.h>
#include <memory.h>

list_t *list_init(void) {
    list_t *list = malloc(sizeof(*list));
    if(!list) return NULL;
    list->head = NULL;
    list->size = 0;
    return list;
}

void list_deinit(list_t *list) {
    if(!list) return;
    list_clean(list);
    free(list);
}

bool list_clean(list_t *list) {
    if(!list) return false;
    while(!list_empty(list)) list_delete(list, 0);
    return true;
}

bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
    if(!list) return false;
    if(list_size(list) < index) return false;
    if(!data) return false;
    struct list_node_tag **current = &list->head;
    while(index--) current = &(*current)->next;
    struct list_node_tag *node = malloc(sizeof(*node));
    if(!node) return false;
    node->data = malloc(size);
    if(!node->data) {free(node); return false;}
    memcpy(node->data, data, size);
    node->size = size;
    node->next = *current;
    *current = node;
    ++list->size;
    return true;
}

bool list_delete(list_t *list, size_t index) {
    if(!list) return false;
    if(list_size(list) <= index) return false;
    struct list_node_tag **current = &list->head;
    while(index--) current = &(*current)->next;
    struct list_node_tag *temp = *current;
    *current = temp->next;
    free(temp->data); free(temp);
    --list->size;
    return true;
}

bool list_get(const list_t *list, size_t index, void *data, size_t size) {
    if(!list) return false;
    if(list_size(list) <= index) return false;
    if(!data) return false;
    struct list_node_tag *const *current = &list->head;
    while(index--) current = &(*current)->next;
    struct list_node_tag *temp = *current;
    if(temp->size > size) return false;
    memcpy(data, temp->data, temp->size);
    return true;
}

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

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

size_t list_size(const list_t *list) {
    if(!list) return 0;
    return list->size;
}

bool list_empty(const list_t *list) {
    if(!list) return true;
    return list_size(list) == 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

不是静态链表吗?我没学过,我也不知道
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

https://www.jianshu.com/p/0f05c2fb3d81
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-23 20:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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