首页 > 代码库 > IT公司100题-2
IT公司100题-2
1 // 定义栈的数据结构,要求添加一个min 函数,能够得到栈的最小元素。 2 // 要求函数min、push 以及pop 的时间复杂度都是O(1)。 3 #include <iostream> 4 #include "../data/own/c2_list.h" 5 using namespace std; 6 7 template <typename object> 8 class miniStack{ 9 public:10 bool isEmpty() const11 {return _list.empty();}12 13 const object& top() const14 {_list.front();}15 16 void push(const object& x)17 {18 _list.push_front(x);19 if(x < _min)20 _min = x;21 }22 23 void min(){cout << _min << endl;}24 25 object pop()26 {object x = _list.front(); _list.pop_front(); return x;}27 28 private:29 List<object> _list;30 object _min;31 };32 33 int main()34 {35 miniStack<int> mstack;36 mstack.push(10);37 mstack.push(28);38 mstack.push(7);39 mstack.push(12);40 mstack.push(23);41 mstack.push(32);42 mstack.push(11);43 mstack.push(9);44 mstack.min();45 46 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。