首页 > 其他 > 详细

编程范式 episode 6

时间:2015-03-25 19:12:09      阅读:232      评论:0      收藏:0      [点我收藏+]

//既然在这里开始,那就在这里结束。

 实现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];
}

 

编程范式 episode 6

原文:http://www.cnblogs.com/aprilapril/p/4366434.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!