参考胡学钢,合肥工业大学数据结构教材
//队列代码
#include<iostream>
#define MaxLen 50
using namespace std;
class queue
{
public:
queue();
bool empty()const;
bool full()const;
int get_front()const;
int append(const int x);
void serve();
private:
int count;
int front,rear;
int data[MaxLen];
};
queue::queue()
{
count=0;
front=rear=0;
}
bool queue::empty()const
{
return count==0;
}
bool queue::full()const
{
return count==MaxLen-1;
}
int queue::get_front()const
{
if(empty())
cout<<“Underflow…\n”;
return data[(front+1)%MaxLen];
}
int queue::append(const int x)
{
if(full())
cout<<“Overflow…\n”;
rear=(rear+1)%MaxLen;
data[rear]=x;
count++;
}
void queue::serve()
{
if(empty())
cout<<“Underflow…\n”;
front=(front+1)%MaxLen;
count–;
}
//测试代码为 打印杨辉三角
int main()
{
cout<<“三角形行数:”;
int n;cin>>n;
int s1,s2;
queue q;
cout<<1<<endl;
q.append(1);
for(int i=2;i<=n;i++)
{
s1=0;
for(int j=1;j<=i-1;j++)
{
s2=q.get_front();
q.serve();
cout<<s1+s2<<” “;
q.append(s1+s2);
s1=s2;
}
cout<<1<<endl;
q.append(1);
}
}