//程序编译运行通过,基本结构写好了。
//travel,size,expand等函数可以慢慢扩展
#include <iostream>
using namespace std;
typedef int T;
class Stack
{
private:
T data[5];
int sz; //数组个数
public:
Stack():sz() //只需将sz初始化为0就行了,data不用
{
}
bool isFull() const
{
return 5==sz;
}
bool isEmpty() const
{
return 0==sz;
}
void push(const T& d)
{
if(isFull())
{
throw "满" ;
}
data[sz++] = d;
}
void pop()
{
if(isEmpty())
{
throw "空";
}
sz--;
}
T top() const
{
if(isEmpty())
{
throw "空";
}
return data[sz-1];
}
};
int main()
{
Stack s;
s.push(11);
s.push(22);
s.push(33);
s.push(44);
s.push(55);
cout << s.top() << endl;
s.pop();
cout << s.top() << endl;
//如果这里有个遍历函数,会比较方便,到处可以用。
while(!s.isEmpty())
{
cout << s.top() << ' ';
s.pop();
}
cout << endl;
}
简单的数组栈
最新推荐文章于 2021-02-03 15:53:34 发布