C语言实现顺序栈

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define MAXSIZE 100

typedef struct
{
    int *base;
    int *top;
    int stacksize;
}SqStack;

int InitSqStack(SqStack *p)
{
    p->base=(int*)malloc(MAXSIZE);
    if(!p->base)exit;
    p->top=p->base;
    p->stacksize=MAXSIZE;
    return 0;
}
int push(SqStack *p,int e)
{
    if(p->top-p->base==p->stacksize)
        printf("the stack is full:\n");
    *p->top++=e;
    return 0;
}
int pop(SqStack *p,int e)
{
    if(p->base==p->top)
        printf("the stack is empty:\n");
    e=*--p->top;
    return e;
}
int gettop(SqStack *p)
{
    if(p->top!=p->base)
        return *(p->top-1);
}

int main()
{
    int b,c;
    SqStack a;
    InitSqStack(&a);
    push(&a,10);
    pop(&a,b);
    push(&a,10);
    c=gettop(&a);
    printf("%d,%d\n",b,c);
}
    

 

posted @ 2020-11-26 13:04  UNkey  阅读(140)  评论(0)    收藏  举报