//既然在这里开始,那就在这里结束。
实现stack 功能
_____assert测试
assert带有一个断言作为int 类型参数,断言是指正常情况下为真的表达式。assert是一个宏,但是用函数实现的。
当参数为0,编译停止,提供参数信息输出显示。调试结束后,取消assert测试,只需加上一句
#define NDEBUG 在#include "assert.h"之前。
____void * realloc(void * p, int size);
return the same pointer in the parameter.
if current space is not enough, than go to a new place ,a pointer will be returned correspondly. The orighinal dates will be coped into the new space,2 cases: 1,integars will be copied directly. 2,if char,will consider the dates as address pointing at the char,so in the new space ,also address is copied and stored....
____when dealing with pointers, make sure the pointer is valid one.use assert to perform the check.
____stark 的char 型实现。
typedef struct { uchar length; uchar allocSize; uchar* elem; }stack; stackNew(stack *s); stackDispose(stack *s); stackPush(stack *s, uchar* date); stackPop(stack *s, uchar* date); stackNew(stack *s) { assert(s!=NULL); s->length = 0; s->allocSize = 4; s->elem = malloc(4*sizeof(uchar)); assert(s->elem != NULL); } stackDispose(stack *s) { assert(s != NULL && s->elem != NULL); free(s->elem); } stackPush(stack *s, uchar* date) { assert(s!=NULL && date != NULL); if(s->length == s->allocSize) { s->allocSize *= 2; s->elem = realloc(s->elem,s->allocSize); //reallocate designate size assert(s->elem != NULL); } *(s->elem+s->length) = *date; //s->elem[s->length-1] = *date; s->length += 1; } stackPop(stack *s, uchar* date) { assert(s!=NULL && date!=NULL); assert(s->length >= 1); --s->length; *date = s->elem[s->length]; }
原文:http://www.cnblogs.com/aprilapril/p/4366434.html