您的位置:首页 > 其它

1)栈

2015-12-05 17:07 281 查看
#include<iostream>
#include<iomanip>
using namespace std;

enum error{overflow,underflow,success};
const int maxlen=100;

class  stack
{
public:
stack();//初始化
~stack();//析构
bool empty() const;//判断空
bool full() const;//判断满
int get_front(int &x)const;//取栈顶元素
error push(const int x);//入栈
error pop();//出栈
private:
int count;//统计栈中元素的个数
int data[maxlen];//存储栈中数据
};

/*
*初始化栈
*/
stack:: stack()
{
count=0;
}

/*
*判断为空
*/
bool stack::empty() const{
if(count==0)return true;
return false;
}

/*
*判断为满
*/
bool stack::full() const{
if(count==maxlen)return true;
return false;
}

/*
*取栈顶元素
*/
int stack::get_front(int &x)const{
if(empty())return underflow;
x=data[count-1];
return success;
}

/*
*入栈
*/
error stack::push(const int x){
if(full())return overflow;
data[count]=x;
count++;
return success;
}

/*
*出栈
*/
error stack::pop(){
if(empty())return underflow;
count--;
return success;
}
stack::~stack()
{
while(!empty())pop();
}

/*
*十进制数转化为八进制数
*/
int xchg(int n,stack s){
cout<<"十进制:["<<n<<"]->8进制:";
int mod,x;
while(n!=0){
mod = n % 8;
s.push(mod);
n/=8;
}
while(s.empty()!=true){
s.get_front(x);
cout<<x;
s.pop();
}
cout<<endl;
return 0;
}
int main()
{
stack s;
xchg(100,s);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: