2-5 Two Stacks In One Array
<两栈共享存储空间(双端堆栈)>
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:
其中int Stacknum是堆栈的索引,它是1或2;int MaxElements是堆栈数组的大小;stack定义如下:
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.
注意:如果操作成功,Push应该返回1;如果失败,则返回0。如果堆栈为空,Top_Pop必须返回由judge程序定义的错误。
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
AC代码:
堆栈的题目我感觉很生涩,可能是之前都没有接触过吧。这个题,刚开始的时候我连题目都没有理解,后来过了好几天,我在B站找到了一个讲解视频,感觉好不错呢,稍稍知道了这个题目是什么意思,我把链接给你们搬过来了,就在下面,自己看完消化一下吧!
https://www.bilibili.com/video/BV1LE411J7x2
这是看过视频之后,自己借鉴着写的,很多时候还是不知道该怎么处理,但是啊,必须得学会啊!
<我是分割线>
C语言的堆栈不是很好用,好多东西都得自己来写,但是C++就比较友好了。
//创建一个新的堆栈,这是还要为指针数组申请空间
Stack CreateStack( int MaxElements ){
Stack S=(Stack)malloc(sizeof(struct StackRecord));
S->Capacity=MaxElements;
S->Array=(ElementType *)malloc(sizeof(ElementType)*MaxElements);
S->Top1=-1;
S->Top2=MaxElements;
return S;
}
//判断堆栈是否为空
int IsEmpty( Stack S, int Stacknum ){
if(S->Top1==-1&&Stacknum==1)
return 1;
if(S->Top2==S->Capacity&&Stacknum==2)
return 1;
return 0;
}
//判断堆栈是否已满
int IsFull( Stack S ){
if(S->Top2-S->Top1==1)
return 1;
return 0;
}
//向堆栈Stacknum中压入数据 ,要先判断是否已满
int Push( ElementType X, Stack S, int Stacknum ){
if(!IsFull(S))
{
if(Stacknum==1)
{
S->Array[++(S->Top1)]=X;
}
else if(Stacknum==2)
{
S->Array[--(S->Top2)]=X;
}
return 1;
}
else
return 0;
}
//从堆栈中取出元素,不改变堆栈
ElementType Top_Pop( Stack S, int Stacknum ){
if(IsEmpty(S,Stacknum))
return ERROR;
if(Stacknum==1)
return S->Array[(S->Top1)--];
if(Stacknum==2)
return S->Array[(S->Top2)++];
}