为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

jelasin 的学生作业:

//seqlist.h #ifndef __SEQLIST_H__ #define __SEQLIST_H__ #include #include #include #include #define RED "\033[31m" #define NONE "\033[0m" #define MAX 10 //实际学⽣的存储 struct student { char name[20]; int id; int age; }; typedef struct student datatype_t; typedef struct{ datatype_t buf[MAX]; //定义数组记录班级学⽣每个学⽣的信息。 int n; //学⽣实际到来的个数。 }seqlist_t; extern seqlist_t *create_empty_seqlist(); extern bool is_full_seqlist(seqlist_t *l); extern void insert_data_seqlist(seqlist_t *l, datatype_t data); extern void printf_data_seqlist(seqlist_t *l); extern void destroy_seqlist(seqlist_t *l); extern bool is_empty_seqlist(seqlist_t *l); extern bool is_equal(datatype_t a, datatype_t b); extern int delete_data_seqlist(seqlist_t *l, datatype_t data); extern int find_data_seqlist(seqlist_t *l, datatype_t data); extern int replace_data_seqlist(seqlist_t *l, datatype_t old, datatype_t new); #endif //seqlist.c #include "seqlist.h" seqlist_t *create_empty_seqlist() { seqlist_t * l = (seqlist_t *)malloc(sizeof(seqlist_t)); if (NULL == l) { perror(RED "[-] malloc error when create_empty_seqlist" NONE); return NULL; } memset(l, '\x00', sizeof(seqlist_t)); l->n = 0; return l; } bool is_full_seqlist(seqlist_t *l) { return l->n >= MAX ? true : false; } void insert_data_seqlist(seqlist_t *l, datatype_t data) { if (is_full_seqlist(l)) { printf(RED "[*]The list is full, cannot insert data.\n" NONE); return; } l->buf[l->n] = data; l->n++; return; } void printf_data_seqlist(seqlist_t *l) { for (int i = 0; i n; i++) { printf("Name: %s, ID: %d, Age: %d\n", l->buf[i].name, l->buf[i].id, l->buf[i].age); } return; } void destroy_seqlist(seqlist_t *l) { free(l); l = NULL; return; } bool is_empty_seqlist(seqlist_t *l) { return l->n == 0 ? true : false; } bool is_equal(datatype_t a, datatype_t b) { if (a.age == b.age && a.id == b.id && !strcmp(a.name, b.name)) { return true; } else { return false; } } int delete_data_seqlist(seqlist_t *l, datatype_t data) { if (is_empty_seqlist(l)) { return -1; } int i = 0, j = 0; for (i = 0; i n; i++) { if (!is_equal(l->buf[i], data)) { l->buf[j++] = l->buf[i]; } } l->n = j; if (i == j) { return -2; } } int find_data_seqlist(seqlist_t *l, datatype_t data) { for (int i = 0; i n; i++) { if (is_equal(l->buf[i], data)) { return i; } } return -1; } int replace_data_seqlist(seqlist_t *l, datatype_t old, datatype_t new) { while (-1 != find_data_seqlist(l, old)) { l->buf[find_data_seqlist(l, old)] = new; } return 0; } //main.c #include "seqlist.h" int test() { seqlist_t *list = create_empty_seqlist(); if (NULL == list) { perror(RED "[-]Failed to create seqlist" NONE); return 0; } printf("[1] is_empty ==> %s \n", is_empty_seqlist(list) ? "true" : "false"); datatype_t data_1 = { .age = 25, .id = 1, .name = "John Doe" }; datatype_t data_2 = { .age = 26, .id = 2, .name = "Trump" }; for (size_t i = 0; i delete_data_seqlist(list, data_2)) { perror(RED "[-] Failed to delete data from seqlist, seqlist is empty or data is not exist" NONE); } printf("[4] printf_data_seqlist\n"); printf_data_seqlist(list); replace_data_seqlist(list, data_1, (datatype_t){ .age = 30, .id = 3, .name = "Jane Doe" }); printf("[5] After replacement\n"); printf_data_seqlist(list); printf("[6] destroy_seqlist\n"); destroy_seqlist(list); return 0; } int main(int argc, char const *argv[]) { return test(); } ➜ 1 ./seqlist [1] is_empty ==> true [2] printf_data_seqlist Name: John Doe, ID: 1, Age: 25 Name: Trump, ID: 2, Age: 26 Name: John Doe, ID: 1, Age: 25 Name: Trump, ID: 2, Age: 26 Name: John Doe, ID: 1, Age: 25 Name: Trump, ID: 2, Age: 26 Name: John Doe, ID: 1, Age: 25 Name: Trump, ID: 2, Age: 26 Name: John Doe, ID: 1, Age: 25 Name: Trump, ID: 2, Age: 26 [3] delete data_2 [4] printf_data_seqlist Name: John Doe, ID: 1, Age: 25 Name: John Doe, ID: 1, Age: 25 Name: John Doe, ID: 1, Age: 25 Name: John Doe, ID: 1, Age: 25 Name: John Doe, ID: 1, Age: 25 [5] After replacement Name: Jane Doe, ID: 3, Age: 30 Name: Jane Doe, ID: 3, Age: 30 Name: Jane Doe, ID: 3, Age: 30 Name: Jane Doe, ID: 3, Age: 30 Name: Jane Doe, ID: 3, Age: 30 [6] destroy_seqlist

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号