栈的pop,min,push

来源:互联网 发布:informix 重启数据库 编辑:程序博客网 时间:2024/05/16 11:28
class stack
{
public:
stack(int size=100)
{
    buff=new int[size];
cur=-1;
}


~stack()
{
delete[] buff;
}


void push(int val)
{
buff[++cur]=val;
}


void pop()
{
--cur;
}


int top()
{
return buff[cur];
}
bool empty()
{
return cur==-1;
}
private:
int* buff;
int cur;


}


class stackwithmin
{


public:
stackwithmin()
{
}
~stackwithmin()
{}
void push(int val)
{
s1.push(val);
if(val<=min())
s2.push(val);
}


void pop()
{
if(s1.top()==min())
s2.pop();
s1.pop();
}
int top()
{
return s1.top();
}
bool empty()
{
return s1.empty();
}
    int min()
{
if(s2.empty())
return INT_MAX;
else return s2.top();
}
private:
stack s1,s2;
}
0 0
原创粉丝点击