#include <stdio.h>
#include <stdlib.h>
#define ERROR 1e8
typedef int ElementType;
//typedef enum { push, pop, end } Operation;
//typedef num {false = 0,true = 1}bool;
typedef int Position;
struct SNode {
ElementType *Data;// 利用数组的时候要求必然是一个指针 S->Data[S->Top1]
Position Top1, Top2;
int MaxSize;
};
typedef struct SNode *Stack;
Stack CreateStack( int MaxSize );
int Push( Stack S, ElementType X, int Tag );
ElementType Pop( Stack S, int Tag );
int 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 1:
scanf("%d %d", &Tag, &X);
if (!Push(S, X, Tag)) printf("Stack %d is Full!\n", Tag);
break;
case 2:
scanf("%d", &Tag);
X = Pop(S, Tag);
if ( X==ERROR ) printf("Stack %d is Empty!\n", Tag);
break;
case 3:
PrintStack(S, 1);
PrintStack(S, 2);
done = 1;
break;
}
}
return 0;
}
/* 你的代码将被嵌在这里 */
Stack CreateStack(int MaxSize) {
Stack stack = (Stack)malloc(sizeof(struct SNode));
stack->Data = (int *)malloc(sizeof(ElementType)* MaxSize);
stack->Top1 = -1;
stack->Top2 = MaxSize;
stack->MaxSize = MaxSize;
return stack;
}
bool Push(Stack S, ElementType X, int Tag) {
if (S == NULL)return false;
if(S->Top1+1==S->Top2) {
printf("Stack Full\n");
return 0;
}
if(Tag==1) {
// S->Data = (int *)malloc(sizeof(ElementType)* MaxSize);
S->Top1++;
S->Data[S->Top1] = X;
return 1;
} else {
S->Top2--;
S->Data[S->Top2] = X;
return 1;
}
}
ElementType Pop( Stack S, int Tag ) { //如果某堆栈是空的,则Pop函数必须输出“Stack Tag(1或2) Empty”(其中Tag是该堆栈的编号),并且返回ERROR。
if (S == NULL)return ERROR;
if(Tag==1) {
if(S->Top1==-1) {
printf("Stack 1 Empty\n");
return ERROR;
}
return S->Data[ S->Top1--];// S->Data = (int *)malloc(sizeof(ElementType)* MaxSize);
} else {
if(S->Top2==S->MaxSize) {
printf("Stack 2 Empty\n");
return ERROR;
}
return S->Data[S->Top2++];// S->Data = (int *)malloc(sizeof(ElementType)* MaxSize);
}
}
int GetOp() {
char Push[] = "1";
char Pop[] = "2";
char End[] = "3";
char s[100];
scanf("%s", s);
if ((Push, s) == 0)return 1;
if ((Pop, s) == 0)return 2;
if ((End, s) == 0)return 3;
}
void PrintStack(Stack S, int Tag) {
printf("Pop from Stack %d:", Tag);
if (Tag == 1) {
while (S->Top1 != -1) {
printf(" %d", S->Data[S->Top1--]);
}
} else {
while (S->Top2 != S->MaxSize) {
printf(" %d", S->Data[S->Top2++]);
}
}
putchar('\n');
}