#include <iostream>
using namespace std;
typedef int T;
class Stack{
T data[8];
int sz;
public:
Stack():sz(){}
void push(const T& d){
if(full())throw "full";
data[sz++] = d;
}
void pop(){
if(empty()) throw "empty";
sz--;
}
T top(){
if(empty())throw "empty";
return data[sz-1];
}
bool empty(){return 0==sz;}
bool full(){return 8==sz;}
int size(){return sz;}
};
int main()
{
Stack s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
cout << s.size() << endl;
cout << s.empty() << endl;
cout << s.full() << endl;
while(!s.empty()){
int x = s.top();
cout << x << ' ';
s.pop();
}
cout << endl;
cout << s.size() << endl;
cout << s.empty() << endl;
cout << s.full() << endl;
}
C++栈结构
最新推荐文章于 2024-09-19 13:36:34 发布