栈,第1张

概述本文章向大家介绍栈,主要包括栈使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

#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 _stack;

};

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();

}

}

}

总结

以上是内存溢出为你收集整理的栈全部内容,希望文章能够帮你解决栈所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1264528.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存