/**************************************************************************
* 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.data.dll的注册问题

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

  3. PHP 笔记一(systax/variables/echo/print/Data Type)

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

  4. SQL Server error "Xml data type is not supported in distributed queries" and workaround for it

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

  5. 数据结构(C++)之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 <----> java data type (数值)

    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++数据类型(data type)介绍

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

随机推荐

  1. C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\Team Foundation\4.0\Cache\VersionControl.config is not valid and cannot be loaded.

    Recently, we experienced a strange problem with TFS 2010. We spent a few days before we figured it o ...

  2. 2015年4月 15款免费jQuery插件

    点这里 We have collected for you the 15 fresh, free and handy jQuery plugins that will help to speed up ...

  3. java cookie

    public static void AddCookie(HttpServletResponse response, String key, String value) { Cookie cookie ...

  4. BZOJ 3224: Tyvj 1728 普通平衡树 vector

    3224: Tyvj 1728 普通平衡树 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除 ...

  5. Assembly(c#中简单说明[转]

    什么是Assembly(程序集)?Assembly是一个包含来程序的名称,版本号,自我描述,文件关联关系和文件位置等信息的一个集合.在.net框架中通过Assembly类来支持,该类位于System. ...

  6. Android应用的核心基础

    Android4开发入门经典 之 第二部分:Android应用的核心基础 Android应用中的组件 Application Components Android应用中最主要的组件是: 1:Activ ...

  7. jquery easyUi 配置默认页码

    jquery easyUI用pagenation 属性如果修改其默认加载页面显示,配置该怎样写? 注意区分datagrid的pagenation分页的区别,代码如下. if ($.fn.paginat ...

  8. MySQL定义外键的方法

    MySQL定义外键的方法是每个学习MySQL的人都需要掌握的知识,下文就对MySQL定义外键的语句写法进行了详细的阐述,供您参考. 外键为MySQL带来了诸多的好处,下面就为您介绍MySQL定义外键的 ...

  9. Spring框架学习之第8节

    <bean id=”foo” class=”…Foo”> <property name=”属性”> <!—第一方法引用--> <ref bean=”bean对 ...

  10. Emmet语法介绍

    例子: (div+p#test>span.test2.test3)*5+p[name="hello"]>div.test4^a*5 <div></di ...