环境
$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.1 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
list.h在这里可以找到
Application使用内核链表_蓝牙先生的博客-CSDN博客经常遇到需要使用链表、丢列、栈数据结构问题,备份一下,免得每次都去查找;在kernel模块里面创建链表_内核模块中简单的插入链表_蓝牙先生的博客-CSDN博客。
https://blog.csdn.net/m0_37132481/article/details/129672070?spm=1001.2014.3001.5501
list_queue.h
$ cat list_queue.h
#if ! defined(__LIST_QUEUE_H__)
#define __LIST_QUEUE_H__
#include
#include
#include void *list_queue_alloc(void);
void list_queue_destroy(void *handle);
uint32_t list_queue_get_size(void *handle);
bool list_queue_in(void *handle, void *data);
bool list_queue_out(void *handle, void **data);#endif
list_queue.c
$ cat list_queue.c #include "list_queue.h"#include "list.h"struct queue_data_t {void *data;struct list_head node;};struct list_queue_mgr_t {uint32_t queue_size;struct queue_data_t queue_head;};void *list_queue_alloc(void) {struct list_queue_mgr_t *handle = (struct list_queue_mgr_t *)calloc(1, sizeof(struct list_queue_mgr_t));if(!handle) {/* null */} else {handle->queue_size = 0;INIT_LIST_HEAD(&handle->queue_head.node);}return handle;}void list_queue_destroy(void *handle) {struct list_queue_mgr_t *mgr = (struct list_queue_mgr_t *)handle;if(!mgr) {/* null */} else {if(list_empty(&mgr->queue_head.node)) {/* null */} else {struct queue_data_t *pos = NULL, *tmp = NULL;list_for_each_entry_safe(pos, tmp, &mgr->queue_head.node, node) {list_del(&pos->node);free(pos->data);free(pos);mgr->queue_size--;}}free(mgr);}}uint32_t list_queue_get_size(void *handle) {struct list_queue_mgr_t *mgr = (struct list_queue_mgr_t *)handle;if(!handle) {/* null */} else {return mgr->queue_size;}}bool list_queue_in(void *handle, void *data) {struct list_queue_mgr_t *mgr = (struct list_queue_mgr_t *)handle;if(!data || !handle) {/* null */} else {struct queue_data_t *new = (struct queue_data_t*)calloc(1, sizeof(struct queue_data_t));if(!new) {/* null */} else {new->data = data;list_add_tail(&new->node, &mgr->queue_head.node);mgr->queue_size++;}}}bool list_queue_out(void *handle, void **data) {struct list_queue_mgr_t *mgr = (struct list_queue_mgr_t *)handle;if(!data || !handle) {/* null */} else if(list_empty(&mgr->queue_head.node)){/* null */} else {struct list_head *node = mgr->queue_head.node.next;list_del(node);struct queue_data_t *d = list_entry(node, struct queue_data_t, node);*data = d->data;free(d);mgr->queue_size--;}}
main.c
$ cat main.c
/*file name:main.cdate : Mon Mar 20 06:20:04 AM EDT 2023
*/
#include
#include
#include
#include
#include
#include
#include
#include "list_queue.h"int main(int argc, char *argv[])
{void *handle = list_queue_alloc();for(int loop = 0; ; loop++) {for(int i = 0; i < 10; i++) {int *data = calloc(1, sizeof(int));*data = i;list_queue_in(handle, (void *)data);}while(list_queue_get_size(handle)) {int *data;list_queue_out(handle, (void **)&data);printf("%d ", *data);}printf("\n");}list_queue_destroy(handle);return 0;
}
经常遇到需要使用链表、丢列、栈数据结构问题,备份一下,免得每次都去查找;方便下次快速使用;
<完>
上一篇: 监督年度工作计划