内核链表 例题 C语言实现

问题:

将下面的数据节点信息转换为链表结构,并遍历输出。要求根据type的值来决定val的类型。

type为1代表bool类型,2代表整形,3代表浮点型。无需解析文本,直接赋值形成节点即可。


代码:

list.c
cpp 复制代码
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include "list.h"

/*定义Value共用体*/
typedef union{
    bool bool_val;
    int int_val;
    float float_val;
} Value;

/*定义Data结构体*/
typedef struct Data{
    int key;
    int type;
    Value val;
    struct list_head list;
}Data;
/*初始化结点*/
Data create_node(int key,int type,char* val_str){
    Data node;

    node.key = key;
    node.type = type;
    
    /*根据类型存数据*/
    switch(type){
    case 1:
        node.val.bool_val = atoi(val_str) != 0;
        break;
    case 2:
        node.val.int_val = atoi(val_str);
        break;
    case 3:
        node.val.float_val = atof(val_str);
        break;
    default:
        printf("error\n");
        exit(1);
    }
    node.list.next = NULL;
    node.list.prev = NULL;
    return node;
}
/*输出结点*/
void print_node(Data *tmp){
    switch(tmp->type){
        case 1:
            printf("key = %d,val = %s\n",tmp->key,(tmp->val.bool_val == 0 ? "false":"true"));;
            break;
        case 2:
            printf("key = %d,val = %d\n",tmp->key,tmp->val.int_val);
            break;
        case 3:
            printf("key = %d,val = %.2f\n",tmp->key,tmp->val.float_val);
            break;
        default:
            printf("print error\n");
            exit(1);
        }
}

int main(){
    struct list_head head;
    INIT_LIST_HEAD(&head);

    Data data1,data2,data3;
    /*初始化结点*/
    data1 = create_node(1,2,"10");
    data2 = create_node(2,1,"0");
    data3 = create_node(3,3,"22.5");

    /*把结点加入链表*/
    list_add(&data1.list, &head);
    list_add(&data2.list, &head);
    list_add(&data3.list, &head);

    struct list_head *pos;
    Data *tmp;

    printf("init list\n");

    /*遍历链表*/
    list_for_each(pos, &head){
        tmp = list_entry(pos, Data, list); // 获取当前结点
        print_node(tmp); // 输出结点
    }
    printf("\n");

    puts("del last\n");
    pos = get_last(&head);
    list_del(pos); // 删除末尾结点

    printf("after del:\n");

    /*遍历链表*/
    list_for_each(pos, &head){
        tmp = list_entry(pos, Data, list); // 获取当前结点
        print_node(tmp); // 输出结点
    }
    printf("\n");
}
list.h
cpp 复制代码
/***********************************************************************************
Copy right:	Coffee Tech.
Author:			wanghan
Version:		V1.0
Date:			2025-1
Description:	从linux内核抽出来链表,可以做通用链表使用,可保留
***********************************************************************************/

#ifndef _LIST_H
#define _LIST_H

//定义核心链表结构
struct list_head
{
	struct list_head *next, *prev;
};

//链表初始化
static inline void INIT_LIST_HEAD(struct list_head *list)
{
	list->next = list;
	list->prev = list;
}

//插入结点
static inline void __list_add(struct list_head *new_list,
							  struct list_head *prev, struct list_head *next)
{
	next->prev = new_list;
	new_list->next = next;
	new_list->prev = prev;
	prev->next = new_list;
}

//在链表头部插入
static inline void list_add(struct list_head *new_list, struct list_head *head)
{
	__list_add(new_list, head, head->next);
}

//尾部插入结点
static inline void list_add_tail(struct list_head *new_list, struct list_head *head)
{
	__list_add(new_list, head->prev, head);
}

static inline void __list_del(struct list_head *prev, struct list_head *next)
{
	next->prev = prev;
	prev->next = next;
}

//删除任意结点
static inline void list_del(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
}

//是否为空
static inline int list_empty(const struct list_head *head)
{
	return head->next == head;
}

//得到第一个结点
static inline struct list_head *get_first(const struct list_head *head)
{
	return head->next;
}

//得到最后一个结点
static inline struct list_head *get_last(const struct list_head *head)
{
	return head->prev;
}

static inline void __list_splice(const struct list_head *list,
								 struct list_head *prev,
								 struct list_head *next)
{
	struct list_head *first = list->next;
	struct list_head *last = list->prev;

	first->prev = prev;
	prev->next = first;

	last->next = next;
	next->prev = last;
}

/**
 * list_splice - join two lists, this is designed for stacks
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 */
static inline void list_splice(const struct list_head *list,
							   struct list_head *head)
{
	if (!list_empty(list))
		__list_splice(list, head, head->next);
}

/**
 * list_splice_tail - join two lists, each list being a queue
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 */
static inline void list_splice_tail(struct list_head *list,
									struct list_head *head)
{
	if (!list_empty(list))
		__list_splice(list, head->prev, head);
}

//后序(指针向后走)遍历链表
#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)

#define list_for_each_safe(pos, n, head) \
	for (pos = (head)->next, n = pos->next; pos != (head); pos = n, n = pos->next)

//前序(指针向前走)遍历链表
#define list_for_each_prev(pos, head) \
	for (pos = (head)->prev; pos != (head); pos = pos->prev)

#define offsetof_list(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER)

#define list_entry(ptr, type, member) ({			\
    const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
    (type *)( (char *)__mptr - offsetof_list(type,member) ); })

#endif

输出:

相关推荐
Tummer836312 分钟前
C++四种类型转换:static_cast、 dynamic_cast const cast、 reinterpret_cast
c++
sz66cm34 分钟前
Linux基础 -- 在内存中使用chroot修复eMMC
linux·服务器
小刘不想改BUG44 分钟前
LeetCode LCR 015. 找到字符串中所有字母异位词 (Java)
linux·算法·leetcode
范纹杉想快点毕业1 小时前
以项目的方式学QT开发(三)——超详细讲解(120000多字详细讲解,涵盖qt大量知识)逐步更新!
c语言·开发语言·c++·qt·mysql·算法·命令模式
敲代码的瓦龙1 小时前
STL?list!!!
c语言·开发语言·数据结构·c++·windows·list
程序员莫小特1 小时前
【GESP真题解析】第 20 集 GESP 二级 2025 年 3 月编程题 1:等差矩阵
c语言·数据结构·c++·算法·青少年编程·矩阵
机器视觉知识推荐、就业指导2 小时前
Qt/C++面试【速通笔记九】—视图框架机制
c++·笔记·qt
s_little_monster2 小时前
【Linux】socket网络编程基础
linux·运维·网络·笔记·学习·php·学习方法
FL171713143 小时前
UR5e机器人Matlab仿真
linux·matlab·机器人
君鼎7 小时前
C++设计模式——单例模式
c++·单例模式·设计模式