//用循环链表实现杨辉三角
#include<iostream>
#define Length 10
#define ERROR 0
#define TRUE 1
using namespace std;
class XU_DL
{
private:
int *item;
int front;
int rear;
int maxlength;
public:
XU_DL(int length=Length) //对队列的初始化
{
if(length<Length)length=Length;
item=new int [length];
maxlength=length;
front=rear=0;
}
~XU_DL() //析构函数销毁队列
{
delete [] item;
}
void ClearXU_DL() //清空队列
{
front=rear=0;
}
int Getlength() //获得长度
{
if(rear==front) return ERROR;
return ((rear-front)+maxlength)%maxlength;
}
int Inser(int e) //插入数据e
{
if((rear+1)%maxlength==front)return ERROR; //队满时的判断语句
rear=(rear+1)%maxlength;
item[rear]=e;
return TRUE;
}
int Gethand(int e) //获得队头元素
{
if(rear==front)return ERROR; //队列为空时的判断语句
e=item[(front+1)%maxlength];
return e;
}
int Deletedate(int e) //获取数据并删除该数
{
if(rear==front) return ERROR; //队列为空
front=(front+1)%maxlength;
e=item[front];
return e;
}
void Output()
{
if(front==rear){cout<<“The queue is null!”<<endl;return;}
cout<<“The queue length is :”<<Getlength()<<endl;
cout<<“The queue’s content is :”;
for(int i=1;i<Getlength();i++)
cout<<item[i]<<” “;
cout<<endl;
}
int Gethead()
{
if(front==rear){cout<<“The queue is null!”<<endl;return ERROR;}
return item[(front+1)%maxlength];
}
};
void YHSJ()
{
int n,t;
bool frag(true);
while(frag)
{
cout<<“请输入要输出杨辉三角的行数为:”;
cin>>n;n++; //一定要n++,否则的话只能改变后面的条件了
XU_DL a(1000);
a.Inser(1);
for(int i=2;i<=n;i++)
{
a.Inser(1);
for(int j=1;j<i-1;j++)
{
t=a.Deletedate(t);
cout<<t<<” “;
t=t+a.Gethead();
a.Inser(t);
}
t=a.Deletedate(t);
cout<<t<<” “;
a.Inser(1);
cout<<endl;
}
cout<<“是否还要继续输入?是请输入yes,否请输入no:”;
char s[5];
cin>>s;
if(!strcmp(s,”no”)) frag=false;
}
}
int main()
{
YHSJ();
return 0;
}