进出栈

进出栈代码如下

#include<stdio.h>
#include<stdlib.h>

#define M 100

typedef struct {
    int data[M];
    int top;
}SqStack; 
//初始化 
int InitSqStack(SqStack *S)
{
    S->top=-1;
    return 1;
}
//压栈
int PushSqStack(SqStack *S,int e)
{
    if(S->top==M-1)
    {
        return 0;
    }
    S->top++;
    S->data[S->top]=e;
    return 1;
} 
//出栈
int PopSqStack(SqStack *S,int *e)
{
    if(S->top==-1)
    {
        return 0;
    }
    *e=S->data[S->top--];
    return 1;
}
int main()
{
    SqStack S;
    int e,j;
    InitSqStack(&S);
    //以下是测试
    PushSqStack(&S,4);
    PushSqStack(&S,8);
    PushSqStack(&S,10);
    PushSqStack(&S,78);
    PushSqStack(&S,49);
    for(j=0;j<M;j++)
    {
        if(PopSqStack(&S,&e)){
            printf("%d ",e);
        }
    }
    printf("\n");
    return 0;
} 

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值