顺序栈C语言

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 5    /* 栈最大容量 */
#define Empty 0        /* 空 */
#define Full 1        /* 满 */
#define Avail -1    /* 可用 */
typedef int Eletype;
typedef struct stack
{
    Eletype *top;            /* 栈顶指针 */
    Eletype *base;        /* 栈底指针 */
    int stacksize;        /* 栈的最大容量 */
}stack;

stack InitStack (){
    stack S;
    S.base=(Eletype*)malloc(S.stacksize*sizeof(Eletype));
    if (!S.base)
    {
        printf("error\n");
        exit(0);
    }

        S.top=S.base;
        S.stacksize=MAX_SIZE;
        return S;   
}
stack Push(stack S){
    if(S.top-S.base==S.stacksize){
        printf("erroe full\n");
        exit(0);
    }
    else{
        int data;
                printf("pleasr input the data\n");
        while(scanf("%d",&data) != EOF){
		
        *S.top=data;
        S.top++;
    }
        return S;
    }
}
stack pop(stack S)
{
    if (S.base==S.top)
    {
        printf("kong \n");
        exit(0);
    }
    while(S.top!=S.base){
    S.top--;
    printf("data is :%d\n",*S.top);
}
}
int main ()
{
    stack S;
    S=InitStack ();
    S=Push(S);
    pop(S);
}

  

posted @ 2022-03-16 21:54  zhichutian  阅读(37)  评论(0)    收藏  举报