其他分享
首页 > 其他分享> > 6-7 在一个数组中实现两个堆栈 (20 分)

6-7 在一个数组中实现两个堆栈 (20 分)

作者:互联网

题目地址:https://pintia.cn/problem-sets/15/problems/730

Pop函数成功弹出后应该返回弹出的值,否则就是错的,应该是和PrintStack函数有关

Stack CreateStack(int Maxsize) {
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->Data = (int *)malloc(sizeof(ElementType)* Maxsize);
    S->MaxSize = Maxsize;
    S->Top1 = -1;
    S->Top2 = Maxsize;
    return S;
}

bool Push(Stack S, ElementType X, int Tag) {
    if(Tag == 1) {
        if(S->Top1+1 == S->Top2) {
            printf("Stack Full\n");
            return false;
        }
        S->Data[++S->Top1] = X;
        return true;
    }
    else {
        if(S->Top2 == S->Top1+1) {
            printf("Stack Full\n");
            return false;
        }
        S->Data[--S->Top2] = X;
        return true;
    }
}

ElementType Pop(Stack S, int Tag) {
    if(Tag == 1) {
        if(S->Top1 == -1) {
            printf("Stack %d Empty\n", Tag);
            return ERROR;
        }
        return S->Data[S->Top1--];
    } else {
        if(S->Top2 == S->MaxSize) {
            printf("Stack %d Empty\n", Tag);
            return ERROR;
        }
        return S->Data[S->Top2++];
    }
}
View Code

 

标签:return,Top2,Top1,Tag,数组,堆栈,20,Data,Stack
来源: https://www.cnblogs.com/mile-star/p/11473995.html