先进后出
后进先出
#include#include #define SIZE 10 typedef struct Student{ int id; char *name; }Data; //定义栈结构 typedef struct stack{ Data data[SIZE];//栈数据 int top; //栈顶 }SeqStack; SeqStack *init(){ SeqStack *p; if(p = (SeqStack*)malloc(sizeof(SeqStack))){ p->top = 0; return p; } return NULL; } //栈是否为空 int SeqStackIsEmpty(SeqStack *s){ return (s->top==0); } //释放栈的内存空间 void SeqStackFree(SeqStack *s){ if(s){ free(s); } } //清空栈 void SeqStackClear(SeqStack *s){ s->top=0; } //判断栈是否已经满 int SeqStackIsFull(SeqStack *s){ return (s->top == SIZE); } //入栈 *** 作 int SeqStackPush(SeqStack *s,Data data){ if((s->top+1)>SIZE){ printf("SeqStack is full!n"); return 0; } s->data[++s->top] = data; return 1; } //出栈 *** 作 Data SeqStackPop(SeqStack *s){ if(s->top == 0){ printf("SeqStack is empty!n"); exit(0); } return s->data[s->top--]; } //读栈顶数据 Data SeqStackTop(SeqStack *s){ if(s->top == 0){ printf("SeqStack is empty!n"); exit(0); } return s->data[s->top]; } int main(void) { SeqStack *s; s = init(); //1,kiki1进栈 Data d1; d1.id=1; d1.name="kiki1"; SeqStackPush(s,d1); //2.kiki2进栈 Data d2; d2.id=2; d2.name="kiki2"; SeqStackPush(s,d2); //出栈 Data dd1; dd1=SeqStackPop(s); printf("The pop data is(%d,%s)n",dd1.id,dd1.name); Data dd2; dd2=SeqStackPop(s); printf("The pop data is(%d,%s)n",dd2.id,dd2.name); return 0; }
执行结果如下:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)