一个简单的C stack实现

用C语言实现了一个简单的栈。基本思路是定义一个栈结构体,里面有两个指针和一个表示栈大小的int。两个指针分别指向栈底和栈顶,当栈底指针和栈顶指针重合时,说明栈为空;当栈顶指针减去栈底指针的值大于等于栈的大小,说明栈已满。


//mystack.h

#ifndef mystack_H
#define mystack_H #include <stdio.h> #include <stdlib.h> #define MYSTACK_INCREASE_NUM 2 typedef int ElementType; //暂定为int typedef int Bool; typedef struct mystack { struct mystack * _this; //this ElementType * bottom; //栈底指针 ElementType * top; //栈顶指针 int size; //栈大小 Bool (*freeStack)(); Bool (*push)(ElementType data); Bool (*pop)(ElementType *outputData); Bool (*isEmpty)(); void (*makeStackEmpty)(); void (*print)(); } MyStack; MyStack * initStack( int size); //初始化 Bool freeStack(); //释放内存 Bool push(ElementType data); Bool pop(ElementType * outputData); Bool isEmpty(); void makeStackEmpty(); void print(); #endif

 

 

//mystack.c
#include "mystack.h"


MyStack * initStack( int size);                            //初始化
Bool freeStack();                        //释放内存
Bool push(ElementType data);            
Bool pop(ElementType * outputData);
Bool isEmpty();
void makeStackEmpty();
void print();

MyStack* stack = NULL;

MyStack* initStack( int size)
{
    stack = (MyStack*)malloc(sizeof(MyStack));                    //分配内存
    if(stack==NULL)
        return NULL;
    stack->bottom = (ElementType*)malloc(sizeof(ElementType)*size);
    if(stack->bottom == NULL)
        return NULL;
    stack->top = stack->bottom;                //初始化为空栈   栈顶指针和栈底指针指向同一位置
    stack->size = size;                        //初始化大小

    stack->freeStack = freeStack;
    stack->push = push;
    stack->pop = pop;
    stack->isEmpty =isEmpty;
    stack->makeStackEmpty = makeStackEmpty;
    stack->print = print;
    stack->_this = stack;

    return stack;
}


Bool freeStack()
{
    if(stack->_this!=NULL)
    {
        free(stack->_this->bottom);
        free(stack->_this);
        return 1;
    }
    else
    {
        return 0;
    }
}

void makeStackEmpty()
{
    if(stack->_this!=NULL)
        stack->_this->top = stack->_this->bottom;
}

Bool isEmpty()
{
    if(stack->_this != NULL)
    {
        if(stack->_this->bottom == stack->_this->top)
            return 1;
        else
            return 0;
    }
    else
        return 0;
}

Bool push(ElementType data)
{
    if(stack->_this->top - stack->_this->bottom >= stack->_this->size)
    {
        stack->_this->bottom = (ElementType*)realloc(stack->_this->bottom,
            sizeof(ElementType*)*(stack->_this->size + MYSTACK_INCREASE_NUM));
        if(stack->_this->bottom == NULL)
            return 0;
        stack->_this->top = stack->_this->bottom + stack->_this->size;
        stack->_this->size += MYSTACK_INCREASE_NUM;
    }
    *(stack->_this->top)=data;                        //先存值
    stack->_this->top++;                            //指针加1,指向下一内存单元
    return 1;
}

Bool pop(ElementType * outputData)
{
    if(isEmpty())
    {
        printf("stack is empty\n");
        return 0;
    }
    stack->_this->top--;                                //指针减1后指向栈中最顶上的元素
    *outputData = *(stack->_this->top);                //取值
    return 1;
}

void print()
{
    if(stack->_this!= NULL)
    {
        if(stack->_this->bottom == stack->_this->top)
        {
            printf("stack is empty\n");
        }
        else
        {
            /*for(int i = 0 ; i < stack->top - stack->bottom ; i++)
                printf("%d\n",stack->bottom[i] );*/
            ElementType * element = stack->_this->bottom;
            for(;element != stack->_this->top ; element++)
                printf("%d\n",*element);
        }
    }
}

 

 

 

#include "mystack.h"
#include <stdio.h>


int main(int argc, char const *argv[])
{
    /* code */
    int a,b,c;
    MyStack* stack = initStack(2);
    stack->push(1);
    stack->push(2);
    stack->print();       //预期输出 1 、2
    stack->push(3);
    stack->print();        //预期输出 1、2、3
    stack->pop(&a);
    stack->pop(&b);
    stack->push(4);
    stack->pop(&c);
    printf("a=%d  b=%d c=%d\n",a,b,c );   //预期输出 a=3 b=2 c=4
    stack->makeStackEmpty();            
    stack->pop(&a);                    //预期输出 stack is empty
    printf("a=%d\n", a);            //预期输出 a=3
    stack->freeStack();

    return 0;
}

 

参考 :  百度文库

    http://blog.csdn.net/mci2004/article/details/7532205

posted @ 2016-05-09 21:33  BingQiang  阅读(1513)  评论(0编辑  收藏  举报