纯C系统上的范型代码示例 list.c

此博客主要展示了C语言中双向列表模板的实现代码。包含模板函数声明,如列表节点和列表结构体定义,以及列表的创建、删除、尾部插入和头部插入等操作函数。还给出了构造和析构函数的宏定义,并在调试部分进行了简单测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//作者 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值