Linux 链表示例 LIST_INIT LIST_INSERT_HEAD

list(3) — Linux manual page

用Visual Studio 2022创建CMake项目

* CmakeLists.txt

# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.12)project ("llist")# Include sub-projects.
add_subdirectory ("llist")

* llist/CMakeLists.txt

# CMakeList.txt : CMake project for llist, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.12)# Add source to this project's executable.
add_executable (llist "llist.c" "llist.h")if (CMAKE_VERSION VERSION_GREATER 3.12)set_property(TARGET llist PROPERTY CXX_STANDARD 11)
endif()# TODO: Add tests and install targets if needed.

* llist/llist.h

// llist.h : Include file for standard system include files,
// or project specific include files.#pragma once#ifndef NULL
#define NULL (void *)0
#endif
/** List definitions.*/
#define LIST_HEAD(name, type)                                           \
struct name {                                                           \struct type *lh_first;  /* first element */                     \
}#define LIST_HEAD_INITIALIZER(head)                                     \{ NULL }#define LIST_ENTRY(type)                                                \
struct {                                                                \struct type *le_next;   /* next element */                      \struct type **le_prev;  /* address of previous next element */  \
}/** List functions.*/
#define LIST_INIT(head) do {                                            \(head)->lh_first = NULL;                                        \
} while (/*CONSTCOND*/0)#define LIST_INSERT_AFTER(listelm, elm, field) do {                     \if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)  \(listelm)->field.le_next->field.le_prev =               \&(elm)->field.le_next;                              \(listelm)->field.le_next = (elm);                               \(elm)->field.le_prev = &(listelm)->field.le_next;               \
} while (/*CONSTCOND*/0)#define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \(elm)->field.le_prev = (listelm)->field.le_prev;                \(elm)->field.le_next = (listelm);                               \*(listelm)->field.le_prev = (elm);                              \(listelm)->field.le_prev = &(elm)->field.le_next;               \
} while (/*CONSTCOND*/0)#define LIST_INSERT_HEAD(head, elm, field) do {                         \if (((elm)->field.le_next = (head)->lh_first) != NULL)          \(head)->lh_first->field.le_prev = &(elm)->field.le_next;\(head)->lh_first = (elm);                                       \(elm)->field.le_prev = &(head)->lh_first;                       \
} while (/*CONSTCOND*/0)#define LIST_REMOVE(elm, field) do {                                    \if ((elm)->field.le_next != NULL)                               \(elm)->field.le_next->field.le_prev =                   \(elm)->field.le_prev;                               \*(elm)->field.le_prev = (elm)->field.le_next;                   \
} while (/*CONSTCOND*/0)#define LIST_FOREACH(var, head, field)                                  \for ((var) = ((head)->lh_first);                                \(var);                                                  \(var) = ((var)->field.le_next))/** List access methods.*/
#define LIST_EMPTY(head)                ((head)->lh_first == NULL)
#define LIST_FIRST(head)                ((head)->lh_first)
#define LIST_NEXT(elm, field)           ((elm)->field.le_next)// TODO: Reference additional headers your program requires here.

llist/llist.c

/* llist.c : Defines the entry point for the application.* /
/* #include <sys/queue.h> */
#include <stdio.h>
#include <stdlib.h> /* malloc, free */
#include <string.h> /* strcpy */
#include "llist.h"struct entry {char s[256];LIST_ENTRY(entry) entries;
};
LIST_HEAD(listhead, entry);int main()
{struct listhead head;struct entry* n1, * n2, * np;/* Initialize the list */LIST_INIT(&head);/* Insert at the head */n1 = malloc(sizeof(struct entry));strcpy_s(n1->s, 256, "line#1");LIST_INSERT_HEAD(&head, n1, entries);/* Insert after */n2 = malloc(sizeof(struct entry));strcpy_s(n2->s, 256, "line#2");LIST_INSERT_AFTER(n1, n2, entries);struct entry* n3;n3 = malloc(sizeof(struct entry));strcpy_s(n3->s, 256, "line#3");LIST_INSERT_BEFORE(n2, n3, entries);LIST_FOREACH(np, &head, entries) {printf("%s\n", np->s);}LIST_REMOVE(n2, entries);free(n2);LIST_FOREACH(np, &head, entries) {printf("%s\n", np->s);}/* Destroy */while (LIST_FIRST(&head) != NULL) {LIST_REMOVE(LIST_FIRST(&head), entries);free(LIST_FIRST(&head));}return 0;
}

选择最外层的CMakeLists.txt 右键Build

CMakePresets.json

{"version": 3,"configurePresets": [{"name": "windows-base","hidden": true,"generator": "Ninja","binaryDir": "${sourceDir}/out/build/${presetName}","installDir": "${sourceDir}/out/install/${presetName}","cacheVariables": {"CMAKE_C_COMPILER": "cl.exe","CMAKE_CXX_COMPILER": "cl.exe"},"condition": {"type": "equals","lhs": "${hostSystemName}","rhs": "Windows"}},{"name": "x64-debug","displayName": "x64 Debug","inherits": "windows-base","architecture": {"value": "x64","strategy": "external"},"cacheVariables": {"CMAKE_BUILD_TYPE": "Debug"}},{"name": "x64-release","displayName": "x64 Release","inherits": "x64-debug","cacheVariables": {"CMAKE_BUILD_TYPE": "Release"}},{"name": "x86-debug","displayName": "x86 Debug","inherits": "windows-base","architecture": {"value": "x86","strategy": "external"},"cacheVariables": {"CMAKE_BUILD_TYPE": "Debug"}},{"name": "x86-release","displayName": "x86 Release","inherits": "x86-debug","cacheVariables": {"CMAKE_BUILD_TYPE": "Release"}},{"name": "linux-debug","displayName": "Linux Debug","generator": "Ninja","binaryDir": "${sourceDir}/out/build/${presetName}","installDir": "${sourceDir}/out/install/${presetName}","cacheVariables": {"CMAKE_BUILD_TYPE": "Debug"},"condition": {"type": "equals","lhs": "${hostSystemName}","rhs": "Linux"},"vendor": {"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"}}},{"name": "macos-debug","displayName": "macOS Debug","generator": "Ninja","binaryDir": "${sourceDir}/out/build/${presetName}","installDir": "${sourceDir}/out/install/${presetName}","cacheVariables": {"CMAKE_BUILD_TYPE": "Debug"},"condition": {"type": "equals","lhs": "${hostSystemName}","rhs": "Darwin"},"vendor": {"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"}}}]
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/116299.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Linux】系统编程线程读写者模式(C++)

目录 一、读写锁 二、读写锁接口 【2.1】设置读写优先 【2.2】初始化 【2.3】销毁 【2.4】加读锁 【2.5】加写锁 【2.6】解锁 三、读写锁实例 一、读写锁 在编写多线程的时候&#xff0c;有一种情况是十分常见的。那就是&#xff0c;有些公共数据修改的机会比较少。相…

14:00面试,14:06就出来了,问的问题过于变态了。。。

从小厂出来&#xff0c;没想到在另一家公司又寄了。 到这家公司开始上班&#xff0c;加班是每天必不可少的&#xff0c;看在钱给的比较多的份上&#xff0c;就不太计较了。没想到5月一纸通知&#xff0c;所有人不准加班&#xff0c;加班费不仅没有了&#xff0c;薪资还要降40%…

使用 docker-compose 构建你的项目

使用 docker-compose 构建你的项目 1. Docker1.1 安装1.2 docker-compose 2 准备项目2.1 初始化一个node项目4. 准备一个 Dockerfile 文件5. 构建镜像 3.docker-compose构建3.1 配置docker-compose.yml 文件3.2 编排多个服务 重新构建镜像 --force-rm 删除构建过程中的临时容器…

AI-Chat,一款集全网ai功能的应用(附下载链接)

AI-Chat是一款综合性的聊天机器人&#xff0c;集成了多种先进的模型和功能。它采用了GPT4.0、联网版GPT和清华模型等多种模型&#xff0c;使得其具备更强大的语言处理能力。同时&#xff0c;AI-Chat还融合了AI绘画模型&#xff0c;例如Stable Diffusion绘画、文生图、图生图、艺…

硬件知识积累 网口接口 百兆,千兆,万兆 接口介绍与定义 (RJ45 --简单介绍)

1. 百兆网口 1.1百兆网的定义 百兆网的意思是是100Mb/S&#xff0c;中文叫做100兆位/秒。 1.2百兆网口的常用连接器 1.1.1 一般百兆网口的连接器一般是RJ45 下面是 实物图&#xff0c; 原理图&#xff0c;封装图。 1.3 百兆网口连接线的介绍 1.3.1 百兆需要使用的线的定义 百…

Flowable主要子流程介绍

1. 内嵌子流程 &#xff08;1&#xff09;说明 内嵌子流程又叫嵌入式子流程&#xff0c;它是一个可以包含其它活动、分支、事件&#xff0c;等的活动。我们通常意义上说的子流程通常就是指的内嵌子流程&#xff0c;它表现为将一个流程&#xff08;子流程&#xff09;定…

十四、流式编程(2)

本章概要 中间操作 跟踪和调试流元素排序移除元素应用函数到元素在 map() 中组合流 中间操作 中间操作用于从一个流中获取对象&#xff0c;并将对象作为另一个流从后端输出&#xff0c;以连接到其他操作。 跟踪和调试 peek() 操作的目的是帮助调试。它允许你无修改地查看…

Learn Prompt- Midjourney 图片生成:基本设置和预设

/settings指令为模型版本、样式值、质量值和升级器版本等常用选项提供切换按钮。 备注 添加到提示末尾的参数将覆盖/settings中的设置。 模型版本​ 1️⃣ MJ Version 12️⃣ MJ Version 23️⃣ MJ Version 34️⃣ MJ Version 45️⃣ MJ Version 5&#x1f308; Niji Mode&a…

数据结构与算法-时间复杂度与空间复杂度

数据结构与算法 &#x1f388;1.概论&#x1f52d;1.1什么是数据结构&#xff1f;&#x1f52d;1.2什么是算法&#xff1f; &#x1f388;2.算法效率&#x1f52d;2.1如何衡量一个算法的好坏&#xff1f;&#x1f52d;2.2算法的复杂度&#x1f52d;2.3时间复杂度&#x1f4d6;2…

Swift SwiftUI 隐藏键盘

如果仅支持 iOS 15 及更高版本&#xff0c;则可以通过聚焦和取消聚焦来激活和关闭文本字段的键盘。 在最简单的形式中&#xff0c;这是使用 FocusState 属性包装器和 focusable() 修饰符完成的-第一个存储一个布尔值&#xff0c;用于跟踪第二个当前是否被聚焦。 Code struct C…

分类预测 | MATLAB实现WOA-CNN-GRU-Attention数据分类预测

分类预测 | MATLAB实现WOA-CNN-GRU-Attention数据分类预测 目录 分类预测 | MATLAB实现WOA-CNN-GRU-Attention数据分类预测分类效果基本描述模型描述程序设计参考资料 分类效果 基本描述 1.MATLAB实现WOA-CNN-GRU-Attention数据分类预测&#xff0c;运行环境Matlab2021b及以上&…

使用python处理MNIST数据集

文章目录 一. MNIST数据集1.1 什么是MNIST数据集1.2MNIST数据集文件格式1.3使用python访问MNIST数据集文件内容 附录程序源码 一. MNIST数据集 1.1 什么是MNIST数据集 MNIST数据集是入门机器学习/识别模式的最经典数据集之一。最早于1998年Yan Lecun在论文:[Gradient-based l…