Linux C double linked for any data type

时间:2022-09-04 16:14:34
/**************************************************************************
* Linux C double linked for any data type
* 声明:
* 提供一种双链接口,可以保存保存任何类型的数据。
*
* 2015-12-25 晴 深圳 南山平山村 曾剑锋
*************************************************************************/ \\\\\\\\-*- 目录 -*-////////
| 一、cat double_linked.h
| 二、cat myerror.h
| 三、cat doulbe_linked.c
| 四、cat main.c
--------------------------- 一、cat double_linked.h
#ifndef __DOUBLE_LINK_H__
#define __DOUBLE_LINK_H__ #include <stdio.h>
#include <stdlib.h>
#include "myerror.h"
#include <string.h> typedef struct DOUBLE_LINKED{
void* datap;
int size;
struct DOUBLE_LINKED* pre;
struct DOUBLE_LINKED* next;
} double_linked; typedef void(*double_linked_print)(double_linked* data);
typedef int(*double_linked_delete)(void* data, double_linked* linked_data); void init_double_linked_node(double_linked* node);
int check_data_size(void* data, int size);
void empty_double_linked(double_linked* header);
void _delete_double_linked_node(double_linked* pre, double_linked* next);
void delete_double_linked_node(double_linked* node);
/**
* create a double linked for user
*/
double_linked* create_double_linked(void* data, int size);
void _add_double_linked(double_linked* current, double_linked* pre, double_linked* next);
/**
* top add doulbe linked
*/
int top_add_double_linked(void* data, int size, double_linked* header);
/**
* tail add double linked
*/
int tail_add_double_linked(void* data, int size, double_linked* header);
/**
* print all data in double linked
*/
void print_double_linked(double_linked* header, double_linked_print func);
/**
* empty all data in double linked
*/
void empty_double_linked(double_linked* header);
/**
* delete a element in the double linked
*/
void delete_double_linked(void* data, double_linked* header, double_linked_delete func);
/**
* free all data in the double linked
*/
void free_double_linked(double_linked* header);
#endif 二、cat myerror.h
#ifndef _MYERROR_H
#define _MYERROR_H //serial error 1
// error 2
// warning 3
// information 4 #define DEBUG1(...) fprintf(stderr,"SERI ERR: " __VA_ARGS__);
#define DEBUG2(...) fprintf(stderr,"ERR: " __VA_ARGS__);
#define DEBUG3(...) fprintf(stdout,"WARNING: " __VA_ARGS__);
#define DEBUG4(...) fprintf(stdout,"INFORMATION: " __VA_ARGS__);
#define DEBUG() fprintf(stdout,"\033[32mbug at func( %s ) : %d\033[0m\n", __func__, __LINE__); #define ERR(lever ,con, ret , ...) \
do \
{ \
if(con) \
{ \
DEBUG##lever(__VA_ARGS__) \
ret; \
} \
}while() #endif //_MYERROR_H 三、cat doulbe_linked.c
#include "double_linked.h"
void init_double_linked_node(double_linked* node){
node->next = node;
node->pre = node;
node->datap = NULL;
node->size = ;
} int check_data_size(void* data, int size){
return ((data != NULL) && (size > ));
}
/**
* create a double linked for user
*/
double_linked* create_double_linked(void* data, int size){
double_linked* header = malloc(sizeof(double_linked));
ERR(, NULL == header, goto err, "create double linked fail\n"); init_double_linked_node(header); if(check_data_size(data, size)){
void* new_data = malloc(size);
ERR(, NULL == new_data, goto err, "create double linked data fail\n");
memcpy(new_data, data, size);
header->size = size;
header->datap = new_data;
} return header;
err:
return NULL;
}
void _add_double_linked(double_linked* current, double_linked* pre, double_linked* next){
pre->next = current;
current->next = next;
next->pre = current;
current->pre = pre;
}
/**
* top add doulbe linked
*/
int top_add_double_linked(void* data, int size, double_linked* header){
if(check_data_size(data, size)){
double_linked* node = malloc(sizeof(double_linked));
ERR(, NULL == header, goto err, "create note fail.\n"); init_double_linked_node(node); void* new_data = malloc(size);
ERR(, NULL == new_data, goto err, "create note data fail.\n");
memcpy(new_data, data, size);
node->size = size;
node->datap = new_data;
_add_double_linked(node, header, header->next);
return ;
}
err:
return -;
}
/**
* tail add double linked
*/
int tail_add_double_linked(void* data, int size, double_linked* header){
if(check_data_size(data, size)){
double_linked* node = malloc(sizeof(double_linked));
ERR(, NULL == header, goto err, "create note fail.\n"); init_double_linked_node(node); void* new_data = malloc(size);
ERR(, NULL == new_data, goto err, "create note data fail.\n");
memcpy(new_data, data, size);
node->size = size;
node->datap = new_data;
_add_double_linked(node, header->pre, header);
return ;
}
err:
return -; }
/**
* print all data in double linked
*/
void print_double_linked(double_linked* header, double_linked_print func){
double_linked* tmp = header->next;
while(tmp != header){
func(tmp);
tmp = tmp->next;
}
}
void _delete_double_linked_node(double_linked* pre, double_linked* next){
pre->next = next;
next->pre = pre;
}
void delete_double_linked_node(double_linked* node){
_delete_double_linked_node(node->pre, node->next);
if(node->datap){
free(node->datap);
}
init_double_linked_node(node);
free(node);
}
/**
* empty all data in double linked
*/
void empty_double_linked(double_linked* header){
double_linked* tmp = header->next;
double_linked* pre = header;
while(tmp != header){
pre = tmp;
tmp = tmp->next;
delete_double_linked_node(pre);
}
init_double_linked_node(header);
if(header->datap)
free(header->datap);
header->datap = NULL;
header->size = ;
}
/**
* delete a element in the double linked
*/
void delete_double_linked(void* data, double_linked* header, double_linked_delete func){
double_linked* tmp = header->next;
double_linked* pre = header;
while(tmp != header){
pre = tmp;
tmp = tmp->next;
if(func(data, pre)){
delete_double_linked_node(pre);
}
}
}
void free_double_linked(double_linked* header){
double_linked* tmp = header->next;
double_linked* pre = header;
while(tmp != header){
pre = tmp;
tmp = tmp->next;
delete_double_linked_node(pre);
} init_double_linked_node(header);
if(header->datap)
free(header->datap);
header->datap = NULL;
header->size = ;
free(header);
} 四、cat main.c
#include "double_linked.h" #define NR(x) ((sizeof(x))/sizeof(x[0])) /**
* test struct
*/
typedef struct STUDENT{
int id;
int score;
}student; /**
* callback function
*/
void print(double_linked* node);
int delete(void* data, double_linked* node); int main(int argc, char** argv){
/**
* demo data
*/
student students[] = {
{,},
{,},
{,},
{,},
};
double_linked* header = create_double_linked(NULL, ); printf("--------------source--------------->\n");
int i = ;
for(i = ; i < NR(students); i++){
printf("student: id = %d, score = %d\n", students[i].id, students[i].score);
} printf("------------top add--------------->\n");
for(i = ; i < NR(students); i++){
top_add_double_linked(&students[i],sizeof(student), header);
}
print_double_linked(header, print); empty_double_linked(header); printf("-----------tail add-------------->\n");
for(i = ; i < NR(students); i++){
tail_add_double_linked(&students[i],sizeof(student), header);
}
print_double_linked(header, print); printf("-----------delete-------------->\n");
student stu = {,};
delete_double_linked(&stu, header, delete);
print_double_linked(header, print); printf("-------------free-------------->\n");
free_double_linked(header);
}
/**
* impelement of the client print
*/
void print(double_linked* node){
student* stu = (student*)(node->datap);
printf("student: id = %d, score = %d\n", stu->id, stu->score);
}
/**
* impelement of the client delete
*/
int delete(void* data, double_linked* node){
student* stu = (student*)(node->datap);
return (((student*)data)->id == stu->id) && (((student*)data)->score == stu->score);
}

Linux C double linked for any data type的更多相关文章

  1. Linux C single linked for any data type

    /************************************************************************** * Linux C single linked ...

  2. 关于Linux和Windows下部署mysql&period;data&period;dll的注册问题

    mysql ado.net connector下载地址: http://dev.mysql.com/downloads/connector/net/ 选择版本: Generally Available ...

  3. PHP 笔记一(systax&sol;variables&sol;echo&sol;print&sol;Data Type)

    PHP stands for "Hypertext Preprocessor" ,it is a server scripting language. What Can PHP D ...

  4. SQL Server error &quot&semi;Xml data type is not supported in distributed queries&quot&semi; and workaround for it

    Recently while working with data migration,got an error while running a following query where Server ...

  5. 数据结构&lpar;C&plus;&plus;&rpar;之Double Linked List实践

    //double linked list (type int),the position starts from 0 #include <iostream> using namespace ...

  6. 数据类型(data type)

    基本数据类型(primitive data type):字符型(2个字节),布尔型(一位),byte(1个字节),short(两个字节),int(4个字节),long(8个字节),float(2个字节 ...

  7. mysql data type &lt&semi;----&gt&semi; java data type &lpar;数值)

    https://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html +----------------------------+---- ...

  8. MySql and Oracle Data Type Mappings

    the default settings used by SQL Developer to convert data types from MySQL to Oracle. SQL Developer ...

  9. C&plus;&plus;数据类型(data type)介绍

    在编写程序时,数据类型(data type)定义了使用存储空间的(内存)的方式. 程序员通过定义数据类型(data type),告诉特定存储空间这里要存储的数据类型是什么,以及你即将操作他的方式.(注 ...

随机推荐

  1. JavaScript中有关数字的精确计算

    问题这样的: 37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) ,我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.08499999999998 怎么会 ...

  2. &lbrack;ActionScript 3&period;0&rsqb; 获取随机颜色

    import flash.display.MovieClip; import flash.geom.ColorTransform; //方法一 var red:Number = Math.random ...

  3. Shell 变量

    定义变量时,变量名不加美元符号($,PHP语言中变量需要),如: your_name="runoob.com" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一 ...

  4. ICDM 2007

    Language-Independent Set Expansion of Named Entities Using the Web. Chao Wang, Venu Satuluri, Sriniv ...

  5. POI使用:用poi接口不区分xls&sol;xlsx格式解析Excel文档&lpar;41种日期格式解析方法,5种公式结果类型解析方法,3种常用数值类型精度控制办法&rpar;

    一.使用poi解析excel文档 注:全部采用poi接口进行解析,不需要区分xls.xlsx格式,不需要判断文档类型. poi中的日期格式判断仅支持欧美日期习惯,对国内的日期格式并不支持判断,怎么办? ...

  6. Android 自定义ViewGroup手把手教你实现ArcMenu

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37567907 逛eoe发现这样的UI效果,感觉很不错,后来知道github上有这 ...

  7. web&commat;HTML重要标签详介绍&period;

    1.table标签<table border="1px " rules="groups" cellpadding="5px" cell ...

  8. &period;NetCore&lpar;四&rpar; 在Nginx部署

    ​本篇主要体验一下Nginx的使用,之前只接触过IIS. 一.Nginxa) ASP.NET Core内置了Kestrel服务器,但功能简单,主要用于SelfHost,正式运行还是要依赖IIS.Apa ...

  9. 计蒜客 31434 - 广场车神 - &lbrack;DP&plus;前缀和&rsqb;

    题目链接:https://nanti.jisuanke.com/t/31434 小 D 是一位著名的车手,他热衷于在广场上飙车.每年儿童节过后,小 D 都会在广场上举行一场别样的车技大赛. 小 D 所 ...

  10. Prometheus Node&lowbar;exporter 之 Network Netstat ICMP

    Network Netstat ICMP /proc/net/snmp 1. ICMP Errors 1 type: GraphUnit: shortLabel: Messages out (-) / ...