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


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

    函数接口定义:

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

    其中Tag是堆栈编号,取1或2;MaxSize堆栈数组的规模;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。

    裁判测试程序样例:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 #define ERROR 1e8
     5 typedef int ElementType;
     6 typedef enum { push, pop, end } Operation;
     7 typedef enum { false, true } bool;
     8 typedef int Position;
     9 struct SNode {
    10     ElementType *Data;
    11     Position Top1, Top2;
    12     int MaxSize;
    13 };
    14 typedef struct SNode *Stack;
    15 
    16 Stack CreateStack( int MaxSize );
    17 bool Push( Stack S, ElementType X, int Tag );
    18 ElementType Pop( Stack S, int Tag );
    19 
    20 Operation GetOp();  /* details omitted */
    21 void PrintStack( Stack S, int Tag ); /* details omitted */
    22 
    23 int main()
    24 {
    25     int N, Tag, X;
    26     Stack S;
    27     int done = 0;
    28 
    29     scanf("%d", &N);
    30     S = CreateStack(N);
    31     while ( !done ) {
    32         switch( GetOp() ) {
    33         case push: 
    34             scanf("%d %d", &Tag, &X);
    35             if (!Push(S, X, Tag)) printf("Stack %d is Full!
    ", Tag);
    36             break;
    37         case pop:
    38             scanf("%d", &Tag);
    39             X = Pop(S, Tag);
    40             if ( X==ERROR ) printf("Stack %d is Empty!
    ", Tag);
    41             break;
    42         case end:
    43             PrintStack(S, 1);
    44             PrintStack(S, 2);
    45             done = 1;
    46             break;
    47         }
    48     }
    49     return 0;
    50 }
    51 
    52 /* 你的代码将被嵌在这里 */

    输入样例:

    5
    Push 1 1
    Pop 2
    Push 2 11
    Push 1 2
    Push 2 12
    Pop 1
    Push 2 13
    Push 2 14
    Push 1 3
    Pop 2
    End

    输出样例:

    Stack 2 Empty
    Stack 2 is Empty!
    Stack Full
    Stack 1 is Full!
    Pop from Stack 1: 1
    Pop from Stack 2: 13 12 11

    函数实现细节:

     1 Stack CreateStack( int MaxSize ){
     2     Stack S=(Stack)malloc(sizeof(struct SNode));
     3     S->Data=(ElementType *)malloc(MaxSize*sizeof(ElementType));
     4     S->Top1=-1;
     5     S->Top2=MaxSize;
     6     S->MaxSize=MaxSize;
     7     return S;
     8 }
     9 
    10 bool Push( Stack S, ElementType X, int Tag ){
    11     if(S->Top1+1==S->Top2){
    12         printf("Stack Full
    ");
    13         return false;
    14     }
    15     if(Tag==1){
    16         S->Data[++S->Top1]=X;
    17     }
    18     else{
    19         S->Data[--S->Top2]=X;
    20     }
    21     return true;
    22 }
    23 
    24 ElementType Pop( Stack S, int Tag ){
    25     if(Tag==1){
    26         if(S->Top1==-1){
    27             printf("Stack %d Empty
    ",Tag);
    28             return ERROR;
    29         }
    30         return S->Data[S->Top1--];
    31     }
    32     else if(Tag==2){
    33         if(S->Top2==S->MaxSize){
    34             printf("Stack %d Empty
    ",Tag);
    35             return ERROR;
    36         }
    37         return S->Data[S->Top2++];
    38     }
    39      
    40 }
  • 相关阅读:
    project 2013 激活 key 7YHNW-RVCQY-VBDB2-QX69Q-B96WK viso 66DNF-28W69-W4PPV-W3VYT-TJDBQ
    电脑快捷键
    Error (167005): Can't assign I/O pad "GX_TX" to PIN_AG27 because this causes failure in the placement of the other atoms in its associated channel
    学习资源
    数值孔径
    网络通信芯片
    DMD数字微镜
    运放输入阻抗
    DS18B20测温
    LED灯开关电路
  • 原文地址:https://www.cnblogs.com/wyjgr/p/13073507.html
Copyright © 2020-2023  润新知