栈是计算机里边极其常见的一类数据结构,特别是在编译原理里边,栈溢出(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;
}