在一个数组中实现两个堆栈

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。

裁判测试程序样例:

#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;
}

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

输入样例:

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 #include <cstdlib>
  2 #include <iostream>
  3 #define ERROR 1e8
  4 typedef int ElementType;
  5 typedef enum{
  6     push, pop, end
  7 }Operation;
  8 
  9 typedef int Position;
 10 struct SNode{
 11     ElementType *Data;
 12     Position Top1, Top2;
 13     int MaxSize;
 14 };
 15 typedef struct SNode *Stack;
 16 
 17 
 18 using namespace std;
 19 
 20 Operation GetOp(){
 21     string s1;
 22     cin >> s1;
 23     if(s1 == "Push"){
 24         return push;
 25     }
 26     if(s1 == "Pop"){
 27         return pop;
 28     }
 29     if(s1 == "End"){
 30         return end;
 31     }
 32 }
 33 
 34 void PrintStack(Stack S, int Tag){
 35     //Tag的作用是选择栈 
 36     if(Tag == 1){
 37         cout << "Pop from Stack" << Tag;
 38         while((S->Top1) != -1){//循环终止的条件是栈为空 
 39             cout << S->Data[(S->Top1)--];
 40         }
 41         cout << endl;
 42     }else{
 43         printf("Pop from Stack %d:", Tag);
 44         while((S->Top2) != S->MaxSize ){//当栈二为空时栈指针在数组的最大值处 
 45             cout << S->Data[(S->Top2)++];
 46             
 47         }
 48         cout << endl;
 49     }
 50 }
 51 Stack CreateStack(int MaxSize){
 52     //建栈并初始化 
 53     Stack S = new struct SNode();
 54     S->MaxSize = MaxSize;
 55     S->Data = new int[MaxSize];
 56     S->Top2 = MaxSize;//分别将栈顶指针指到初始位置 
 57     S->Top1 = -1;
 58     return S;
 59 }
 60 bool Push(Stack S, ElementType X, int Tag){//将元素押入相应的栈中 
 61     if(S == NULL){//还未初始化 
 62         return false;
 63     }    
 64     if(S->Top1 + 1 == S->Top2){//当俩个栈顶指针相邻时栈满 
 65         cout << "Stack Full\n";
 66         return false;
 67     }
 68     if(Tag == 1){//选择栈1 
 69         S->Data[++(S->Top1)] = X;
 70         return true;
 71     }
 72     else if(Tag == 2){
 73         S->Data[(S->Top2)--] = X;
 74         return true;
 75     }
 76     return false;
 77 }
 78 
 79 ElementType Pop(Stack S, int Tag){//弹出对应栈的栈顶元素 
 80     if(S == NULL){//栈未初始化 
 81         return ERROR;
 82     }
 83     if(Tag == 1){
 84         if(-1 == S->Top1){//栈顶指针在初始位置 
 85             cout << "Stack " << Tag << " Empty\n";
 86             return ERROR;
 87         }
 88         return S->Data[(S->Top1)--];
 89     }else if(Tag == 2){
 90         if(S->MaxSize == S->Top2){
 91             cout << "Stack " << Tag << " Empty\n";
 92             return ERROR;
 93         }
 94         return S->Data[(S->Top2)++];
 95     }
 96     return ERROR;
 97 }
 98 int main(){
 99     int N, Tag, X;
100     Stack S;
101     int done = 0;
102     cin >> N;
103     S = CreateStack(N);//建栈 
104     while(!done){
105         switch(GetOp()){
106             case push://push == 0 
107                 cin >> Tag >> X;
108                 if(!Push(S, X, Tag)){
109                     cout << "Stack " << Tag << " is Full!\n";
110                     
111                 }
112                 break;
113             case pop: // pop == 1; 
114                 cin >> Tag;
115                 X = Pop(S, Tag);
116                 if(X == ERROR){
117                     printf("Stack %d is Empty!\n", Tag);
118                 }
119                 break;
120             case end: // pop == 2;
121                 PrintStack(S, 1);
122                 PrintStack(S, 2);
123                 done = 1;
124                 break;
125         }
126     }
127     return 0;
128 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值