利用循环队列打印杨辉三角(c语言实现)

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

#define MAXQSIZE 200
typedef int QElemType;
typedef struct {
    QElemType  *base;
    int front;
    int rear;
}SqQueue;
void InitQueue(SqQueue *Q){
    //构造一个空队列Q
    Q->base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType));
    if(!Q->base)exit(1);
    Q->front=Q->rear=0;
}
int QueueLength(SqQueue *Q){
    //返回Q的元素个数,即队列的长度
    int e;
    e=(Q->rear-Q->front+MAXQSIZE)%MAXQSIZE;
    return e;
}
void EnQueue(SqQueue *Q,QElemType e){
    //插入的元素e为Q的新的队尾元素
    if((Q->rear+1)%MAXQSIZE ==Q->front) exit(1);
    Q->base[Q->rear]=e;
    Q->rear=(Q->rear+1)%MAXQSIZE;
}
void DeQueue(SqQueue *Q){
    //若队列不空,则删除Q的队头元素,用e返回其值
    if(Q->front==Q->rear)
        exit(1);
    //e=Q.base[Q.front];
    Q->front=(Q->front+1)%MAXQSIZE;
}
QElemType GetHead(SqQueue *Q){
    //返回队头元素
    return Q->base[Q->front];
}
int main(){
    int N,n,c;
    QElemType t,x;
    SqQueue f,*Q;
    Q=&f;
    InitQueue(Q);
    printf("请输入杨辉三角规模:\n");
    scanf("%d",&N);
    EnQueue(Q,1);
    for(n=2;n<=N;n++){
        EnQueue(Q,1);
        for(c=1;c<=n-2;c++){
            t=GetHead(Q);
            DeQueue(Q);
            printf("%4d",t);
            x=GetHead(Q);
            t=t+x;
            EnQueue(Q,t);   
        }
        EnQueue(Q,1);
        printf("%4d",GetHead(Q));
        DeQueue(Q);
        printf("\n");
    }
    while(Q->front!=Q->rear){
            printf("%4d",GetHead(Q));
            DeQueue(Q);
        }
    return 0;
    }

《利用循环队列打印杨辉三角(c语言实现)》

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/weixin_43009363/article/details/82184726
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞