一個簡單的C stack實現

用C語言實現了一個簡單的棧。基本思路是定義一個棧結構體,裏面有兩個指針和一個表示棧大小的int。兩個指針分別指向棧底和棧頂,當棧底指針和棧頂指針重合時,說明棧爲空;當棧頂指針減去棧底指針的值大於等於棧的大小,說明棧已滿。this

 
 
//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();        //預期輸出 一、二、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;
}

 

參考 :  百度文庫spa

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

相關文章
相關標籤/搜索