C语言编程实现顺序栈的初始化,入栈,出栈,取栈顶元素,显示 *** 作

C语言编程实现顺序栈的初始化,入栈,出栈,取栈顶元素,显示 *** 作,第1张

#define STACKSIZE 100

int mstack[STACKSIZE],top,bottom

void mInitStack() { top=bottom=0}

void mPush(int x) { if ( top-bottom<=STACKSIZE ) { mstack[top]=xtop++} }

int mPop() { int r=0if ( top>bottom ) { r=mstack[top]top--} return r}

void mShowStack() { int iprintf("[")for ( i=bottomi<topi++ ) printf("%d ",mstack[i])printf("]\n")}

void main()

{

    int i,n,x,loop=1,s

    char buffer[80]

    mInitStack()

    scanf("%d",&n)for ( i=0i<ni++ ) { scanf("%d",&x)mPush(x)}

    mShowStack()

    while ( loop )

    { buffer[1]=0gets(buffer)s=1

        switch ( buffer[1] )

        { case 'O':

            case 'o': x=mPop()break

            case 'U':

            case 'u': x=atoi(buffer+5)mPush(x)break

            case 'n':

            case 'N': loop=0break

            default: s=0break

        }

        mShowStack()

    }

    mShowStack()

}

#define STACK_SIZE 100

#define PUSH_POP_SUCCESS 1

#define PUSH_POP_ERROR 0

struct _stackbuf {

int _collection[STACK_SIZE]

int _top

}

typedef struct _stackbuf S_STACK

typedef unsigned int u_int_f

// 入栈

u_int_f push(S_STACK *stack, int d){

if (stack->_top >= STACK_SIZE) return PUSH_POP_ERROR

stack->_collection[stack->_top++] = d

return PUSH_POP_SUCCESS

}

// 出栈

u_int_f pop(S_STACK *stack, int *e){

if (!stack->_top) return PUSH_POP_ERROR

*e=stack->_collection[--(stack->_top)]

return PUSH_POP_SUCCESS

}

int main(){

S_STACK stack = { {0},0 }

push(&stack, 1)

push(&stack, 2)

push(&stack, 3)

int gv = 0

pop(&stack, &gv)

printf("%d\n", gv)

system("PAUSE")

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8131959.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存