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 所 ...
随机推荐
- php array_map array_filter sort
array_map — Applies the callback to the elements of the given arrays (处理映射) array_filter — Filters e ...
- 91. Decode Ways(动态规划 26个字母解码个数)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- python一段代码 感受一下
class T(): def aa(self): write = 1 print '123' class B(): def hehe(self) ...
- Python笔记 #10# Histograms
1.Build a histogram In [1]: help(plt.hist) Help on function hist in module matplotlib.pyplot: hist(x ...
- Python3.x:函数定义
Python3.x:函数定义 1,函数定义: def 函数名称([参数1,参数2,参数3......]): 执行语句 2,实例一(不带参数和没返回值): def helloWorld(): print ...
- 20145204 《Java程序设计》第四周学习总结
20145204 <Java程序设计>第四周学习总结 教材学习内容总结 继承 什么时候使用继承? 当多个类中出现重复定义的行为(即多个类中出现重复的代码)时,就把相同的程序代码提成为父类. ...
- 20145221 《Java程序设计》第一周学习总结
20145221 <Java程序设计>第一周学习总结 教材学习内容总结 第一周内容已在假期完成,详见博客: Hello Java! 开源中国的代码托管 代码调试中的问题和解决过程 第一周内 ...
- Windows10下用Anaconda3安装TensorFlow教程【转】
本文转载自:https://www.cnblogs.com/HongjianChen/p/8385547.html 1. 安装好Anaconda3版本 (1) 注:可以发现最新版本是Anaconda5 ...
- How to install tensorflow on ubuntu 18.04 64bit
Ans:pip install tensorflow (note: version number of pip and python must be consistent)
- Asynchronous Programming Model (APM)异步编程模型
https://msdn.microsoft.com/zh-cn/library/ms228963(v=vs.110).aspx 一.概念 An asynchronous operation that ...