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.检索,重新得到) ...
随机推荐
- java基础深入解析基本类型
一.基本类型的简介 基本类型的两条准则: Java中,如果对整数不指定类型,默认时int类型,对小数不指定类型,默认是double类型. 基本类型由小到大,可以自动转换,但是由大到小,则需要强制类型转 ...
- 《剑指offer》第十三题(机器人的运动范围)
// 面试题:机器人的运动范围 // 题目:地上有一个m行n列的方格.一个机器人从坐标(0, 0)的格子开始移动,它 // 每一次可以向左.右.上.下移动一格,但不能进入行坐标和列坐标的数位之和 // ...
- 2018焦作网络赛Mathematical Curse
题意:开始有个数k,有个数组和几个运算符.遍历数组的过程中花费一个运算符和数组当前元素运算.运算符必须按顺序花费,并且最后要花费完.问得到最大结果. 用maxv[x][y]记录到第x个元素,用完了第y ...
- python-day6---while循环
# while 条件:# 循环体的代码1# 循环体的代码2# 循环体的代码3# count=0# while count < 10:# print(count)# count+=1 # whil ...
- Oracle 账户锁定问题解决办法
1 打开 SQL PLUS 2 登录数据库 3 输入 conn/as sysdba; 4 输入 alter user 数据库名 account unlock;
- iOS UI-微博案例(通过代码自定义Cell)
一.Model BWWeiBo数据模型 #import <Foundation/Foundation.h> @interface BWWeiBo : NSObject @property ...
- IOS UI总结
一.UIView常见属性 1.frame 位置和尺寸(以父控件的左上角为原点(0,0)) 2.center 中点(以父控件的左上角为原点(0,0)) 3.bounds 位置和尺寸(以自己的左上角为 ...
- Oracle12c中性能优化&功能增强新特性之重大突破——内存列存储新特性
内存列存储(IM column store) 是Oracle12.1.0.2版本的主要特点.该特点允许列,表,分区和物化视图在内存中以列格式存储,而不是通常的行格式.数据存在内存中的好处显而易见,而列 ...
- SMTP 通过 ssh 通道发送垃圾邮件
通过SSH隧道传输SMTP 根据设计,我们不允许校外机器使用我们的SMTP服务器.如果我们允许它,我们将允许任何和所有使用我们的SMTP服务器来分发垃圾邮件.但是也可以通过我们的SMTP服务器发送邮件 ...
- js设计模式-观察者模式
定义: 观察者模式又叫发布订阅模式,它定义了对象间的一种一对多的依赖关系.观察者模式让两个对象松耦合地联系在一起,虽然不太清楚彼此的细节,但这不影响他们之间的互相通信. 思路 定义一个对象,在对象中实 ...