PTA数据结构与算法题目集(中文)-6-7: 在一个数组中实现两个堆栈

一、题目描述

本题要求在一个数组中实现两个堆栈。

函数接口定义:

Stack CreateStack( int MaxSize );
bool Push( Stack S, ElementType X, int Tag );
ElementType Pop( Stack S, int Tag );

其中Tag是堆栈编号,取12MaxSize堆栈数组的规模;Stack结构定义如下:

typedef int Position;
struct SNode {
    ElementType *Data;
    Position Top1, Top2;
    int MaxSize;
};
typedef struct SNode *Stack;

注意:如果堆栈已满,Push函数必须输出“Stack Full”并且返回false;如果某堆栈是空的,则Pop函数必须输出“Stack Tag Empty”(其中Tag是该堆栈的编号),并且返回ERROR

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

#define ERROR 1e8
typedef int ElementType;
typedef enum { push, pop, end } Operation;
typedef enum { false, true } bool;
typedef int Position;
struct SNode {
    ElementType *Data;
    Position Top1, Top2;
    int MaxSize;
};
typedef struct SNode *Stack;

Stack CreateStack( int MaxSize );
bool Push( Stack S, ElementType X, int Tag );
ElementType Pop( Stack S, int Tag );

Operation GetOp();  /* details omitted */
void PrintStack( Stack S, int Tag ); /* details omitted */

int main()
{
    int N, Tag, X;
    Stack S;
    int done = 0;

    scanf("%d", &N);
    S = CreateStack(N);
    while ( !done ) {
        switch( GetOp() ) {
        case push: 
            scanf("%d %d", &Tag, &X);
            if (!Push(S, X, Tag)) printf("Stack %d is Full!\n", Tag);
            break;
        case pop:
            scanf("%d", &Tag);
            X = Pop(S, Tag);
            if ( X==ERROR ) printf("Stack %d is Empty!\n", Tag);
            break;
        case end:
            PrintStack(S, 1);
            PrintStack(S, 2);
            done = 1;
            break;
        }
    }
    return 0;
}

/* 你的代码将被嵌在这里 */

二、解题思路

一个数组实现双栈,其实就是数组的0下标处作为栈1的第一个位置,数组的MaxSize - 1下标处作为栈2的第一个位置
Stack->Top1的含义是什么呢,它的含义是当前栈的栈顶在哪个下标处。考虑初始情况,也就是栈内一个元素都没有的情况,那么此时Stack->Top1应该为-1而不是0Stack->Top2与之同理,起始时应该为MaxSize而不是MaxSize - 1
CreateStack的时候,千万不要忘记申请内存。第一次为结构体指针申请,第二次为结构体的指针成员申请。

三、解题代码

Stack CreateStack(int MaxSize) {
  	struct SNode* tmp = (struct SNode*)malloc(sizeof(struct SNode));
  	if(!tmp)	exit(0);
  	tmp->MaxSize = MaxSize;
  	tmp->Data = (ElementType*)malloc(sizeof(ElementType) * MaxSize);
  	if(!tmp->Data)	exit(0);
  	tmp->Top1 = -1;
  	tmp->Top2 = MaxSize;
 		return tmp;
}
bool Push(Stack S, ElementType X, int Tag){
  	if(Tag == 1){
      	if(S->Top1 + 1 == S->Top2){
          	printf("Stack Full\n");
          	return false;
        }
      	else	S->Data[++S->Top1] = X; 
    }
  	else if(Tag == 2){
      	if(S->Top2 - 1 == S->Top1){
          	printf("Stack Full\n");
          	return false;
        }
      	else	S->Data[--S->Top2] = X;
    }
  	else 	return false;
  	return true;
}
ElementType Pop(Stack S, int Tag){
  	ElementType ret;
  	if(Tag == 1){
      	if(S->Top1 == -1){
          	printf("Stack %d Empty\n", Tag);
          	return ERROR;
        }
      	else	ret = S->Data[S->Top1--];
    }
  	else if(Tag == 2){
        if(S->Top2 == S->MaxSize){
            printf("Stack %d Empty\n", Tag);
            return ERROR;
        }
    	  else	ret = S->Data[S->Top2++];
    }
  	return ret;
}

四、运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44587168/article/details/106045878
今日推荐