纯C系统上的范型代码示例 list.c
2004-12-01 13:15
453 查看
//作者 Robinkin from DevonIT .inc
#ifndef VECTOR_C
#define VECTOR_C
#include "memory.c"
#define DEBUG
#ifdef DEBUG
#include<iostream>
using namespace std;
#endif
#ifdef CMP // the list has special cmp method
//双向列表模板
//模板函数声明,放在全局区域
#define List_Declare(t) /
/
struct list_##t##_node{ /
t value; /
list_##t##_node * next; /
list_##t##_node * front; /
}; /
struct list_##t{ /
list_##t##_node * head; /
list_##t##_node * tail; /
int size; /
}; /
void list_##t##_new(list_##t & n){ /
n.head=n.tail=NULL; /
n.size=0; /
/
} /
/
void list_##t##_del(list_##t & n){ /
list_##t##_node * h; /
while(n.size!=0){ /
h=n.head; /
n.head=h->next; /
free(h); /
n.size--; /
} /
} /
/
bool list_##t##_push_back(list_##t & l, t x){/
list_##t##_node * node /
=(list_##t##_node*)malloc(sizeof(list_##t##_node));/
if(NULL==node){return FALSE;} /
else{ /
if(0==l.size){ /
l.head=l.tail=node; /
} /
node->value=x; /
node->next=NULL; /
node->front=l.tail; /
/
l.tail=node; /
l.size++; /
return TRUE; /
} /
} /
/
bool list_##t##_push_front(list_##t & l,t x){/
list_##t##_node * node /
=(list_##t##_node*)malloc(sizeof(list_##t##_node));/
if(NULL==node){return FALSE;} /
else{ /
if(0==l.size){ /
l.head=l.tail=node; /
} /
node->value=x; /
node->next=l.head; /
node->front=NULL; /
l.head=node; /
l.size++; /
return TRUE; /
} /
} /
#undef CMP //remove after side affect
#else // the list has plain cmp method
//双向列表模板
//模板函数声明,放在全局区域
#define List_Declare(t) /
struct list_##t##_node{ /
t value; /
list_##t##_node * next; /
list_##t##_node * front; /
}; /
struct list_##t{ /
list_##t##_node * head; /
list_##t##_node * tail; /
int size; /
}; /
void list_##t##_new(list_##t & n){ /
n.head=n.tail=NULL; /
n.size=0; /
/
} /
/
void list_##t##_del(list_##t & n){ /
list_##t##_node * h; /
while(n.size!=0){ /
h=n.head; /
n.head=h->next; /
free(h); /
n.size--; /
} /
} /
/
bool list_##t##_push_back(list_##t & l, t x){/
list_##t##_node * node /
=(list_##t##_node*)malloc(sizeof(list_##t##_node));/
if(NULL==node){return FALSE;} /
else{ /
if(0==l.size){ /
l.head=l.tail=node; /
} /
node->value=x; /
node->next=NULL; /
node->front=l.tail; /
/
l.tail=node; /
l.size++; /
return TRUE; /
} /
} /
/
bool list_##t##_push_front(list_##t & l,t x){/
list_##t##_node * node /
=(list_##t##_node*)malloc(sizeof(list_##t##_node));/
if(NULL==node){return FALSE;} /
else{ /
if(0==l.size){ /
l.head=l.tail=node; /
} /
node->value=x; /
node->next=l.head; /
node->front=NULL; /
l.head=node; /
l.size++; /
return TRUE; /
} /
}
#endif
#define List_New(t,a) /
list_##t a; /
list_##t##_new(a); //构造函数
#define LN List_New
#define List_Del(t,a) /
list_##t##_del(a); //析构函数
#define LD List_Del
#ifdef DEBUG
int int_cmp(int a,int b){
if(a<b){return -1;}
if(a>b){return 1;}
if(a==b){return 1;}
}
#define CMP int_cmp //declare that the list use a special cmpare method
//声明这个list要用的cmp函数
//如果没有这条define则这个list使用平凡的<>=比较方法
List_Declare(int)
int main(){
List_New(int,a)
list_int_push_back(a,1000);
cout<<a.tail->value;
list_int_push_front(a,100);
cout<<a.head->value;
cout<<a.tail->front->value;
cout<<a.head->next->value;
List_Del(int,a)
}
#endif
#endif
#ifndef VECTOR_C
#define VECTOR_C
#include "memory.c"
#define DEBUG
#ifdef DEBUG
#include<iostream>
using namespace std;
#endif
#ifdef CMP // the list has special cmp method
//双向列表模板
//模板函数声明,放在全局区域
#define List_Declare(t) /
/
struct list_##t##_node{ /
t value; /
list_##t##_node * next; /
list_##t##_node * front; /
}; /
struct list_##t{ /
list_##t##_node * head; /
list_##t##_node * tail; /
int size; /
}; /
void list_##t##_new(list_##t & n){ /
n.head=n.tail=NULL; /
n.size=0; /
/
} /
/
void list_##t##_del(list_##t & n){ /
list_##t##_node * h; /
while(n.size!=0){ /
h=n.head; /
n.head=h->next; /
free(h); /
n.size--; /
} /
} /
/
bool list_##t##_push_back(list_##t & l, t x){/
list_##t##_node * node /
=(list_##t##_node*)malloc(sizeof(list_##t##_node));/
if(NULL==node){return FALSE;} /
else{ /
if(0==l.size){ /
l.head=l.tail=node; /
} /
node->value=x; /
node->next=NULL; /
node->front=l.tail; /
/
l.tail=node; /
l.size++; /
return TRUE; /
} /
} /
/
bool list_##t##_push_front(list_##t & l,t x){/
list_##t##_node * node /
=(list_##t##_node*)malloc(sizeof(list_##t##_node));/
if(NULL==node){return FALSE;} /
else{ /
if(0==l.size){ /
l.head=l.tail=node; /
} /
node->value=x; /
node->next=l.head; /
node->front=NULL; /
l.head=node; /
l.size++; /
return TRUE; /
} /
} /
#undef CMP //remove after side affect
#else // the list has plain cmp method
//双向列表模板
//模板函数声明,放在全局区域
#define List_Declare(t) /
struct list_##t##_node{ /
t value; /
list_##t##_node * next; /
list_##t##_node * front; /
}; /
struct list_##t{ /
list_##t##_node * head; /
list_##t##_node * tail; /
int size; /
}; /
void list_##t##_new(list_##t & n){ /
n.head=n.tail=NULL; /
n.size=0; /
/
} /
/
void list_##t##_del(list_##t & n){ /
list_##t##_node * h; /
while(n.size!=0){ /
h=n.head; /
n.head=h->next; /
free(h); /
n.size--; /
} /
} /
/
bool list_##t##_push_back(list_##t & l, t x){/
list_##t##_node * node /
=(list_##t##_node*)malloc(sizeof(list_##t##_node));/
if(NULL==node){return FALSE;} /
else{ /
if(0==l.size){ /
l.head=l.tail=node; /
} /
node->value=x; /
node->next=NULL; /
node->front=l.tail; /
/
l.tail=node; /
l.size++; /
return TRUE; /
} /
} /
/
bool list_##t##_push_front(list_##t & l,t x){/
list_##t##_node * node /
=(list_##t##_node*)malloc(sizeof(list_##t##_node));/
if(NULL==node){return FALSE;} /
else{ /
if(0==l.size){ /
l.head=l.tail=node; /
} /
node->value=x; /
node->next=l.head; /
node->front=NULL; /
l.head=node; /
l.size++; /
return TRUE; /
} /
}
#endif
#define List_New(t,a) /
list_##t a; /
list_##t##_new(a); //构造函数
#define LN List_New
#define List_Del(t,a) /
list_##t##_del(a); //析构函数
#define LD List_Del
#ifdef DEBUG
int int_cmp(int a,int b){
if(a<b){return -1;}
if(a>b){return 1;}
if(a==b){return 1;}
}
#define CMP int_cmp //declare that the list use a special cmpare method
//声明这个list要用的cmp函数
//如果没有这条define则这个list使用平凡的<>=比较方法
List_Declare(int)
int main(){
List_New(int,a)
list_int_push_back(a,1000);
cout<<a.tail->value;
list_int_push_front(a,100);
cout<<a.head->value;
cout<<a.tail->front->value;
cout<<a.head->next->value;
List_Del(int,a)
}
#endif
#endif
相关文章推荐
- 关于C++中的虚函数
- VC++.NET开发环境整合技巧
- MASM宏使用总结
- [ZT]代码地震(作者:王咏刚 2004 年1 月)
- 32位代码优化常识
- hymn新闻系统(后台程序)全部源代码
- Java中对文件的操作
- Delphi基础:回调函数及其使用
- Dreamweaver自动生成的垃圾代码
- 如何从一个php文件向另一个地址post数据,不用表单和隐藏的变量
- 大虾们。这里来看下,帮帮小弟哈(高分求助)
- 关于Visual InterDev 6.0 调试ASP工程的个人方法。
- 创建文件夹和文件的java代码范例
- 在Java中如何模拟多继承
- 搞编程的有必要一读
- 收录Google公益广告的答案
- Delphi的MD5算法
- 311530 HOW TO:在 Visual C# .NET 中使用 System.Xml 合并两个 XML 文档中的数据 (From MKBA)
- asp inject 总结
- Windows CE 编程的十点忠告(z)