//作者 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
本文地址:http://com.8s8s.com/it/it24822.htm