#include <iostream>
using namespace std;
class stack{
public:
stack(unsigned int n = 0):m_start(new int[n]),m_current(-1),m_capacity(n){}
~stack(){delete[] m_start;}
void push(int e);
void pop();
int top();
bool isEmpty();
bool isFull();
private:
int* m_start;
int m_current;
int m_capacity;
};
void stack::push(int e){
if(m_current + 1 < m_capacity){
m_start[++m_current] = e;
}else{
cout << "stack is full.\n";
}
}
void stack::pop(){
if(m_current >= 0){
--m_current;
}else{
cout << "stack is empty.\n";
}
}
int stack::top(){
if(m_current >= 0){
return m_start[m_current];
}else{
cout << "stack is empty.\n";
exit(1);
}
}
bool stack::isEmpty(){
return m_current == -1;
}
bool stack::isFull(){
return m_current + 1 == m_capacity;
}
int main(){
stack s(10);
for (int i = 0; i < 10; ++i){
s.push(i);
}
cout << "stack is full : " << s.isFull() << endl;
s.push(10);
while(!s.isEmpty()){
cout << s.top() << ends;
s.pop();
}
cout << endl << "stack is empty : " << s.isEmpty() << endl;
s.pop();
return 0;
}
![这里写图片描述](https://img-blog.csdn.net/20170713125615698?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb2xpeGkxOTkzMTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)