Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used.

Format of functions:

Stack CreateStack( int MaxElements );
int IsEmpty( Stack S, int Stacknum );
int IsFull( Stack S );
int Push( ElementType X, Stack S, int Stacknum );
ElementType Top_Pop( Stack S, int Stacknum );

where int Stacknum is the index of a stack which is either 1 or 2; int MaxElements is the size of the stack array; and Stack is defined as the following:

typedef struct StackRecord *Stack;
struct StackRecord {
int Capacity; /* maximum size of the stack array */
int Top1; /* top pointer for Stack 1 */
int Top2; /* top pointer for Stack 2 */
ElementType *Array; /* space for the two stacks */
}

Note: Push is supposed to return 1 if the operation can be done successfully, or 0 if fails. If the stack is empty, Top_Pop must return ERROR which is defined by the judge program.

Sample program of judge:

#include <stdio.h>
#include <stdlib.h>
#define ERROR 1e8
typedef int ElementType;
typedef enum { push, pop, end } Operation; typedef struct StackRecord *Stack;
struct StackRecord {
int Capacity; /* maximum size of the stack array */
int Top1; /* top pointer for Stack 1 */
int Top2; /* top pointer for Stack 2 */
ElementType *Array; /* space for the two stacks */
}; Stack CreateStack( int MaxElements );
int IsEmpty( Stack S, int Stacknum );
int IsFull( Stack S );
int Push( ElementType X, Stack S, int Stacknum );
ElementType Top_Pop( Stack S, int Stacknum ); Operation GetOp(); /* details omitted */
void PrintStack( Stack S, int Stacknum ); /* details omitted */ int main()
{
int N, Sn, X;
Stack S;
int done = 0; scanf("%d", &N);
S = CreateStack(N);
while ( !done ) {
switch( GetOp() ) {
case push:
scanf("%d %d", &Sn, &X);
if (!Push(X, S, Sn)) printf("Stack %d is Full!\n", Sn);
break;
case pop:
scanf("%d", &Sn);
X = Top_Pop(S, Sn);
if ( X==ERROR ) printf("Stack %d is Empty!\n", Sn);
break;
case end:
PrintStack(S, 1);
PrintStack(S, 2);
done = 1;
break;
}
}
return 0;
} /* Your function will be put here */

Sample Input:

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

Sample Output:

Stack 2 is Empty!
Stack 1 is Full!
Pop from Stack 1: 1
Pop from Stack 2: 13 12 11
代码:
Stack CreateStack( int MaxElements )
{
Stack p = (Stack)malloc(sizeof(struct StackRecord));
p -> Array = (int *)malloc(sizeof(MaxElements));
p -> Capacity = MaxElements;
p -> Top1 = -;
p -> Top2 = MaxElements;
return p;
}
int IsEmpty( Stack S, int Stacknum )
{
if(Stacknum == )
{
if(S -> Top1 == -)return ;
return ;
}
else
{
if(S -> Top2 == S -> Capacity)return ;
return ;
}
}
int IsFull( Stack S )
{
if(S -> Top2 - S -> Top1 == )return ;
return ;
}
int Push( ElementType X, Stack S, int Stacknum )
{
if(IsFull(S))return ;
if(Stacknum == )S -> Array[++ (S -> Top1)] = X;
else S -> Array[-- (S -> Top2)] = X;
return ;
}
ElementType Top_Pop( Stack S, int Stacknum )
{
if(IsEmpty(S,Stacknum))return ERROR;
if(Stacknum == )return S -> Array[(S -> Top1) --];
else return S -> Array[(S -> Top2) ++];
}

6-18 Two Stacks In One Array(20 分)的更多相关文章

  1. Array.apply(null,{length:20})与new Array(20)的区别

    Array.apply(null,{length:20}) 这句代码的实际意义:创建长度为20的一个数组,但并非空数组. 跟new Array(20)的区别在于,前一种创建方式,得到的数组中的每一个元 ...

  2. Array.apply(null, {length: 20})和Array(20)的理解

    话说今晚在学习Vue.js教程里:Render函数,这一章节是发现了一个问题,就是利用下面的这个render函数可以渲染20个重复的段落: render: function (createElemen ...

  3. PAT(B) 1018 锤子剪刀布(C:20分,Java:18分)

    题目链接:1018 锤子剪刀布 分析 用一个二维数组保存两人所有回合的手势 甲乙的胜,平,负的次数刚好相反,用3个变量表示就可以 手势单独保存在signs[3]中,注意顺序.题目原文:如果解不唯一,则 ...

  4. PAT 乙级 1092 最好吃的月饼 (20 分)

    1092 最好吃的月饼 (20 分) 月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种. 若想评比出一种“最好吃”的月饼,那势必在吃货界引发一场腥风血雨…… 在这里我们用数字说话,给出 ...

  5. 1042 Shuffling Machine (20 分)

    1042 Shuffling Machine (20 分) Shuffling is a procedure used to randomize a deck of playing cards. Be ...

  6. PTA 10-排序4 统计工龄 (20分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/721 5-13 统计工龄   (20分) 给定公司NN名员工的工龄,要求按工龄增序输出每 ...

  7. PTA 02-线性结构2 一元多项式的乘法与加法运算 (20分)

    原题地址 https://pta.patest.cn/pta/test/15/exam/4/question/710 5-2 一元多项式的乘法与加法运算   (20分) 设计函数分别求两个一元多项式的 ...

  8. pat 1042 Shuffling Machine(20 分)

    1042 Shuffling Machine(20 分) Shuffling is a procedure used to randomize a deck of playing cards. Bec ...

  9. PAT 甲级 1042 Shuffling Machine (20 分)(简单题)

    1042 Shuffling Machine (20 分)   Shuffling is a procedure used to randomize a deck of playing cards. ...

  10. L1-049 天梯赛座位分配 (20 分)

    L1-049 天梯赛座位分配 (20 分)(Java解法) 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所 ...

随机推荐

  1. android中代码操作外部SD卡出错:W/System.err(1595): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)

    AndroidManifest.xml 中加上: <uses-permission android:name="android.permission.WRITE_EXTERNAL_ST ...

  2. MyEclipse优化,解决MyEclipse运行慢、卡顿问题

    工具: myeclipse2015 2.0 最近想用myeclipse做一下测试,发现myeclipse运行非常卡,直接影响代码的开发,而且还出现软件卡退的情况,让我十分恼火.   一.加大JVM的非 ...

  3. InFusion错误类型分析

    1       God Class 1.1     特征 上帝类通常过多的操纵其他类的数据,从而破坏了类的封装性.上帝类从其他类中获得功能,同时增加了自身的耦合性,通常会导致自己具有规模过大和较高的复 ...

  4. HTTP从入门到入土(2)——HTTP协议发展历史

    HTTP协议的发展历史 HTTP/0.9 0.9版本是第一个定稿的HTTP版本,相对较为简陋.它有以下特点: 只有一个命令GET 没有header等描述数据的信息 服务器发送完毕,就关闭TCP连接 注 ...

  5. linux内核分析第三周-跟踪分析Linux内核的启动过程

    一.实验流程 1.打开环境 执行命令:cd LinuxKernel/ 执行命令:qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd root ...

  6. Python3基础 print %d 输出整数

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. POJ 1797 Heavy Transportation(最短路&Dijkstra变体)题解

    题意:给你所有道路的载重,找出从1走到n的所有路径中载重最大的,即路径最小值的最大值. 思路:和之前的POJ3268很像.我们用Dijkstra,在每次查找时,我们把最大的先拿出来,因为最大的不影响最 ...

  8. Mac下配置Hive环境

    在配置Hive环境之前,需要Hadoop环境. 安装Hive 点击下载 下载结束后,会有一个.tar文件,使用以下命令解压该文件. tar -zxvf 要解压的tar包 解压完成后如下 修改Hive配 ...

  9. UVa 1637 纸牌游戏(全概率公式)

    https://vjudge.net/problem/UVA-1637 题意: 36张牌分成9堆,每堆4张牌.每次可以拿走某两堆顶部的牌,但需要点数相同.每种拿法的概率均为1/5.求成功概率. 思路: ...

  10. C++中的动态绑定

    C++中基类和派生类遵循类型兼容原则:即可用派生类的对象去初始化基类的对象,可用派生类的对象去初始化基类的引用,可用派生类对象的地址去初始化基类对象指针. C++中动态绑定条件发生需要满足2个条件: ...