Stack CreateStack( int MaxSize ){
Stack s=(Stack)malloc(sizeof(struct SNode));
s->Data= (int *)malloc(MaxSize*sizeof(int));
s->MaxSize=MaxSize;
s->Top1=-1;
s->Top2=MaxSize;
return s;
}
bool Push( Stack S, ElementType X, int Tag ){
if(S->Top1+1==S->Top2){
printf("Stack Full\n");
return false;
}
if(Tag==1){
S->Data[++S->Top1]=X;
}
else if(Tag==2){
S->Data[--S->Top2]=X;
}
return true;
}
ElementType Pop( Stack S, int Tag ){
int key;
if(Tag==1){
if(S->Top1==-1){
printf("Stack %d Empty\n",Tag);
return ERROR;
}
else{
key=S->Data[S->Top1--];
return key;
}
}
else if(Tag==2){
if(S->Top2==S->MaxSize){
printf("Stack %d Empty\n",Tag);
return ERROR;
}
else{
key=S->Data[S->Top2++];
return key;
}
}
}a
6-7 在一个数组中实现两个堆栈 (20 分)
最新推荐文章于 2022-03-13 15:57:16 发布