解题代码
Stack CreateStack(int MaxSize) {
Stack new = (Stack)malloc(sizeof(struct SNode));
new->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
new->Top1 = -1;
new->Top2 = MaxSize;
new->MaxSize = MaxSize;
return new;
}
bool Push(Stack S, ElementType X, int Tag) {
bool flag = false;
if (Tag == 1) {
if (S->Top1==S->Top2-1) {
printf("Stack Full\n");
}
else {
S->Data[++S->Top1] = X;
flag = true;
}
}
else if(Tag==2){
if (S->Top1 == S->Top2 - 1) {
printf("Stack Full\n");
}
else {
S->Data[--S->Top2] = X;
flag = true;
}
}
return flag;
}
ElementType Pop(Stack S, int Tag) {
ElementType pop=ERROR;
if (Tag == 1) {
if (S->Top1==-1) {
printf("Stack %d Empty\n", Tag);
}
else {
pop = S->Data[S->Top1--];
}
}
else if (Tag == 2) {
if (S->Top2 == S->MaxSize) {
printf("Stack %d Empty\n", Tag);
}
else {
pop = S->Data[S->Top2++];
}
}
return pop;
}
测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/39181e823ed380d8df30755d958b3662.png)
问题整理
1.写程序要从思路到框架到细节。