纪念逝去的岁月——C++实现一个栈

1、代码

2、运行结果


 

1、代码 stack.cpp

#include <stdio.h>
#include <string.h>

class ClsStack
{
    private :
        void ** __m_Data;
        int     __m_pos;
        size_t  __m_memsize;

    protected :
        int     __resize(size_t n);
        size_t  __doublesize(size_t n);

    public :
        ClsStack(size_t n = 0);
        ~ClsStack();

        int  pop(void ** ppData);
        int  top(void ** ppData);
        int  push(void * pData);
        void clear();
};

ClsStack::ClsStack(size_t n)
{
    __m_Data = NULL;
    __m_pos = -1;
    __m_memsize = 0;

    if(0 != n)
    {
        __m_Data = new void * [n];
        if(NULL != __m_Data)
        {
            __m_memsize = n;
        }
    }
}

ClsStack::~ClsStack()
{
    if(NULL != __m_Data)
    {
        delete __m_Data;
        __m_Data = NULL;
    }
    __m_pos = -1;
    __m_memsize = 0;
}

size_t ClsStack::__doublesize(size_t n)
{
    size_t x = 0;
    if(0 == n)
    {
        x = 1;
    }
    else
    {
        x = n * 2;
    }

    return x;
}

int ClsStack::__resize(size_t n)
{
    void ** p = new void * [n];
    if(NULL == p)
    {
        return -1;
    }
    memset(p, 0, sizeof(void *) * (n));
    if(NULL != __m_Data)
    {
        if( NULL == memcpy(p, __m_Data, __m_memsize * sizeof(void *)))
        {
            delete p;
            return -1;
        }
        delete __m_Data;
    }
    __m_Data = p;
    __m_memsize = n;

    return 0;
}

int ClsStack::pop(void ** ppData)
{
    if(NULL == ppData)
    {
        return -1;
    }
    int r = 0;
    if(-1 == __m_pos)
    {
        *ppData = NULL;
        r = -1;
    }
    else
    {
        *ppData = __m_Data[__m_pos --];
        r = 0;
    }

    return r;
}

int ClsStack::top(void ** ppData)
{
    if(NULL == ppData)
    {
        return -1;
    }
    int r = 0;
    if(-1 == __m_pos)
    {
        *ppData = NULL;
        r = -1;
    }
    else
    {
        *ppData = __m_Data[__m_pos];
        r = 0;
    }

    return r;
}

int ClsStack::push(void * pData)
{
    if(__m_pos + 1 >= __m_memsize)
    {
        size_t n = __doublesize(__m_memsize);
        if(0 != __resize(n))
        {
            return -1;
        }
    }
    __m_Data[++__m_pos] = pData;

    return 0;
}

void ClsStack::clear()
{
    if(NULL != __m_Data && 0 != __m_memsize)
    {
        delete __m_Data;
    }
    __m_Data = NULL;
    __m_pos = -1;
    __m_memsize = 0;
}

int main()
{
    ClsStack objStack;
    int x = 10;
    int * p = &x;
    objStack.push(p);
    int i = 0;
    for(i = 0; i <= 10; i++)
    {
        int * z = new int;
        *z = i;
        objStack.push(z);
    }
    for(i = 10; i >= 0; i--)
    {
        int * z = NULL;
        int r = objStack.pop((void **)&z);
        if(NULL == z)
        {
            printf("z == NULL\n");
            continue;
        }
        if(i == *z)
        {
            printf("RIGHT\n");
        }
        else
        {
            printf("WRONG\n");
        }
        delete z;
    }
    int * g = NULL;
    int r = objStack.pop((void **)&g);
    if(x == *g)
    {
        printf("RIGHT\n");
    }
    else
    {
        printf("WRONG\n");
    }

    return 0;
}

2、编译及运行

  2.1、编译

g++ -g -o stack stack.o

  2.2运行

RIGHT
RIGHT
RIGHT
RIGHT
RIGHT
RIGHT
RIGHT
RIGHT
RIGHT
RIGHT
RIGHT
RIGHT

 

posted @ 2015-05-31 23:01  fengbohello  阅读(722)  评论(0编辑  收藏  举报