#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define SElemType int
typedef struct {
    int top[2], bot[2]; // 栈顶和栈底指针
    SElemType *V;       // 栈数组
    int m;              // 栈最大可容纳元素个数
} DblStack;

#include <stdbool.h>
#include <stdlib.h>
// 初始化
DblStack *InitStack(int maxsize) {
    DblStack *ptr = (DblStack *)malloc(sizeof(DblStack));
    ptr->V = (SElemType *)malloc(maxsize * sizeof(SElemType));
    if (ptr->V == NULL)
        exit(0); // 内存分配失败
    ptr->m = maxsize;
    ptr->top[0] = ptr->bot[0] = -1;
    ptr->top[1] = ptr->bot[1] = ptr->m;
    return ptr;
}
// 判断栈空，sign为栈编号，空返回true
bool StackEmpty(DblStack S, int sign) {
    sign = !!sign; // 默认将非0号栈转为1号栈
    if (S.top[sign] == S.bot[sign])
        return true;
    else
        return false;
}
// 判断栈满，满返回true
bool StackFull(DblStack S) {
    if (S.top[0] + 1 == S.top[1])
        return true;
    else
        return false;
}
// 进栈，sign为栈编号，elem为进栈元素，成功返回true
bool push(DblStack *ptr, int sign, SElemType elem) {
    if (!StackFull(*ptr)) {
        switch (sign) {
        case 0:
            ptr->V[++ptr->top[0]] = elem;
            return true;
        case 1:
            ptr->V[--ptr->top[1]] = elem;
            return true;
        }
    }
    return false;
}
// 出栈，sign为栈编号，用elem返回出栈元素，成功返回true
bool pop(DblStack *ptr, int sign, SElemType *elem) {
    if (!StackEmpty(*ptr, sign)) {
        switch (sign) {
        case 0:
            *elem = ptr->V[ptr->top[0]--];
            return true;
        case 1:
            *elem = ptr->V[ptr->top[1]++];
            return true;
        }
    }
    return false;
}

int main(void) {
    DblStack *ptr = InitStack(5);
    SElemType *elem;
    if (StackEmpty(*ptr, 0))
        printf("0号栈空\n");
    else
        printf("0号栈不空\n");
    if (StackEmpty(*ptr, 1))
        printf("1号栈空\n");
    else
        printf("1号栈不空\n");
    if (push(ptr, 0, 1))
        printf("入栈成功\n");
    else
        printf("入栈失败\n");
    if (push(ptr, 0, 2))
        printf("入栈成功\n");
    else
        printf("入栈失败\n");
    if (push(ptr, 0, 3))
        printf("入栈成功\n");
    else
        printf("入栈失败\n");
    if (StackEmpty(*ptr, 0))
        printf("0号栈空\n");
    else
        printf("0号栈不空\n");
    if (push(ptr, 1, 4))
        printf("入栈成功\n");
    else
        printf("入栈失败\n");
    if (push(ptr, 1, 5))
        printf("入栈成功\n");
    else
        printf("入栈失败\n");
    if (StackEmpty(*ptr, 1))
        printf("1号栈空\n");
    else
        printf("1号栈不空\n");
    if (StackFull(*ptr))
        printf("栈满\n");
    else
        printf("栈不满\n");
    if (push(ptr, 1, 6))
        printf("入栈成功\n");
    else
        printf("入栈失败\n");
    if (pop(ptr, 0, elem))
        printf("出栈成功 %d \n", *elem);
    else
        printf("出栈失败\n");
    if (pop(ptr, 1, elem))
        printf("出栈成功 %d \n", *elem);
    else
        printf("出栈失败\n");
    if (pop(ptr, 1, elem))
        printf("出栈成功 %d \n", *elem);
    else
        printf("出栈失败\n");
    if (pop(ptr, 1, elem))
        printf("出栈成功 %d \n", *elem);
    else
        printf("出栈失败\n");
    if (pop(ptr, 10, elem))
        printf("出栈成功 %d \n", *elem);
    else
        printf("出栈失败\n");
    return 0;
}