#include <stdio.h> #include <stdlib.h> #define N 500 typedef struct node { int data[N]; int front; int rear; }Queue; void initQueue(Queue*& q) //初始队列 { q= (Queue*)malloc(sizeof(Queue)); q->front = 0; q->rear = 0; } void insertQueue(Queue* q,int n)//插入元素 { if((q->rear+1)%N==q->front) { printf("队列满了!插入错误/n"); exit(0); } q->rear = (q->rear+1)%N; q->data[q->rear] = n; } int delQueue(Queue* q)//删除元素 { if(q->rear==q->front) { printf("队列为空!删除错误/n"); exit(0); } q->front = (q->front+1)%N; return q->data[q->front]; } void yanghui(Queue* q,int n)//杨辉三角打印 { insertQueue(q,1); insertQueue(q,1); int i; int j; int s = 0; int temp; i = 0; for(;i<n;i++) { j = (q->front+1)%N; for(;j<=q->rear;j++) { printf(" %d ",q->data[j]); } printf("/n"); insertQueue(q,0); j = 0; for(;j<i+3;j++) { temp = delQueue(q); insertQueue(q,s+temp); s = temp; } } } int main() { Queue *q; int n; printf("杨辉三角的行数:"); scanf("%d",&n); initQueue(q); yanghui(q,n); free(q); return 0; }