#include<iostream>
using namespace std;
struct stack
{
int* _pElem; //指向元素数据的指针
int _capacity;
int _top;
stack( int n )
:_capacity( n)
{
_top = 0;
_pElem = new int [_capacity];
}
~stack()
{
delete []_pElem;
_pElem = NULL;
}
};
class Stack
{
private:
stack s;
stack minstack;
public:
Stack(int n)
:s( n)
, minstack( n)
{}
bool pop()
{
if (s._top == 0)
return false ;
size_t value = s._pElem[--s._top];
//判断将要弹出的是否是最小元素 如果是 则更新最小元素栈
if (value == minstack._pElem[minstack._top-1])
--minstack._top;
return true ;
}
bool push( int value )
{
if (s._top == s._capacity)
return false ;
if (minstack._top==0 || value <=minstack._pElem[minstack._top-1])
minstack._pElem[minstack._top++] = value;
s._pElem[s._top++] = value;
return true ;
}
int min()
{
if (minstack._top == 0)
return NULL ;
int pmin = minstack._pElem[minstack._top-1];
return pmin;
}
};
转载于:https://blog.51cto.com/green906/1759022