|
发表于 2023-11-7 20:33:34
|
显示全部楼层
本楼为最佳答案
 - sh-5.2$ cat main.c
- #include "list.h"
- #include <stdio.h>
- size_t find(const list_t *list, int n) {
- for(size_t i = 0; i < list_size(list); ++i) {
- int x; list_get(list, i, &x, sizeof(x));
- if(x == n) return i;
- }
- return -1;
- }
- int main(void) {
- list_t *list = list_init();
- while(1) {
- int n; scanf("%d", &n);
- if(find(list, n) != -1) break;
- list_append(list, &n, sizeof(n));
- }
- list_deinit(list);
- return 0;
- }
- sh-5.2$ cat 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
- sh-5.2$ cat list.c
- #include "list.h"
- #include <stdlib.h>
- #include <string.h>
- static struct list_node_tag *new_node(const void *data, size_t size) {
- struct list_node_tag *result = malloc(sizeof(*result));
- if(!result) return NULL;
- result->data = malloc(size);
- if(!result->data) {free(result); return NULL;}
- memcpy(result->data, data, size);
- result->size = size;
- result->next = NULL;
- return result;
- }
- static void free_node(struct list_node_tag *n) {
- free(n->data);
- free(n);
- }
- static struct list_node_tag *const *locate_node_const(const list_t *list, size_t index) {
- struct list_node_tag *const *result = &list->head;
- while(index--) result = &(*result)->next;
- return result;
- }
- static struct list_node_tag **locate_node(list_t *list, size_t index) {
- return (struct list_node_tag **)locate_node_const(list, index);
- }
- 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)) if(!list_delete(list, 0)) return false;
- 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 = locate_node(list, index);
- struct list_node_tag *node = new_node(data, size);
- if(!node) return false;
- 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 = locate_node(list, index);
- struct list_node_tag *temp = *current;
- *current = temp->next;
- free_node(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 *current = *locate_node_const(list, index);
- if(current->size > size) return false;
- memcpy(data, current->data, current->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;
- }
- sh-5.2$ ./main
- 1 2 3
- 1
- sh-5.2$
复制代码 |
|