6-4 在一个数组中实现两个堆栈 分数 15

6-4 在一个数组中实现两个堆栈 分数 15_第1张图片
6-4 在一个数组中实现两个堆栈 分数 15_第2张图片

Stack CreateStack(int MaxSize) 
{
    Stack S = (Stack)malloc(sizeof(struct SNode));

    S->Data = (ElementType*)malloc(MaxSize * sizeof(ElementType));
    S->MaxSize = MaxSize;
    S->Top1 = -1;
    S->Top2 = MaxSize;

    return S;
}

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

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

你可能感兴趣的:(C家家精品好题,c++,数据结构)