#include <iostream>
using namespace std;
typedef int T;
class Stack{
T a[10];
int num; //记录已经放入的元素个数
public:
void push(const T& t)
{
if (full())
throw "stack overflow";
a[num++] = t;
}
void pop()
{
if (empty())
throw "empty stack";
num--;
}
T top()
{
if (empty())
throw "no top stack";
return a[num-1];
}
bool empty()
{
return num == 0;
}
bool full()
{
return num == 10;
}
int size()
{
return num;
}
int capacity()
{
return 10;
}
void clear()
{
num = 0;
}
Stack():num(0)
{
}
};
int main()
{
Stack s;
s.push(1);
s.push(2);
s.push(3);
/*
for (int i=0; i<10; i++)
{
s.push(i*5);
}
*/
while(!s.empty())
{
cout << s.top() << ' ';
s.pop();
}
cout << endl;
/* for (int i=0; i<10; i++)
s.push(i);
cout << "full?" << s.full() << endl;
s.push(100); //there is an exception
cout << "cant see this" << endl;
*/
}
栈 数组
最新推荐文章于 2022-01-13 20:46:39 发布