栈
#include #include #include #include using namespace std; class iStack { public: iStack(int capacity) :_stack(capacity),_top(0) {} bool pop(int &value); bool push(int value); bool full(); bool empty(); void display(); int size(); private: int _top; vector }; inline int iStack::size() { return _top; } inline bool iStack::empty() { return _top ? false : true; } inline bool iStack::full() { return _top < _stack.size() - 1 ? false : true; } bool iStack::pop(int &top_value) { if (empty()) { return false; } top_value = _stack[--_top]; cout << "iStack::pop():" << top_value << endl; return true; } bool iStack::push(int value) { cout << "istack::push" << value << endl; if (full()) { return false; } _stack[_top++] = value; return true; } void iStack::display() { if (!size()) { cout << "( 0 )" << endl; } cout << "size:" << size() << endl; for (int ix = 0; ix < _top; ix++) { cout << _stack[ix] << " "; } cout << endl; } int main() { iStack stack(32); stack.display(); for (int ix = 0; ix < 51; ++ix) { if (ix % 2 == 0) { stack.push(ix); } if (ix % 5 == 0) { stack.display(); } if (ix % 10 == 0) { int dumpy; stack.pop(dumpy); stack.pop(dumpy); stack.display(); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |