栈的相关操作 C/C++

//InitStack(S);
//StackEmpty(S);
//Push(S,x);
//Pop(S,x);
//GetTop(S,x);
//ClearStack(S);
//栈顶指针:S.top=-1;栈顶元素:S.data[S.top]
//进栈操作:指针+1,再送值到栈顶元素
//出栈操作:先取栈顶元素值,再将栈顶指针减1
//栈空条件:S.top==-1,栈满条件:S.top=MaxSize-1,栈长:S.top+1
#include<iostream>
using namespace std;
#include"stdbool.h"
#define MaxSize 50 //栈的容量

typedef struct 
{
int data[MaxSize];
int top;
}SqStack;//栈的名字
void InitStack(SqStack *s){
    cout << "init Now s = " << s << endl;
    (*s).top=-1;
}

bool StackEmpty(SqStack s){
    if(s.top==-1){
        return true;
    }else
    {
        return false;
    }
}

bool Push(SqStack *s,int x){
    cout << "push Now s = " << s << endl;
    if((*s).top==MaxSize-1){
        return false;
    }else
    {
        (*s).data[++(*s).top]=x;
        return true;
    }
    
    
}

int Pop(SqStack *s,int &x){
    cout << "pop Now s = " << s << endl;
    if((*s).top==-1){
        return 0;
    }else
    {
        x=(*s).data[(*s).top--];
        return x;
    }
}

int GetTop(SqStack s,int x){
    if(s.top==-1){
        return -999;
    }else
    {
        x=s.data[s.top];
        return x;
    }
    
}

int Getlength(SqStack s){
    return s.top+1;
}
int main(){
    SqStack s;
     cout << "main Now s = " << &s << endl; //查看一下s的地址
    int x=4;
    InitStack(&s);  //传入s的地址
    Push(&s,x);
    int o=8;
    Push(&s,o);
    int m;
    cout << "Now pop = " << Pop(&s,m) << endl;
    int n;
    int p=GetTop(s,n);
     cout << "Now p = " << p << endl;
     cout << "Now empty = " << StackEmpty(s) << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值