创建字符型的单链表
有大佬知道怎么创建一个储存字符串的单链表吗?比如我要存储一个姓氏的链表zhao qian sun li zhou然后进行插入删除其他的姓氏$ ls
list.clist.hmainmain.c
$ cat main.c
#include "list.h"
#include <stdio.h>
void output(const list_t *list, size_t index) {
if(index >= list_size(list)) return;
int temp; list_get(list, index, &temp, sizeof(temp));
printf("%c", temp);
output(list, index + 1);
}
int main(void) {
const char *str = "hello world!";
list_t *list = list_init();
for(size_t i = 0; str; ++i) {
list_append(list, &str, sizeof(str));
}
output(list, 0); puts("");
list_deinit(list);
return 0;
}
$ 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
$ cat 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;
}
$
人造人 发表于 2022-10-16 20:18
请问大佬这个是用c实现的吗,一开头的符号识别不了{:9_234:}
耀耀切克闹 发表于 2022-10-16 20:25
请问大佬这个是用c实现的吗,一开头的符号识别不了
一开始的那些是linux下的指令,复制粘贴的时候把那些去掉
耀耀切克闹 发表于 2022-10-16 20:25
请问大佬这个是用c实现的吗,一开头的符号识别不了
还有,这不是一个文件,这是3个文件
list.clist.h main.c
人造人 发表于 2022-10-16 20:30
还有,这不是一个文件,这是3个文件
list.clist.h main.c
感谢大佬
页:
[1]