增加插入数据的功能
This commit is contained in:
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"stdbool.h": "c"
|
||||||
|
}
|
||||||
|
}
|
@@ -37,9 +37,9 @@ bool list_init(list_t *list, size_t elem_size) {
|
|||||||
* @return true
|
* @return true
|
||||||
* @return false
|
* @return false
|
||||||
*/
|
*/
|
||||||
bool list_enlist(list_t *list, const void *value) {
|
bool list_push(list_t *list, const void *value) {
|
||||||
// 分配新节点
|
// 分配新节点
|
||||||
if (list->size >= list_MAX_SIZE) {
|
if (list->size >= LIST_MAX_SIZE) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
list_node_t *node = (list_node_t *)malloc(sizeof(list_node_t));
|
list_node_t *node = (list_node_t *)malloc(sizeof(list_node_t));
|
||||||
@@ -80,7 +80,7 @@ bool list_enlist(list_t *list, const void *value) {
|
|||||||
* @return true
|
* @return true
|
||||||
* @return false
|
* @return false
|
||||||
*/
|
*/
|
||||||
bool list_delist_head(list_t *list, void *value) {
|
bool list_pop_head(list_t *list, void *value) {
|
||||||
// 检查队列是否为空
|
// 检查队列是否为空
|
||||||
if (list->size == 0) {
|
if (list->size == 0) {
|
||||||
return false;
|
return false;
|
||||||
@@ -109,7 +109,7 @@ bool list_delist_head(list_t *list, void *value) {
|
|||||||
* @return true
|
* @return true
|
||||||
* @return false
|
* @return false
|
||||||
*/
|
*/
|
||||||
bool list_delist_tail(list_t *list, void *value) {
|
bool list_pop_tail(list_t *list, void *value) {
|
||||||
if (list->size == 0) {
|
if (list->size == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -156,6 +156,58 @@ bool list_peek_at(const list_t *list, size_t index, void *value) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 插入一个数据到队列指定位置
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
|
* @param index
|
||||||
|
* @param value
|
||||||
|
* @return true
|
||||||
|
* @return false
|
||||||
|
*/
|
||||||
|
bool list_insert_at(list_t *list, size_t index, const void *value){
|
||||||
|
if (index > list->size || list->size >= LIST_MAX_SIZE) {
|
||||||
|
return false; // 越界或队列已满
|
||||||
|
}
|
||||||
|
list_node_t *node = (list_node_t *)malloc(sizeof(list_node_t));
|
||||||
|
if (!node) {
|
||||||
|
return false; // 内存分配失败
|
||||||
|
}
|
||||||
|
node->data = malloc(list->elem_size);
|
||||||
|
if (!node->data) {// 数据分配失败,释放节点
|
||||||
|
free(node);
|
||||||
|
return false; // 数据分配失败
|
||||||
|
}
|
||||||
|
memcpy(node->data, value, list->elem_size);
|
||||||
|
|
||||||
|
if (index == 0) {
|
||||||
|
// 插入到头部
|
||||||
|
node->next = list->head;
|
||||||
|
list->head = node;
|
||||||
|
if (list->size == 0) {
|
||||||
|
list->tail = node; // 如果队列之前为空,更新尾部指针
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 插入到中间或尾部
|
||||||
|
list_node_t *prev = NULL;
|
||||||
|
list_node_t *current = list->head;
|
||||||
|
for (size_t i = 0; i < index; ++i) {
|
||||||
|
prev = current;
|
||||||
|
current = (current ? current->next : NULL);
|
||||||
|
}
|
||||||
|
node->next = current;
|
||||||
|
if (prev) {
|
||||||
|
prev->next = node;
|
||||||
|
}
|
||||||
|
if (current == NULL) {
|
||||||
|
list->tail = node; // 如果插入到尾部,更新尾部指针
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
list->size++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 遍历队列
|
* @brief 遍历队列
|
||||||
*
|
*
|
||||||
|
@@ -14,7 +14,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#define list_MAX_SIZE (1024)
|
#define LIST_MAX_SIZE (1024)
|
||||||
// 定义队列节点
|
// 定义队列节点
|
||||||
typedef struct list_node {
|
typedef struct list_node {
|
||||||
void *data;
|
void *data;
|
||||||
@@ -33,15 +33,17 @@ typedef struct {
|
|||||||
bool list_init(list_t *list, size_t elem_size);
|
bool list_init(list_t *list, size_t elem_size);
|
||||||
|
|
||||||
// 入队操作
|
// 入队操作
|
||||||
bool list_enlist(list_t *list, const void *value);
|
bool list_push(list_t *list, const void *value);
|
||||||
// 出队操作
|
// 出队操作
|
||||||
|
|
||||||
// 从队列头部出队
|
// 从队列头部出队
|
||||||
bool list_delist_head(list_t *list, void *value);
|
bool list_pop_head(list_t *list, void *value);
|
||||||
// 从队列尾部出队
|
// 从队列尾部出队
|
||||||
bool list_delist_tail(list_t *list, void *value);
|
bool list_pop_tail(list_t *list, void *value);
|
||||||
// 查询队列指定位置的数据
|
// 查询队列指定位置的数据
|
||||||
bool list_peek_at(const list_t *list, size_t index, void *value);
|
bool list_peek_at(const list_t *list, size_t index, void *value);
|
||||||
|
// 插入数据
|
||||||
|
bool list_insert_at(list_t *list, size_t index, const void *value);
|
||||||
// 遍历队列
|
// 遍历队列
|
||||||
void list_foreach(const list_t *list, void (*func)(void *data, size_t index));
|
void list_foreach(const list_t *list, void (*func)(void *data, size_t index));
|
||||||
// 抛弃队列前n个数据
|
// 抛弃队列前n个数据
|
||||||
|
@@ -27,7 +27,7 @@ int main(void) {
|
|||||||
|
|
||||||
// 入队
|
// 入队
|
||||||
for (int i = 1; i <= 10; ++i) {
|
for (int i = 1; i <= 10; ++i) {
|
||||||
ret = list_enlist(&q, &i);
|
ret = list_push(&q, &i);
|
||||||
printf("Enlist %d: %s\n", i, ret ? "OK" : "FAIL");
|
printf("Enlist %d: %s\n", i, ret ? "OK" : "FAIL");
|
||||||
}
|
}
|
||||||
list_foreach(&q, print_int);
|
list_foreach(&q, print_int);
|
||||||
@@ -40,14 +40,14 @@ int main(void) {
|
|||||||
|
|
||||||
// 从头部出队
|
// 从头部出队
|
||||||
for (int i = 0; i < 2; ++i) {
|
for (int i = 0; i < 2; ++i) {
|
||||||
if (list_delist_head(&q, &val)) {
|
if (list_pop_head(&q, &val)) {
|
||||||
printf("Delist head: %d\n", val);
|
printf("Delist head: %d\n", val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
list_foreach(&q, print_int);
|
list_foreach(&q, print_int);
|
||||||
|
|
||||||
// 从尾部出队
|
// 从尾部出队
|
||||||
if (list_delist_tail(&q, &val)) {
|
if (list_pop_tail(&q, &val)) {
|
||||||
printf("Delist tail: %d\n", val);
|
printf("Delist tail: %d\n", val);
|
||||||
}
|
}
|
||||||
list_foreach(&q, print_int);
|
list_foreach(&q, print_int);
|
||||||
|
Reference in New Issue
Block a user