#include <stdio.h> #include <stdlib.h> #define ElementType int const int MAXSIZE = 10; typedef struct Node *DStack; typedef struct Node{ ElementType Data[MAXSIZE]; int last0; int last1; }; bool Push(int flag,DStack &S, ElementType X){//0代表插入栈0,1代表插入栈1 //printf("------------------------------------ "); if(S->last0==S->last1-1){ printf("栈满 "); return 0; } //printf("------------------------------------ "); if(flag==0){ S->Data[++S->last0] = X; return true; }else if(flag == 1){ S->Data[--S->last1]=X; return true; }else{ printf("插入序号有误 "); return false; } } ElementType Pop(int flag,&DStack S){ if((flag==0 && S->last0==-1)||(flag==1 && S->last1 == MAXSIZE)){ printf("栈空"); return 0; } if(flag ==0) return S->Data[S->last0--]; else if(flag ==1) return S->Data[S->last1++]; else{ printf("插入序号有误 "); return 0; } } void InintDStack(DStack &S){ S = (DStack)malloc(sizeof(struct Node)); S->last0 = -1; S->last1= MAXSIZE; } int main(){ DStack S; InintDStack(S); for(int i = 0;i<12;i++){ Push(i%2,S,i); } for(int i=0;i<12;i++){ printf("%d ",Pop(i%2,S)); } return 0; }