6-2-Two Stacks In One Array-函数题
解题代码
Stack CreateStack(int MaxElements) {
Stack new = (Stack)malloc(sizeof(struct StackRecord));
new->Capacity = MaxElements;
new->Top1 = -1;
new->Top2 = new->Capacity;
new->Array = (ElementType *)malloc(MaxElements * sizeof(ElementType));
return new;
}
int IsEmpty(Stack S, int Stacknum) {
if (Stacknum == 1) {
if (S->Top1 == -1) return 1;
else return 0;
}
else {
if (S->Top2 == S->Capacity) return 1;
else return 0;
}
}
int IsFull(Stack S) {
if (S->Top2 - S->Top1 == 1) return 1;
else return 0;
}
int Push(ElementType X, Stack S, int Stacknum) {
if (!IsFull(S)) {
if (Stacknum == 1) {
S->Array[++S->Top1] = X;
return 1;
}
else {
S->Array[--S->Top2] = X;
return 1;
}
}
else {
return 0;
}
}
ElementType Top_Pop(Stack S, int Stacknum) {
if (!IsEmpty(S, Stacknum)) {
if (Stacknum == 1) {
return S->Array[S->Top1--];
}
else {
return S->Array[S->Top2++];
}
}
else {
return ERROR;
}
}
测试结果
问题整理
1.这题海星。