面试题-Stack的最小值o(1)

 // Stack.cpp : 定义控制台应用程序的入口点。
 //

 #include "stdafx.h"
 #include <iostream>
 using namespace std;

 template <class Type>
 struct Stack
 {
 private:
 Type *_stack;
 int _top;
 int _min;
 int _size;

 public:
 Stack(),_min()
 {
 _stack = new Type[size];//(Type*)malloc(sizeof())
 }
 void push(const Type &value)
 {

 )
 {
 _min = value;
 }
 else if(_top == _size)
 {
 cout<<"Push failed,the stack is full"<<endl;
 return;
 }
 _stack[_top++] = value-_min;
 if(value < _min)
 {
 _min = value;
 }

 }
 Type pop()
 {
 )
 {
 cout<<"Pop failed,the stack is emply."<<endl;
 ;
 }
 Type popvalue;
 )
 {
 popvalue = _min;
 _min = _min-_stack[_top];
 }
 else
 {
 popvalue = _min + _stack[_top];
 }
 return popvalue;
 }
 Type min()
 {
 return _min;
 }

 };

 int _tmain(int argc, _TCHAR* argv[])
 {
 Stack<);
 sk.push();
 sk.push();
 sk.push();
 sk.push();
 sk.push();
 sk.push();
 sk.push();

 cout<<sk.min()<<endl;
 cout<<sk.pop()<<endl;
 cout<<sk.min()<<endl;
 cout<<sk.pop()<<endl;
 cout<<sk.min()<<endl;
 cout<<sk.pop()<<endl;
 cout<<sk.min()<<endl;
 cout<<sk.pop()<<endl;
 cout<<sk.min()<<endl;
 cout<<sk.pop()<<endl;
 cout<<sk.min()<<endl;
 cout<<sk.pop()<<endl;
 cout<<sk.min()<<endl;
 cout<<sk.pop()<<endl;

 ;
 }
上一篇:JSP动作--JSP有三种凝视方式


下一篇:MySQL参数