栈的顺序存储实现

来源:互联网 发布:mysql查看表字段语句 编辑:程序博客网 时间:2024/04/26 12:57

clear未验证


#ifndef STACK_H_INCLUDED

#define STACK_H_INCLUDED
#define Ini_size 100
#define Inc_size 10


typedef int elemtype;


struct newtype//data[0]存放size
{
    elemtype *data;
    int top;
};
typedef struct newtype stack;


void InitialStack(stack*S)
{
    S->data = (elemtype*)malloc(Ini_size*sizeof(elemtype));
    S->data[0]=0;
    S->top = 0;
}


void Push(stack*S,elemtype x)
{
    if(S->top==S->data[0])
    S->data=(elemtype*)realloc(S->data,(S->data[0]+Inc_size)*sizeof(elemtype));
    S->data[0]+=Inc_size;
    S->top++;
    S->data[S->top]=x;
}


elemtype Pop(stack*S)
{
    if(S->top==0) {printf("stack is empty\n");exit(0);}
    S->top--;
    return S->data[S->top+1];
}


void Clear(stack*s)
{
    free(s->data);
    s->data[0]=0;
    s->top=0;
}
#endif // STACK_H_INCLUDED
原创粉丝点击