#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;
}
stack顺序C++
最新推荐文章于 2024-07-11 17:28:54 发布