栈的基本操作

    xiaoxiao2021-03-25  66

    #include <stdio.h> #include <stdlib.h> #define STACK_INIT_SIZE1 3 #define STACKINCREMENT 3 typedef struct{ int *base; int *top; int stacksize; }SqStack; SqStack * InitStack(SqStack *S){ S->base = (int *)malloc((STACK_INIT_SIZE1)*sizeof(int)); S->top = S->base; S->stacksize = 3; return S; } int GetTop(SqStack *S,int *e){ if(S->base==S->top)return 1; e = (S->top-1); return 0; } int  Push(SqStack *S,int e){ if(S->top-S->base >= S->stacksize){ S->base = (int *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(int)); S->top = S->base + S->stacksize; S->stacksize += STACKINCREMENT; printf("size bigger\n"); } *(S->top++) = e; return 0; } int  Pop(SqStack *S,int *e){ if(S->base == S->top)return 1; e = (--S->top); return *e; } int main(){ SqStack *S; S = InitStack(S); printf("%d\n",S->stacksize); int result; result = Push(S,1); printf("%d\n",*(S->top-1)); result = Push(S,2); printf("%d\n",*(S->top-1)); int *e; result = GetTop(S,e); printf("%d\n", result); result = Push(S,3); result = Push(S,4); printf("%d\n",*(S->top-1)); result = Pop(S,e); printf("%d\n",*(S->top-1)); printf("%d:%p\n", result,e); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-39662.html

    最新回复(0)