问题:
将下面的数据节点信息转换为链表结构,并遍历输出。要求根据type的值来决定val的类型。
type为1代表bool类型,2代表整形,3代表浮点型。无需解析文本,直接赋值形成节点即可。
代码:
list.c
#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
/***********************************************************************************
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