您的位置:首页 > 其它

2)杨辉三角[1]队列实现

2015-11-13 21:56 429 查看
#include<iostream>
#include<iomanip>
using namespace std;

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

class queue{
public:
queue();
bool empty()const;
bool full()const;
int get_front(int &x)const;
error append(const int x);
error serve();
private:
int count;
int rear,front;
int data[maxlen];
};
queue::queue(){
count=0;
rear=front=0;
}

bool queue::empty()const{
if(count==0)return true;
return false;
}

bool queue::full()const{
if(count==maxlen)return true;
return false;
}

int queue::get_front(int &x)const{
if(empty())return underflow;
x=data[(front+1)%maxlen];
return success;
}
error queue::append(const int x){
if(full())return overflow;
rear=(rear+1)%maxlen;
data[rear]=x;
count++;
return success;
}

error queue::serve(){
if(empty())return underflow;
front=(front+1)%maxlen;
count--;
return success;
}

int main(){
queue q;
int n;
cin>>n;
int s1,s2;
for(int i=1;i<n;i++)cout<<"  ";
cout<<1<<endl;
q.append(1);
for(int i=2;i<=n;i++){
s1=0;
for(int k=1;k<=n-i;k++ )cout<<"  ";
for(int j=1;j<=i-1;j++){
q.get_front(s2);
q.serve();
cout<<s1+s2<<setw(4);
q.append(s1+s2);
s1=s2;
}
cout<<1<<endl;
q.append(1);
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: