数据结构(CC++)——顺序栈

    技术2022-07-27  80

    #include<stdio.h> #define MaxSize 20 typedef int ElemType; typedef struct { ElemType data[MaxSize]; int top; }SqStack; void InitSqStack(SqStack& S) { S.top = -1; } bool Push(SqStack& S,ElemType e) { if (S.top == MaxSize - 1) return false; S.data[++S.top] = e; return true; } bool Pop(SqStack& S, ElemType &e) { if (S.top == -1) return false; e = S.data[S.top--]; return true; } bool GetTop(SqStack& S, ElemType &e) { if (S.top == -1) return false; e = S.data[S.top]; return true; } int main() { SqStack S; InitSqStack(S); ElemType e=6; Push(S, e); e = 7; Push(S, e); e = 2; GetTop(S, e); printf("%d", e); Pop(S, e); printf("%d", e); Pop(S, e); printf("%d", e); return 0; }
    Processed: 0.009, SQL: 9