001设计一个有GetMin功能的栈
来源:互联网 发布:linux mint 18和ubuntu 编辑:程序博客网 时间:2024/06/05 16:02
题目要求:实现一个特殊的栈,在实现栈的基础上,在返回栈中最小的元素的操作
#include<iostream>
#include<stack>
using namespace std;
class Mystack
{
public:
Mystack(){ }
void push(int newnum){
if (stackMin.empty()){
this->stackMin.push(newnum);
}
else if (newnum <= this->getmin())
{ stackMin.push(newnum);}
this->stackData.push(newnum);
}
int pop(){
if (this->stackData.empty()){
cout << "栈已经空了" << endl;
}
int value = stackData.top();
if (value==stackMin.top()){
stackMin.pop();
}
this->stackData.pop();
return value;
}
public:
int getmin(){
if (stackMin.empty()){
cout << "栈已经为空了" << endl;
}
else{
return stackMin.top();}
}
public:
stack<int>stackData;
stack<int>stackMin;
};
void main(){
Mystack aa;
aa.push(10);
aa.push(40);
cout<<aa.getmin()<<endl;
stack<int> a;
for (int i = 6; i < 10; i++)
{
a.push(i);
}
cout << a.top() << endl;
cout << a.size();
system("pause");
}
来源:程序员面试代码指南 -左程云
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有 getMin功能的栈
- 设计一个有getMin功能的栈
- 设计一个有getMin功能的栈
- 001设计一个有GetMin功能的栈
- 栈——设计一个有getmin功能的栈
- 栈和队列 设计一个有getMin()功能的栈
- 栈和队列---设计一个有getMin功能的栈
- 了解Ajax基础
- Ubuntu14.04下安装 Python图表绘制库
- Invalid Call – yii\base\InvalidCallException The cookie collection is read only.
- magento 产品页随机调用同子分类产品
- 十个程序员里面九个闷葫芦???
- 001设计一个有GetMin功能的栈
- FZU1002-HangOver
- 面向对象分析的三个模型与5个层次
- applicationContext-common.xml 之spring tx:advice 和 aop:config 配置事务属性详解
- Sqlserver数据库批量导入数据
- CCF201612-2工资计算
- 《javascript设计模式与开发实践》阅读笔记(五)——高阶函数
- C#面试题
- 《μCOS-III源码分析笔记》学习笔记|2|第三章时间管理