#include<iostream>
using namespace std;
typedef int QElemType;
typedef struct node {
QElemType data;
struct node*link;
}LinkNode,*LinkList;
typedef struct {
LinkList front, rear;
}LinkQueue;
void QueueInit(LinkQueue&Q)
{
Q.front = Q.rear = NULL;
}
int EnQueue(LinkQueue&Q, QElemType val)
{
LinkList NewNode=new LinkNode;
NewNode->data = val;
if (Q.front == NULL)
Q.front = Q.rear = NewNode;
else {
Q.rear->link = NewNode;
Q.rear = NewNode;
}
return 1;
}
int DeQueue(LinkQueue&Q, QElemType&x)
{
if (Q.front == NULL)return 0;
else if (Q.rear == Q.front)
{
x = Q.front->data;
Q.front = Q.rear = NULL;
}
else
{
x = Q.front->data;
Q.front = Q.front->link;
}
return 1;
}
void PrintYHA(int num)
{
LinkQueue Q;
QueueInit(Q);
EnQueue(Q, 1);
EnQueue(Q, 1);
QElemType m = 0, n = 0;
for (int i = 0;i < num;i++)
{
EnQueue(Q, 0);
for (int j = 0;j <= i + 2;j++)
{
DeQueue(Q, n);
EnQueue(Q, m + n);
m = n;
if (j != i + 2)cout << n << ' ';
}
cout << endl;
}
}
int main()
{
PrintYHA(5);
getchar();
}
C++队列实现杨辉三角
最新推荐文章于 2023-02-18 11:21:41 发布