Data Structures and Algorithms (English) - 6-2 Two Stacks In One Array(20 分)

题目链接:点击打开链接

 

题目大意:略。

 

解题思路:略。

 

AC 代码

Stack CreateStack(int MaxElements)
{
    Stack p=(Stack)malloc(sizeof(struct StackRecord));
    p->Array=(int*)malloc(MaxElements*sizeof(int));
    p->Top1=-1;
    p->Top2=MaxElements;
    p->Capacity=MaxElements;
    return p;
}

int IsEmpty(Stack S,int Stacknum)
{
    if(Stacknum==1)
    {
        if(S->Top1==-1) return 1;
        return 0;
    }
    else
    {
        if(S->Top2==S->Capacity) return 1;
        return 0;
    }
}

int IsFull(Stack S)
{
    if(S->Top2-S->Top1==1) return 1;
    return 0;
}

int Push(ElementType X, Stack S, int Stacknum)
{
    if(IsFull(S)) return 0;
    if(Stacknum==1) S->Array[++(S->Top1)]=X;
    else S->Array[--(S->Top2)]=X;
    return 1;
}

ElementType Top_Pop(Stack S,int Stacknum)
{
    if(IsEmpty(S,Stacknum)) return ERROR;
    if(Stacknum==1) return S->Array[(S->Top1)--];
    else return S->Array[(S->Top2)++];
}

/*
Operation GetOp()
{
    char s[111];
    scanf("%s",s);
    if(!strcmp(s,"Push")) return push;
    if(!strcmp(s,"Pop")) return pop;
    if(!strcmp(s,"End")) return end;
}

void PrintStack(Stack S,int Stacknum)
{
    while(!IsEmpty(S,Stacknum))
        printf("%d ",Top_Pop(S,Stacknum));
    puts("");
}
*/

 

你可能感兴趣的:(#,ACM,#,PTA)