Linux C single linked for any data type
/**************************************************************************
* Linux C single linked for any data type
* 声明:
* 提供一种单链接口,可以保存保存任何类型的数据,有时候这种需求在
* 很多场合还是会用到的。
*
* 2015-7-5 晴 深圳 南山平山村 曾剑锋
*************************************************************************/ \\\\\\\\\-*- 目录 -*-/////////
| 一、cat single_linked.h
| 二、cat myerror.h
| 三、cat single_linked.c
| 四、cat main.c
\\\\\\\\\\\\\\\////////////// 一、cat single_linked.h
#ifndef __SINGLE_LINKED_H__
#define __SINGLE_LINKED_H__ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "myerror.h" typedef struct SINGLE_LINKED{
void* datap;
int size;
struct SINGLE_LINKED* next;
} single_linked; typedef void (*single_linked_print)(void* data);
typedef int (*single_linked_delete)(void* data, void* linked_delete);
/**
* you should giving a vilid data dan size for save
*/
int check_data_size(void *data, int size);
/**
* create a single linked header
*/
single_linked* create_single_linked(void* data, int size);
/**
* add data to single linked at top
*/
int top_add_single_linked(void* data, int size, single_linked* header);
/**
* add data to single linked at tail
*/
int tail_add_single_linked(void* data, int size, single_linked* header);
/**
* delete data in single linked
*/
int delete_single_linked(void* data, single_linked* header, single_linked_delete func);
/**
* print all data in single linked
*/
void print_single_linked(single_linked* header, single_linked_print func);
/**
* empty the single linked
*/
void empty_single_linked(single_linked* header);
/**
* free all data in single linked
*/
void free_single_linked(single_linked* header);
#endif //__SINGLE_LINKED_H__ 二、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 single_linked.c
#include "single_linked.h"
/**
* create a single linked as header
*/
single_linked* create_single_linked(void *data, int size){
single_linked* header = malloc(sizeof(single_linked));
ERR(, NULL == header, goto err; "create single linked header fail.\n");
header->next = NULL;
header->datap = NULL;
header->size = ; if(check_data_size(data,size)){
void* new_data = malloc(size);
ERR(, NULL == new_data, goto new_data_err; "create single linked header with data fail.\n");
memcpy(new_data, data, size);
header->size = size;
header->datap = new_data;
}
return header;
new_data_err:
free(header);
err:
return NULL;
} int check_data_size(void* data, int size){
return ((data != NULL) && (size > ));
} int top_add_single_linked(void* data, int size, single_linked* header){
ERR(, NULL == header, return -, "the header was NULL in top add single linked\n");
single_linked* new_node = NULL; if(check_data_size(data, size)){
single_linked* new_node = malloc(sizeof(single_linked));
ERR(, NULL == new_node, goto err; "top add malloc node fail.\n");
new_node->next = NULL;
new_node->datap = NULL;
new_node->size = ; void* new_data = malloc(size);
ERR(, NULL == new_data, goto err1; "top add malloc data fail.\n");
memcpy(new_data, data, size);
new_node->size = size;
new_node->datap = new_data; new_node->next = header->next;
/**
* I had pazzled at this point with (header->next = new_node->next)
*/
header->next = new_node; }
return ;
err1:
free(new_node);
err:
return -;
} /**
* add data to single linked at tail
*/
int tail_add_single_linked(void* data, int size, single_linked* header){
ERR(, NULL == header, return -, "the header was NULL in tail add single linked\n");
single_linked* new_node = NULL; if(check_data_size(data, size)){
single_linked* new_node = malloc(sizeof(single_linked));
ERR(, NULL == new_node, goto err; "top add malloc node fail.\n");
new_node->next = NULL;
new_node->datap = NULL;
new_node->size = ; void* new_data = malloc(size);
ERR(, NULL == new_data, goto err1; "top add malloc data fail.\n");
memcpy(new_data, data, size);
new_node->size = size;
new_node->datap = new_data; //new_node->next = header->next;
while(header->next)
header = header->next;
/**
* I had pazzled at this point with (header->next = new_node->next)
*/
header->next = new_node;
}
return ;
err1:
free(new_node);
err:
return -;
} /**
* delete data in single linked
*/
int delete_single_linked(void* data, single_linked* header, single_linked_delete func){
ERR(, ((NULL == data) || (NULL == header) || (NULL == func)), return; "you shouldn't giving NULL for delete single linkde\n"); single_linked* tmp = header->next;
single_linked* pre = header;
while(tmp){
/**
* you should giving a return value to decision delete this or not
*/
if(func(data, tmp->datap)){
pre->next = tmp->next;
tmp->next = NULL;
free(tmp->datap);
free(tmp);
tmp = pre->next;
}else{
pre = tmp;
tmp = tmp->next;
}
}
return ;
} /**
* print all data in single linked
*/
void print_single_linked(single_linked* header, single_linked_print func){
ERR(, ((NULL == header) || (NULL == func)), return; "you shouldn't giving NULL for print single linkde\n");
single_linked* tmp = header->next;
while(tmp){
func(tmp);
tmp = tmp->next;
}
} /**
* free all data in single linked
*/
void free_single_linked(single_linked* header){
ERR(, NULL == header, return; "free a header with NULL\n"); single_linked* tmp = header->next;
single_linked* pre = header->next;
while(tmp){
pre = tmp;
tmp = tmp->next;
free(pre->datap);
free(pre);
} if(header->datap != NULL){
free(header->datap);
header->datap = NULL;
}
header->next = NULL;
free(header);
header == NULL;
} void empty_single_linked(single_linked* header){
ERR(, NULL == header, return, "empty header was NULL\n"); single_linked* tmp = header->next;
single_linked* pre = header->next;
while(tmp){
pre = tmp;
tmp = tmp->next;
free(pre->datap);
free(pre);
} if(header->datap != NULL){
free(header->datap);
header->datap = NULL;
}
header->next = NULL;
header->size = ;
} 四、cat main.c
#include "single_linked.h" #define NR(x) ((sizeof(x))/sizeof(x[0])) /**
* test struct
*/
typedef struct STUDENT{
int id;
int score;
}student; /**
* callback function
*/
void print(void* data);
int delete(void* data, void* linked_data); int main(int argc, char** argv){
/**
* demo data
*/
student students[] = {
{,},
{,},
{,},
{,},
}; single_linked* header = create_single_linked(NULL, ); int i = ;
printf("--------------source------------------>\n");
for(i = ; i < NR(students); i++){
printf("student: id = %d, score = %d \n", students[i].id, students[i].score);
} printf("--------------tail add---------------->\n");
for(i = ; i < NR(students); i++){
//top_add_single_linked(&students[i], sizeof(student), header);
tail_add_single_linked(&students[i], sizeof(student), header);
}
print_single_linked(header, print); empty_single_linked(header); printf("--------------top add---------------->\n");
for(i = ; i < NR(students); i++){
//top_add_single_linked(&students[i], sizeof(student), header);
top_add_single_linked(&students[i], sizeof(student), header);
}
print_single_linked(header, print); printf("--------------delete---------------->\n");
student stu = {,};
delete_single_linked(&stu, header, delete);
print_single_linked(header, print); printf("---------------free----------------->\n");
free_single_linked(header);
} void print(void* data){
student* stu = (((single_linked*)data)->datap);
printf("student: id = %d, score = %d \n", stu->id, stu->score);
} int delete(void* data, void* linked_data){
return (((student*)data)->id == ((student*)linked_data)->id) && (((student*)data)->score == ((student*)linked_data)->score);
}
Linux C single linked for any data type的更多相关文章
- Linux C double linked for any data type
/************************************************************************** * Linux C double linked ...
- 关于Linux和Windows下部署mysql.data.dll的注册问题
mysql ado.net connector下载地址: http://dev.mysql.com/downloads/connector/net/ 选择版本: Generally Available ...
- Extended Data Type Properties [AX 2012]
Extended Data Type Properties [AX 2012] This topic has not yet been rated - Rate this topic Updated: ...
- 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 ...
- PHP 笔记一(systax/variables/echo/print/Data Type)
PHP stands for "Hypertext Preprocessor" ,it is a server scripting language. What Can PHP D ...
- JAVA 1.2(原生数据类型 Primitive Data Type)
1. Java的数据类型分为2类 >> 原生数据类型(primitive data type) >> 引用数据类型(reference data type) 3. 常量和变量 ...
- salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解
建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schema Builder查看表结构以及多表之间的关联关系,可以登录后点击setup在左侧搜索框输入schema ...
- The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
刚刚有在程序中,传递一个空值至MS SQL Server数据库,这个值的数据类型为DATETIME执行时,它却发生了如标题提示的异常:The conversion of a varchar data ...
- XML Data Type Methods(一)
XML Data Type Methods(一) /*XML Data Type Methods: 1.The query('XQuery') method retrieves(vt.检索,重新得到) ...
随机推荐
- BeautifulSoup中的find,find_all
1.一般来说,为了找到BeautifulSoup对象内任何第一个标签入口,使用find()方法. 以上代码是一个生态金字塔的简单展示,为了找到第一生产者,第一消费者或第二消费者,可以使用Beautif ...
- VSS_01
1.设置 工作目录 右击 CSDRGis --> Set Working Folder.. --> D:\CSDRGis 2.下载 源码 右击 CSDRGis --> Get Las ...
- json 与 string 互转
1. json字符串转换成json对象 js: JSON.parse('{"age":"28"}') eval('(' + '{"age": ...
- Android 实现文件上传功能(upload)
文 件上传在B/S应用中是一种十分常见的功能,那么在Android平台下是否可以实现像B/S那样的文件上传功能呢?答案是肯定的.下面是一个模拟网站程 序上传文件的例子.这里只写出了Android部分的 ...
- crond 的注意点
本来在控制台调试的好好的程序,一通过crond运行, 就执行不了. 一般需要注意: 1) 环境变量 当前目录 会变成用户的Home 2) /var/log/messages 可以看出 crond 调 ...
- ThreadLocal实现原理
一.ThreadLocal介绍 这是一个线程的局部变量.也就是说,只有当前线程可以访问.既然是只有当前线程可以访问的数据,自然是线程安全的. 为每一个线程分配不同的对象,需要在应用 ...
- JavaScript--Array; Array.prototype
Array: Javascritp中Array的方法: findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引.否则返回-1. find() 方法返回数组中满足提供的测试函数的第一个 ...
- HDOJ-1124 Factorial 数论
题意哇:求N!末尾多少个0. 很容易想到转化为求N!中5因子的个数.但是从数据范围来看必然不可能一个一个算出来. 所以这里借用数论的一个知识. 如果p是素数,那么n!中p因子的个数可以表示为1-n中整 ...
- MySql 定时完成备份
<?php /*定时备份数据库文件*/ //设置时区 date_default_timezone_set('PRC'); //创建目录 $dirname = 'e:/mysql_dump/'.d ...
- python-day16--内置函数
内置函数操作 #!usr/bin/env python # -*- coding:utf-8 -*- # 1.locals()和globals() # def func(): # x=1 # y=2 ...