c语言模拟实现栈的模板化
来源:互联网 发布:试飞试验数据 编辑:程序博客网 时间:2024/06/05 18:06
对于写过表达式解析的同学一定不会陌生,我们需要两个栈,一个是符号栈(char),一个是操作数栈(int). 经典的数据结构书中的栈,类型是定死了的。
所以能够实现一个栈,自己指定类型,想必编写接下来代码的心情要愉快的多。本实现过程采用宏定义,这样然接口看来就好像实现了模板化。这里有篇csdn的文章也是实现了模板化的,但是它采用的是预处理运算符## 实现的,及字符串化(stringification),具体链接:http://blog.csdn.net/kingofmiaomiao/article/details/2011195 (注意:字符串化也是属于宏的范畴)
下面给出,本人实现的编码,参考了一个名叫glu的开源项目中的array实现,在此表示感谢。
stack.h (这里的push, pop因为大家应该都是再熟悉不过了,就没有写日志了哈)
/** *该文件实现了对栈操作的模板化,在实现过程中参考了一个开源的glu项目 *The file has template the operation about stack by referencing a opensource project 'glu' *@author daniel *@date 2011-09-19 */#ifndef _STACK_H#define _STACK_H#include <string.h>#include <stdlib.h>#define STACK_INIT_SIZE 50#define STACK_ERROR -1#define initStack(type,number) \stack_do_alloc(sizeof(type), number)#define push(type,stack,data) \(stack_error_msg = stack_resize((stack), (stack)->num + 1), \stack_error_msg != STACK_ERROR ? \ *((type *)((stack)->space + (stack)->num++ * (stack)->obj_size)) = data : data)#define getTop(type,stack) \((stack)->num ? *((type *)((stack)->space + ((stack)->num - 1) * (stack)->obj_size)) : *((type *)0))#define pop(type,stack) \((stack)->num ? ((stack)->num--, *((type *)((stack)->space + (stack)->num * (stack)->obj_size))): *((type *)0))#define getNum(stack)\((stack)->num)#define isEmpty(stack)\(0 == (stack)->num)#define clearStack(stack) \((stack)->num = 0, \ (void) memset((stack)->space, 0, (stack)->n_size * (stack)->obj_size))#define freeStack(stack) \(FREE((stack)->space), \ FREE(stack))# define ALLOC(type, num)\ ((type *) malloc(sizeof(type) * (unsigned long) (num)))# define REALLOC(type, obj, num)\ ((type *) realloc((void *) (obj), sizeof(type) * (unsigned long) (num)))# define FREE(obj)\ ((obj) ? (free((void *) (obj)), (obj) = 0) : 0)# define NIL(type)((type *) 0)#ifndef MAX# define MAX(a,b)((a) > (b) ? (a) : (b))#endifint stack_error_msg;typedef struct{char *space; // the head pointint num; // number of stack elementsint n_size; // size of stack or says capability int obj_size; // size of each stack object} stack_t;stack_t * stack_do_alloc(int, int);int stack_resize(stack_t *stack, int newnum);#endif
stack.c
#include "stack.h"stack_t * stack_do_alloc(int size, int number){stack_t *stack;stack = ALLOC(stack_t, 1);if(NIL(stack_t) == stack)return NIL(stack_t);stack->num = 0;stack->n_size = MAX(number, STACK_INIT_SIZE);stack->obj_size = size;stack->space = ALLOC(char, stack->n_size * stack->obj_size);if(NIL(char) == stack->space)return NIL(stack_t);(void) memset(stack->space,0,stack->n_size * stack->obj_size);return stack;}int stack_resize(stack_t *stack, int newsize){int old_size;char *newspace, *pos;if(stack->n_size > newsize)return 0;old_size = stack->n_size;stack->n_size = MAX(stack->n_size * 2, newsize);newspace = REALLOC(char, stack->space, stack->n_size * stack->obj_size);if(NIL(char) == newspace){stack->n_size = old_size;return -1;}stack->space = newspace;pos = stack->space + old_size * stack->obj_size;(void) memset(pos, 0, (stack->n_size - old_size) * stack->obj_size);return 0;}
测试代码:
#include "stack.h"#include <stdio.h>int main(){stack_t *a = initStack(int, 10);push(int, a, 123);printf("getTop:%d\n", getTop(int, a));pop(int, a);freeStack(a);return 0;}
- c语言模拟实现栈的模板化
- c语言模拟模板的函数的方法
- String函数的模拟实现(c语言)
- 模板模拟实现栈
- 模拟实现C语言库函数
- C语言:模拟实现memmove
- C语言:模拟实现memcpy
- C语言:模拟实现strcpy
- c语言模拟实现memmove
- 【C语言】模拟实现memmove
- 【C语言】模拟实现strstr
- C语言 模拟实现memmove
- c语言模拟实现strcat
- 模拟实现strcpy(C语言)
- 模拟实现strstr(c语言)
- C语言-模拟实现strcpy
- C语言-模拟实现strcat
- c语言模拟实现strcpy
- Android网络编程——Socket
- .net 复制DataTable 里面的Row到另一个DataTable
- Web Service之Axis实现
- 民族大学外国语学院2011级新生入学演讲
- java多线程
- c语言模拟实现栈的模板化
- 菜鸟程序员五年经历(1-59,60-93,大结局)
- Eclipse常用快捷键功能
- 程序员面试题精选100题(41)-把数组排成最小的数
- 如何查看项目的struts和hibernate和spring版本
- select()网络通信
- 直线和平面的交点
- 什么叫临界资源和临界区?
- silverlight学习笔记(3) 缺少对象错误