|
发表于 2021-11-5 20:58:21
|
显示全部楼层
1. 不要把所有的代码都写到一个文件中,这样不方便修改,不方便阅读
2. 申请了内存要释放,你为什么不写释放内存的代码?
释放内存,这么重要的操作你居然完全忽略?
3. 你的代码还有很多其他问题,我没有一个一个去看
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, 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_append(list_t *list, 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, 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, size);
return true;
}
bool list_append(list_t *list, 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 0;
return list_size(list) == 0;
}
main.c#include "list.h"
#include <stdio.h>
void read_number(list_t *list) {
while(1) {
size_t n; scanf("%lu", &n);
if(n == 0) break;
list_append(list, &n, sizeof(n));
}
}
list_t *list_merge(const list_t *a, const list_t *b) {
size_t i = 0, j = 0;
list_t *list = list_init();
while(i < list_size(a) && j < list_size(b)) {
size_t m, n;
list_get(a, i, &m, sizeof(m));
list_get(b, j, &n, sizeof(n));
if(m < n) {
list_append(list, &m, sizeof(m));
++i;
} else {
list_append(list, &n, sizeof(n));
++j;
}
}
while(i < list_size(a)) {
size_t m;
list_get(a, i, &m, sizeof(m));
list_append(list, &m, sizeof(m));
++i;
}
while(j < list_size(b)) {
size_t n;
list_get(b, j, &n, sizeof(n));
list_append(list, &n, sizeof(n));
++j;
}
return list;
}
void list_output(const list_t *list) {
const char *sep = "";
for(size_t i = 0; i < list_size(list); ++i) {
size_t temp; list_get(list, i, &temp, sizeof(temp));
printf("%s%lu", sep, temp); sep = " ";
}
printf("\n");
}
int main(void) {
list_t *s1 = list_init();
list_t *s2 = list_init();
read_number(s1);
read_number(s2);
list_t *s3 = list_merge(s1, s2);
list_output(s3);
list_deinit(s3);
list_deinit(s2);
list_deinit(s1);
return 0;
}
|
|