来源:互联网 发布:青岛seo排名优化公司 编辑:程序博客网 时间:2024/06/06 17:02
#include "stdio.h"#include "malloc.h"#define MAXSIZE 100typedef int datatype;#define NULL 0typedef struct{    datatype stack[MAXSIZE];    int top;}seqstack;seqstack * InitStack(seqstack *s){    s=(seqstack *)malloc(sizeof(seqstack));    if(s!=NULL){        s->top=-1;        return s;    }else{        printf("分配内存失败!");        return NULL;    }}int IsEmpty(seqstack *s){    if(s->top<0){        return 1;    }else{        return 0;    }}int Push(seqstack *s,datatype e){    if(s->top==MAXSIZE-1){        printf("占满溢出错误!\n");        return 0;    }else{        s->top++;        s->stack[s->top]=e;        return s->top;    }}//出栈datatype Pop(seqstack *s){    datatype x;    if(IsEmpty(s)){        printf("下溢出错误!");        return 0;    }else{        x=s->stack[s->top];        s->top--;//先->再--        return x;    }}//取栈顶datatype GetPop(seqstack *s){    if(IsEmpty(s)){        printf("栈是空的");        return 0;    }else{        return s->stack[s->top];    }}main(){    int i;    seqstack *s;    s=InitStack(s);    for(i=0;i<50;i++){        Push(s,i+1);    }   printf("%d\n",GetPop(s));//50    for(i=0;i<50;i++){        printf("%3d",Pop(s));    }}

0 0
原创粉丝点击