当前位置: 首页 > news >正文

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.lryc.cn/news/173864.html

相关文章:

  • 【机器学习】详解回归(Regression)
  • mac 配置 httpd nginx php-fpm 详细记录 已解决
  • Angular 项目升级需要注意什么?
  • 开发高性能知识付费平台:关键技术策略
  • python图像匹配:如何使用Python进行图像匹配
  • R语言绘制PCA双标图、碎石图、变量载荷图和变量贡献图
  • Jolokia 笔记 (Kafka/start/stop)
  • Qt5开发及实例V2.0-第十九章-Qt.QML编程基础
  • 固定开发板的ifconfig的IP地址
  • 停车场系统源码
  • R语言贝叶斯MCMC:GLM逻辑回归、Rstan线性回归、Metropolis Hastings与Gibbs采样算法实例...
  • 若依前后端分离如何解决匿名注解启动报错?
  • Spring面试题4:面试官:说一说Spring由哪些模块组成?说一说JDBC和DAO之间的联系和区别?
  • 【再识C进阶3(上)】详细地认识字符串函数、进行模拟字符串函数以及拓展内容
  • docker启动mysql8目录挂载改动
  • CHATGPT中国免费网页版有哪些-CHATGPT中文版网页
  • docker network create命令
  • 4G版本云音响设置教程腾讯云平台版本
  • Grafana离线安装部署以及插件安装
  • 非独立随机变量的概率上界估计
  • 常见电子仪器及其用途
  • 配置测试ip、正式ip、本地ip
  • Linux 系统移植(一)-- 系统组成
  • 利用git的贮藏功能
  • 第52节:cesium 3DTiles模型特效+选中高亮(含源码+视频)
  • day03_基础语法
  • 数据结构与算法-时间复杂度与空间复杂度
  • 数组的去重
  • Electron自动化测试技术选型调研
  • 微服务学习(九):安装OpenOffice