http://blog.csdn.net/whuqin/article/details/6005806
#include <iostream>
using namespace std;
class Stack
{
private:
int *stack;//保存数据
int minNum;//保存栈中最小数
int top;//栈顶指针
int size;//栈大小
public:
Stack(int);
~Stack(void);
int min();
void push(int);
int pop();
};
Stack::Stack(int size)
{
this->size=size;
stack=new int[size];
top=-1;
minNum=0;
}
void Stack::push(int num)
{
if(top==-1)
minNum=num;
else
minNum=minNum>num?num:minNum;
if(top==size-1)
stack[top]=num;
else
stack[++top]=num;
}
int Stack::pop()
{
if(top==-1)
return 0;
return stack[top];
}
int Stack::min()
{
return minNum;
}
Stack::~Stack()
{
if(size==0)
return;
delete stack;
cout<<"析构函数得到调用"<<endl;
}
void main()
{
Stack stack(10);
for(int i=0;i<10;i++)
stack.push(10-i);
cout<<"栈中最小数为:"<<stack.min()<<endl;
cout<<"栈中顶部数为:"<<stack.pop()<<endl;
cin.get();
}