C 语言设计模式(二) 单链表操作
2008-11-26 22:56
225 查看
需要单独实现 :
void list_element_destroy(struct list *ele)
int list_element_cmp(void* l1 , void* l2)
这两个函数,就可以构成一个通用的list,操作代码
struct list
{
void * datas;
struct list * next;
};
struct list * g_first = NULL;
struct list * create_list_ele(void * data)
{
struct list * lt = (struct list *) malloc( sizeof(struct list));
if(lt) {
lt->next = NULL;
lt->datas = data;
}
return lt;
}
void list_element_destroy(struct list *ele)
{
if(ele) {
//destroy ele->data;
free(ele);
}
}
void list_destroy(struct list * lt)
{
struct list *tmp = lt, *t;
//wirte_lock(list)
while(tmp) {
t = tmp;
tmp = t->next;
list_element_destroy(t);
}
//write_unlock(list)
return ;
}
int list_element_cmp(void* l1 , void* l2)
{
return 0;
}
struct list ** list_find_element( void * data)
{
struct list **p;
for(p = &g_first ; *p ; p = &(*p)->next)
if(list_element_cmp((*p)->datas, data) == 0) {
break;
}
return p;
}
int list_add_element(struct list * ele)
{
int ret = 0 ;
struct list **p;
//write_lock(list);
p = list_find_element(ele->datas);
if(*p) {
ret = -1 ; /* element exist already */
} else {
*p = ele;
}
//write_unlock(list);
return ret;
}
int list_remove_element(struct list *ele)
{
struct list ** tmp;
//write_lock(list)
tmp = &g_first;
while(*tmp) {
if(*tmp == ele) {
*tmp = ele->next;
ele->next = NULL;
//write_unlock
return 0;
}
tmp = &(*tmp)->next;
}
//wirte_unlock
return -1; /* not found */
}
int list_element_idx(struct list * ele)
{
struct list * tmp;
int ret = -1, index = 0;
//read_lock(list)
for(tmp = g_first ; tmp ; tmp = tmp->next, index ++) {
if(list_element_cmp(tmp,ele) == 0) {
ret = index;
break;
}
}
//read_unlock(list)
return ret;
}
void list_element_destroy(struct list *ele)
int list_element_cmp(void* l1 , void* l2)
这两个函数,就可以构成一个通用的list,操作代码
struct list
{
void * datas;
struct list * next;
};
struct list * g_first = NULL;
struct list * create_list_ele(void * data)
{
struct list * lt = (struct list *) malloc( sizeof(struct list));
if(lt) {
lt->next = NULL;
lt->datas = data;
}
return lt;
}
void list_element_destroy(struct list *ele)
{
if(ele) {
//destroy ele->data;
free(ele);
}
}
void list_destroy(struct list * lt)
{
struct list *tmp = lt, *t;
//wirte_lock(list)
while(tmp) {
t = tmp;
tmp = t->next;
list_element_destroy(t);
}
//write_unlock(list)
return ;
}
int list_element_cmp(void* l1 , void* l2)
{
return 0;
}
struct list ** list_find_element( void * data)
{
struct list **p;
for(p = &g_first ; *p ; p = &(*p)->next)
if(list_element_cmp((*p)->datas, data) == 0) {
break;
}
return p;
}
int list_add_element(struct list * ele)
{
int ret = 0 ;
struct list **p;
//write_lock(list);
p = list_find_element(ele->datas);
if(*p) {
ret = -1 ; /* element exist already */
} else {
*p = ele;
}
//write_unlock(list);
return ret;
}
int list_remove_element(struct list *ele)
{
struct list ** tmp;
//write_lock(list)
tmp = &g_first;
while(*tmp) {
if(*tmp == ele) {
*tmp = ele->next;
ele->next = NULL;
//write_unlock
return 0;
}
tmp = &(*tmp)->next;
}
//wirte_unlock
return -1; /* not found */
}
int list_element_idx(struct list * ele)
{
struct list * tmp;
int ret = -1, index = 0;
//read_lock(list)
for(tmp = g_first ; tmp ; tmp = tmp->next, index ++) {
if(list_element_cmp(tmp,ele) == 0) {
ret = index;
break;
}
}
//read_unlock(list)
return ret;
}
相关文章推荐
- Swift语言工厂设计模式和抽象工厂设计模式
- 14.C语言和设计模式(享元模式)
- 非关语言: 设计模式
- 简易模式脚本语言(PS)设计
- go语言实现设计模式(一):策略模式
- 设计模式-适配器模式(Go语言描写叙述)
- java语言实现创建型设计模式—抽象工厂模式
- 设计模式之桥接模式(Java语言描述)
- 【ios开发技术】Swift 语言深入--Swift设计模式 (2/2)
- 黑马程序员——Java语言基础:面向对象、封装(构造函数、设计模式)
- 设计模式——状态,C# 语言范例
- 【脚本语言系列】关于Python设计模式,你需要知道的事情
- java语言讲解singleton的编程思想---深入浅出单实例Singleton设计模式
- 干净搞定delphi多语言-兼论设计模式
- 15.C语言和设计模式(代理模式)
- 22.C语言和设计模式(之组合模式)
- 23种设计模式详解(Java示例)->适用于所有OO语言
- Java设计模式——UML语言概述
- 设计模式之单利模式(C#语言描述,附视频下载地址)
- 最常用的设计模式(设计模式和语言没有太多关系)