顺序栈c++实现

栈是计算机里边极其常见的一类数据结构,特别是在编译原理里边,栈溢出(stack overflow)甚至成为世界知名的计算机人员交流网站,顺序栈也是十分常用的。


#include <iostream>

#include <cstdio>
#include <cstring>
#include <cstdlib>


using namespace std;


//栈最多100个元素
#define MAX 100
//顺序栈的数据类型
typedef int StackType;


class Stack
{
private:
    StackType data[MAX];
    int top;//指向头部下表的下一个
public:
    Stack();
    bool Empty();
    bool Full();
    bool GetTop(StackType &e);
    bool Pop(StackType &e);
    bool Push(StackType e);
};


Stack::Stack()
{
    this->top = 0;
}
bool Stack::Empty()
{
    if(this->top == 0)
        return true;
    return false;
}
bool Stack::Full()
{
    if(this->top == MAX)
        return true;
    return false;
}
bool Stack::GetTop(StackType &e)
{
    if(Empty())
        return false;
    e = this->data[this->top-1];
    return true;
}
bool Stack::Pop(StackType &e)
{
    if(Empty())
        return false;
    e = this->data[--this->top];
    return true;
}
bool Stack::Push(StackType e)
{
    if(Full())
        return false;
    this->data[this->top++] = e;
    return true;
}


int main()
{
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值