#include <STDIO.H> #include <STRING.H> #include <STDLIB.H> typedef struct SeqStack { int length; int top; char *data; }seqstack; seqstack* CreatStack(seqstack *s,int n) { s=(seqstack *)malloc(sizeof(seqstack)+n*sizeof(char)); if(s==NULL) return NULL; memset(s,0,sizeof(seqstack)+n*sizeof(char)); s->length=n; s->top=0; s->data=(char *)(s+1); return s; } int StackEmpty(seqstack *s) { if(s->top==0) return 1; else return 0; } int GetTop(seqstack *s,char *e) { if(s->top==0) return -1; *e=s->data[(s->top)-1]; return 0; } int PushStack(seqstack *s,char e) { if(s->top==s->length) return -1; s->data[s->top]=e; s->top+=1; return 0; } int PopStack(seqstack *s,char *e) { *e=s->data[s->top-1]; s->top-=1; return 0; } int StackLength(seqstack *s) { return s->top; } void InitStack(seqstack *s) { s->top=0; } void destroyStack(seqstack *s) { free(s); } int main() { seqstack *s=NULL; int i; char e=65; s=CreatStack(s,10); printf("%d\n",StackEmpty(s)); for(i=0;i<s->length;i++) { PushStack(s,65+i); GetTop(s,&e); printf("%c,",e); } printf("\n"); printf("%d\n",StackEmpty(s)); for(i=0;i<s->length;i++) { PopStack(s,&e); printf("%c,",e); } printf("\n"); printf("%d\n",StackEmpty(s)); destroyStack(s); return 0; }
时间: 2024-11-05 06:13:30