一。//数组的方式
#pragma warning( disable : 4290 )
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
typedef string T;
class Stack
{
T a[5];
int cur;
public:
Stack() :cur(0){}
void push(const T& d)throw (const char*);//数据入栈
T pop()throw (const char *);//栈顶数据出栈
const T& top()throw (const char*);//取得栈顶的数据,也就是最后的数据
bool isemple() const//是否是空栈
{
return cur == 0;
}
bool isfull() const//是否已经满
{
return cur == 5;
}
void clear()//清空栈
{
cur = 0;
}
int size() const//栈里面元素的个数
{
return cur ;
}
void showlist()const;
};
void Stack::push(const T&d)throw (const char*)
{
if (Stack::isfull())throw "满";
a[cur++] = d;
}
T Stack::pop() throw (const char *)
{
if (Stack::isemple()) throw "空";
return a[--cur];
}
const T& Stack::top() throw (const char*)//取得栈顶的数据,也就是最后的数据
{
if (Stack::isemple()) throw "空";
return a[cur - 1];
}
void main()
{
Stack s;
try
{
s.push("hello,");
s.push("hang,");
s.push("zhou,");
}
catch (const char *e)
{
cout << "异常:" << e << endl;
}
while (!s.isemple())
{
cout << s.pop() << endl;
cout << s.size() << endl;
}
system("pause");
}