6-18 Two Stacks In One Array(20 分)
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 分)的更多相关文章
- Array.apply(null,{length:20})与new Array(20)的区别
Array.apply(null,{length:20}) 这句代码的实际意义:创建长度为20的一个数组,但并非空数组. 跟new Array(20)的区别在于,前一种创建方式,得到的数组中的每一个元 ...
- Array.apply(null, {length: 20})和Array(20)的理解
话说今晚在学习Vue.js教程里:Render函数,这一章节是发现了一个问题,就是利用下面的这个render函数可以渲染20个重复的段落: render: function (createElemen ...
- PAT(B) 1018 锤子剪刀布(C:20分,Java:18分)
题目链接:1018 锤子剪刀布 分析 用一个二维数组保存两人所有回合的手势 甲乙的胜,平,负的次数刚好相反,用3个变量表示就可以 手势单独保存在signs[3]中,注意顺序.题目原文:如果解不唯一,则 ...
- PAT 乙级 1092 最好吃的月饼 (20 分)
1092 最好吃的月饼 (20 分) 月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种. 若想评比出一种“最好吃”的月饼,那势必在吃货界引发一场腥风血雨…… 在这里我们用数字说话,给出 ...
- 1042 Shuffling Machine (20 分)
1042 Shuffling Machine (20 分) Shuffling is a procedure used to randomize a deck of playing cards. Be ...
- PTA 10-排序4 统计工龄 (20分)
题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/721 5-13 统计工龄 (20分) 给定公司NN名员工的工龄,要求按工龄增序输出每 ...
- PTA 02-线性结构2 一元多项式的乘法与加法运算 (20分)
原题地址 https://pta.patest.cn/pta/test/15/exam/4/question/710 5-2 一元多项式的乘法与加法运算 (20分) 设计函数分别求两个一元多项式的 ...
- pat 1042 Shuffling Machine(20 分)
1042 Shuffling Machine(20 分) Shuffling is a procedure used to randomize a deck of playing cards. Bec ...
- PAT 甲级 1042 Shuffling Machine (20 分)(简单题)
1042 Shuffling Machine (20 分) Shuffling is a procedure used to randomize a deck of playing cards. ...
- L1-049 天梯赛座位分配 (20 分)
L1-049 天梯赛座位分配 (20 分)(Java解法) 天梯赛每年有大量参赛队员,要保证同一所学校的所有队员都不能相邻,分配座位就成为一件比较麻烦的事情.为此我们制定如下策略:假设某赛场有 N 所 ...
随机推荐
- MySQL中特有的函数If函数
上面我们已经知道了case函数可以实现逻辑判断,可以是很复杂的逻辑判断,但是如果我们只想实现的是如果这个条件成立就返回A否则就返回B这样简单的逻辑如果用case的话,未免复杂了.我们可以使用if函数来 ...
- ruby on rails validates
validates :conclusion, :presence => true, :inclusion => { :in => [0,1] } validates :email, ...
- java基础类型中的char和byte的辨析及Unicode编码和UTF-8的区别
在平常工作中使用到char和byte的场景不多,但是如果项目中使用到IO流操作时,则必定会涉及到这两个类型,下面让我们一起来回顾一下这两个类型吧. char和byte的对比 byte byte 字节, ...
- java Pattern(正则)类
Pattern的静态方法matches 用于快速匹配字符串,该方法适合用于只匹配一次,且匹配全部字符串. Boolean b=Pattern.matches("^((13[0-9])|(15 ...
- uva1452
这题说的是给了一个序列1到n然后你从1 位置开始数k个删除,直到最后每个数都被删除了.我们考虑1个数在被删除前后的位置,比如现在在i位置那么 回到删除当前的最后一个是 (i+k)L 了为删除前 的 个 ...
- 使用Sed和Awk实现批量文件的文本替换
摘要: 使用 Sed 完成文本替换操作任务是非常合适的.结合 find 命令,即可实现指定批量文件的文本替换.同时给出了Awk的解决方案作为对比. 问题 现在, 我要将一个原有Java项目中的一些包及 ...
- 网关服务Spring Cloud Gateway(一)
Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gateway ,相比之前我们使用的 Zuul(1.x) 它有哪些优势呢?Zuul(1.x) 基于 Servlet,使 ...
- PHP获取代码段执行的毫秒时间和消耗内存
我们在项目开发经常需要做一些优化型测试,比如优化代码段,排查代码段效率问题,或者降低内存消耗成本. <?php $start_memory = memory_get_usage(); //开始内 ...
- # 20145106 《Java程序设计》第4周学习总结
教材学习内容总结 翻开第六章的书,发现书中的例子居然是"假设我正在开发一款rpg游戏" public class Magician extends Role { public vo ...
- linux内核分析第八周-理解进程调度时机跟踪分析进程调度与进程切换的过程
实验原理: 一.调度时机 不同类型的进程有不同的调度需求 第一种分类: I/O-bound 频繁的进行I/O 通常会花费很多时间等待I/O操 ...