内联函数
#include <iostream>#include <stdlib.h>
using namespace std;
class stack
{
public :
enum{maxstack=5};
void init(){top=-1;}
void push(int n)
{
if(isfull())
{
errmsg("full stack can't been pushed.");
return ;
}
arr[++top]=n;
}
int pop(){
if(isempty())
{
errmsg("empty stack.popping dummy value.");
return dummy_val;
}
return arr[top--];
}
bool isempty(){return top<0;}
bool isfull(){return top>=maxstack-1;}
void dump(){
cout<<"stack contents ,top to bottom:\n";
for (int i=top;i>=0;i--)
cout<<'\t'<<arr[i]<<'\n';
}
private :
void errmsg(const char *msg)const
{
cerr<<"\n*** stack operation failure:"<<msg<<'\n';
}
int top ;
int arr[maxstack];
int dummy_val;
};
int main(int argc, char *argv[])
{
stack s1;
s1.init();
s1.push(9);
s1.push(4);
s1.dump();
cout<<"popping"<<s1.pop()<<'\n';
s1.push(8);
s1.dump();
s1.pop();
s1.dump();
s1.push(3);
s1.push(5);
s1.dump();
for(unsigned i=0;i<stack::maxstack;i++)
s1.push(1);
s1.dump();
return 0;
system("PAUSE");
return 0;
}
请指点下