(数据结构)建立链栈,将一组数据入栈,然后再分别出栈并输出。

    科技2025-06-15  14

    #include <stdio.h> #define datatype char #define MAXSIZE 100 #define NULL '\0' typedef struct { datatype data[MAXSIZE]; int top; }SEQSTACK; void initstack(SEQSTACK *s) { s->top=-1;//赋值 } void push(SEQSTACK *s,datatype x)//入栈 { if(s->top==MAXSIZE-1)//判断是否相等 { printf("overflow"); exit(0); } else { s->top++; s->data[s->top]=x; } } int empty(SEQSTACK *s)//判空 { if(s->top==-1) { return 1; } else { return 0; } } datatype pop(SEQSTACK *s) { datatype x; if(empty(s)) { printf("underflow\n"); x=NULL; } else { x=s->data[s->top]; s->top--; } return x; } int main(int argc, char *argv[]) { SEQSTACK s,*p; char x,y; p=&s; initstack(p); while((x=getchar())!='$') { push(p,x); } while(!empty(p)) { y=pop(p); printf("%3c",y); } return 0; }
    Processed: 0.012, SQL: 8