C语言:泛型栈的一次尝试
2012-04-19 16:09
148 查看
尝试了用C做一个泛型的栈,抛出来跟大家交流一下。
编译时爆出几个warning,显然用C做泛型还是有些不理智。
#include <stdio.h> /** * Make a struct for stacks. * I hope to make a generic struct of stacks. * I may have a try. * */ /** Two variables: * 01. pointer to char. * 02. the position of the stack. * from 0 to MAX * 03. the length of the array of char which is malloc.; */ typedef struct Stack *StackSP; struct Stack { void *pt; size_t top; size_t length; }; /** function judge whether stack is empty. * argument sp is a pointer to Stack. * * should be aware that top is equal to zero while stack is empty. */ #define size_t ifempty(StackSP sp) sp->top /** Push an element into the top of the stack. */ #define push(vartype, stacksp, value)\ if(stacksp->top == stacksp->length) fprintf(stderr,"error : stack is full.");\ ((vartype *)stacksp->pt)[stacksp->top++] = value ; /** Pop()function return an element from the top of the stack. * Warning : the element is also an pointer to void. * so you cannot referrence the variable by this pointer, * you show make some conventions. */ #define pop(vartype, stacksp)\ (stacksp->top == 0) ? NULL : (((vartype *) stacksp->pt)[--stacksp->top]) /** Free the space applied. */ #define closestack(vartype, stacksp) if(stacksp->pt == NULL){\ free((vartype *)stacksp->pt);\ stacksp->top = 0;} /** Malloc a space for stack. * **/ #define openstack(vartype,stacksp,op_length) \ stacksp->pt = malloc(sizeof(vartype) * op_length );\ stacksp->length = op_length; \ stacksp->top = 0;
编译时爆出几个warning,显然用C做泛型还是有些不理智。
相关文章推荐
- 一次无聊的尝试----用C语言,在子程序里修改主程序的局部变量.
- 一次失败的尝试(下):无法使用泛型的Attribute
- 一次失败的尝试(下):无法使用泛型的Attribute
- 一次泛型引起的意外:java.math.BigDecimal cannot be cast to java.lang.Integer
- C语言 while(feof(fp)){}多循环一次解决方案
- 用C语言的泛型实现交换两个变量值
- 自定义View的一次简单尝试
- 终结:Oracle用一次忘一次“尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将”
- 一次C语言实现字符排序出现的问题
- 毕业生自传----一次大胆的尝试,作别亚信
- 关于稳定物理关节的一次尝试
- 【编程范式】C语言泛型
- 一次小小的模板尝试
- Java连接MySQL数据库和Oracle数据库并进行简单的SQL操作的一次尝试
- 一次Oracle 11g+FluentNHibernate AutoMapping组合尝试
- 找出三个只出现一次的数字 C语言实现
- 统计n个随机数(随机0~10),每出现一次就用*号在数字下面写一次*【c语言实现】
- 一次运用设计模式对现有系统进行重构的尝试(一)
- Initrd is too big 的一次错误尝试
- C语言 数组中只出现一次的值