压栈出栈

#include<stdio.h>
#include<stdlib.h>
#define ERROR 0
#define NULL 0
#define maxSize 100
#define OK 1

typedef struct 
{
    int *top;
    int *base;
    int size;
}stack;

int initStack(stack &s)
{
    if(!(s.top=s.base=(int *)malloc(maxSize*sizeof(int))))
        return ERROR;
    s.size=maxSize;
    return OK;
}

int push(stack &s,int e)
{
    *s.top++=e;
    return OK;
}

int pop(stack &s,int &e)
{
    if(s.base==s.top)
        return ERROR;
    s.top--;
    e=*s.top;
    return e;
}

int main()
{
    stack s;
    int n,e;
    printf("input the number of stack:\n");
    scanf("%d",&n);
    printf("input the stack elements:\n");
    initStack(s);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&e);
        push(s,e);
    }
    printf("output the stack elements:\n");
    while(s.base!=s.top)
        printf("%d ",pop(s,e));
    printf("\n");
    return 0;

}



你可能感兴趣的:(栈)