顺序栈C++(考研数据结构)

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
/**定义一个顺序栈*/
typedef struct {
int data[MAXSIZE];
int top;
}SqStack;

/**初始化栈*/
void initStack(SqStack &st){
st.top=-1;
printf("初始化成功");
}
/**判断栈是否为空*/
int isEmpty(SqStack st){
if(st.top==-1){
    return 1;
}
else{
    return 0;
}
}
/**判断栈是否满*/
int isFull(SqStack st){
if(st.top==MAXSIZE-1){
    return 1;
}
else{
    return 0;
}
}

/**入栈操作*/
void push(SqStack &st,int x){
    //非满状态
if(isFull(st)==0){
    //入栈
    st.data[++st.top]=x;
}else{
printf("栈满,请勿插入!");
}
}
/**出栈操作*/
void pop(SqStack &st,int &x){
    //栈空
if(isEmpty(st)==0){
    x=st.data[st.top--];
    printf("%d ",x);
}else{
    printf("栈空,请勿移除元素!");
}
}
/**打印栈内元素*/
void printStack(SqStack st){
    for(int i=0;i<=st.top;i++){
        printf("%d ",st.data[i]);
    }

}
/**
主函数
*/
int main(){
SqStack st;
initStack(st);
push(st,6);
push(st,2);
push(st,5);
printf("\n");
printf("初始化栈内元素为:");
printStack(st);
printf("\n");
printf("出栈元素是:");
int x=0;
pop(st,x);
pop(st,x);
printf("\n");
printf("剩余元素:");
printStack(st);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

William_Tao(攻城狮)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值