c语言模拟实现栈的模板化

       对于写过表达式解析的同学一定不会陌生,我们需要两个栈,一个是符号栈(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 
#include 

#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))
#endif

int stack_error_msg;

typedef struct{
	char *space; // the head point
	int num; // number of stack elements
	int 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 

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;
}
 
  

 
  
 
  
 
  
 
 

你可能感兴趣的:(linux,c)