6-1 在一个数组中实现两个堆栈 (29 分)
本题要求在一个数组中实现两个堆栈。
函数接口定义:
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
#include
#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
这道题目非常简单,但是我做了半个多小时才看懂题。
怎么说呢?就是在一个数组里面实现两个栈,一个从左往右,一个从右往左。俩索引,top1,top2,控制一下栈空和栈满,没别的东西了。
花我时间最多的是,stack里面的data。我前半个多小时完全不知道初始化函数Stack CreateStack( int MaxSize )怎么写,直到我看了第二题才发现,奥,原来是一个栈的结构体里面有一个data数组……之前我一直不知道数组存在哪。
另外这个题目最坑的地方——换行和Tag。
第一次找错,我直接把Pop函数里面的东西黏贴成了Stack Tag Empty。
后来找了半天错才发现,奥,Tag特么的是个变量输出……
改了,我靠,还不对啊?!
嗷!特么这两句话还得换行……
反正就是做的蛮生气的。
代码黏在下面:
Stack CreateStack( int MaxSize ){
Stack stack = (Stack)malloc(sizeof(struct SNode));
stack->Data = (ElementType *)malloc(sizeof(ElementType)* MaxSize);
stack->MaxSize = MaxSize;
stack->Top1 = -1;
stack->Top2 = stack->MaxSize;
return stack;
}
bool Push( Stack S, ElementType X, int Tag ){
if(S->Top1+1==S->Top2){
printf("Stack Full\n");
return false;
}
if(Tag==1)
S->Data[++(S->Top1)]=X;
else
S->Data[--(S->Top2)]=X;
return true;
}
ElementType Pop( Stack S, int Tag ){
if(Tag==1){
if(S->Top1==-1){
printf("Stack %d Empty\n",Tag);
return ERROR;
}
else{
return S->Data[(S->Top1)--];
}
}
else{
if(S->Top2==S->MaxSize){
printf("Stack %d Empty\n",Tag);
return ERROR;
}
else{
return S->Data[(S->Top2)++];
}
}
}
6-2 另类堆栈 (21 分)
在栈的顺序存储实现中,另有一种方法是将Top定义为栈顶的上一个位置。请编写程序实现这种定义下堆栈的入栈、出栈操作。如何判断堆栈为空或者满?
函数接口定义:
bool Push( Stack S, ElementType X );
ElementType Pop( Stack S );
其中Stack结构定义如下:
typedef int Position;
typedef struct SNode *PtrToSNode;
struct SNode {
ElementType *Data; /* 存储元素的数组 */
Position Top; /* 栈顶指针 */
int MaxSize; /* 堆栈最大容量 */
};
typedef PtrToSNode Stack;
注意:如果堆栈已满,Push函数必须输出“Stack Full”并且返回false;如果队列是空的,则Pop函数必须输出“Stack Empty”,并且返回ERROR。
裁判测试程序样例:
#include
#include
#define ERROR -1
typedef int ElementType;
typedef enum { push, pop, end } Operation;
typedef enum { false, true } bool;
typedef int Position;
typedef struct SNode *PtrToSNode;
struct SNode {
ElementType *Data; /* 存储元素的数组 */
Position Top; /* 栈顶指针 */
int MaxSize; /* 堆栈最大容量 */
};
typedef PtrToSNode Stack;
Stack CreateStack( int MaxSize )
{
Stack S = (Stack)malloc(sizeof(struct SNode));
S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
S->Top = 0;
S->MaxSize = MaxSize;
return S;
}
bool Push( Stack S, ElementType X );
ElementType Pop( Stack S );
Operation GetOp(); /* 裁判实现,细节不表 */
void PrintStack( Stack S ); /* 裁判实现,细节不表 */
int main()
{
ElementType X;
Stack S;
int N, done = 0;
scanf("%d", &N);
S = CreateStack(N);
while ( !done ) {
switch( GetOp() ) {
case push:
scanf("%d", &X);
Push(S, X);
break;
case pop:
X = Pop(S);
if ( X!=ERROR ) printf("%d is out\n", X);
break;
case end:
PrintStack(S);
done = 1;
break;
}
}
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
4
Pop
Push 5
Push 4
Push 3
Pop
Pop
Push 2
Push 1
Push 0
Push 10
End
输出样例:
Stack Empty
3 is out
4 is out
Stack Full
0 1 2 5
做完第一题,这道题目就是小case啦~我觉得这俩题顺序放反了,这个题和第一题相比就是双向变成了单向,还不用自己实现creatstack函数,简直不知道简单几倍。
无话可说,直接上代码:
bool Push( Stack S, ElementType X ){
if(S->Top==S->MaxSize){
printf("Stack Full\n");
return false;
}
else{
S->Data[(S->Top)++]=X;
return true;
}
}
ElementType Pop( Stack S ){
if(S->Top==0){
printf("Stack Empty\n");
return ERROR;
}
else{
return S->Data[--(S->Top)];
}
}