C语言实现:优(欠)美(揍)的双向顺序栈

  • 实现完毕后把能去掉的花括号全部给去掉了,就成这样了
#include 
#include 

#define MaxSize 10
typedef struct DEndStack
{
    int top[2];
    int data[MaxSize];
} DEndStack;

struct DEndStack *InitStack()
{
    struct DEndStack *S = (struct DEndStack *)malloc(sizeof(struct DEndStack));
    S->top[0] = -1;
    S->top[1] = MaxSize;
    return S;
}

//i表示用哪个栈
int push(struct DEndStack *S, int x, int i)
{
    //满
    if (S->top[0] == S->top[1] - 1) return 0;
    //未满
    else
        if (i == 0) S->data[++(S->top[i])] = x;
        else    S->data[--(S->top[i])] = x;
    return 1;
}
int pop(struct DEndStack *S, int i)
{
    int res;
    if (i == 0)
        if (S->top[i] == 0) return 0;
        else    res = S->data[S->top[i]--];
    else
        if (S->top[i] == MaxSize)   return 0;
        else    res = S->data[S->top[i]++];
    return res;
}

int main()
{
    struct DEndStack *S = InitStack();
    push(S, 1, 1);
    pop(S, 1);
    return 0;
}

你可能感兴趣的:(#,算法练习C语言版)