基于队列实现杨辉三角

1.函数

1.1创建队列函数

int InitQueue(Cqueue *Q)
{
    Q->base = (ElemType *)malloc(sizeof(ElemType)*MAXSIZE);
    Q->length = 0;//当前队列长度
    Q->fornt = 0;//头指针
    Q->rear = 0;//尾指针
    return OK;
}

1.2入队函数

int Enqueue(Cqueue *Q,ElemType en_elem)
{
    if(Q->length == MAXSIZE)
    {
        printf("full\n");//队列已满
        return NO;
    }
    Q->base[Q->rear] = en_elem;
    Q->rear = (Q->rear+1)%MAXSIZE;
    Q->length++;
    return OK;
}

1.3出队函数

int Outqueue(Cqueue *Q,Elemtype *out_elem)
{
    if (Q->length == 0)
    {
        printf("Empety\n");
        return NO;
    }
    *out_elem = Q->base[Q->fornt];
    Q->fornt = (Q->fornt+1)%MAXSIZE;
    Q->length--;
    return OK;
}

1.4输出当前队列

int PrintQueue(Cqueue *Q)
{
    if(Q->length == 0)
    {
        return NO;
    }
    int p = Q->fornt;
    for(int i = 0; i < Q->length ; i++)
    {
        printf("%d ",Q->base[p]);
        p++;
    }
    printf("\n");
    return OK;
}

1.5获取队头元素的值

int GetHeadElem(Cqueue *Q,ElemType *head_elem)
{
    if (Q->length == 0)
    {
        printf("Empety\n");
        return NO;
    }
    *head_elem = Q->base[Q->fornt];
    return OK;
}

算法思想:

1.首先将第一行元素1入队,然后将第二行的1入队

001e7a1fbc7b4cadb330940de786e3f0.png

 2.第一行元素出队此时指针指向第二行首元素

0540331208364424bf52aeb7aea598bc.png

 3.第三行的元素1入队

0a13f1a88cf047e99e2781c427e33d5b.png

 4.队头元素出队并且把值保存在A中,并且获取此时队头元素,并且保存在B中

21c9e0ea23e24545801547b0e81f9941.png

 5.将A+B的值入队

fbd6b22db30c4ae998a551229cd8a30f.png

6.循环执行第四第五步

7.将每行最后的固定的元素1入队

516b1b16bc4d4334aa83da5a95c9d3b8.png

 8.输出当前队列

完整代码如下:

#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define NO 0
#define MAXSIZE 100
typedef int ElemType;
typedef struct
{
    ElemType *base;
    int rear;
    int fornt;
    int length;
}Cqueue;
int InitQueue(Cqueue *Q)
{
    Q->base = (ElemType *)malloc(sizeof(ElemType)*MAXSIZE);
    Q->length = 0;//当前队列长度
    Q->fornt = 0;//头指针
    Q->rear = 0;//尾指针
    return OK;
}
int Enqueue(Cqueue *Q,ElemType en_elem)
{
    if(Q->length == MAXSIZE)
    {
        printf("full\n");//队列已满
        return NO;
    }
    Q->base[Q->rear] = en_elem;
    Q->rear = (Q->rear+1)%MAXSIZE;
    Q->length++;
    return OK;
}
int Outqueue(Cqueue *Q,ElemType *out_elem)
{
    if (Q->length == 0)
    {
        return NO;
    }
    *out_elem = Q->base[Q->fornt];
    Q->fornt = (Q->fornt+1)%MAXSIZE;
    Q->length--;
    return OK;
}
int PrintQueue(Cqueue *Q)
{
    if(Q->length == 0)
    {
        return NO;
    }
    int p = Q->fornt;
    for(int i = 0; i < Q->length ; i++)
    {
        printf("%d ",Q->base[p]);
        p++;
    }
    printf("\n");
    return OK;
}
int GetHeadElem(Cqueue *Q,ElemType *head_elem)
{
    if (Q->length == 0)
    {
        return NO;
    }
    *head_elem = Q->base[Q->fornt];
    return OK;
}
int main()
{
    Cqueue Q;
    ElemType A,B;
    int n,i,j;
    printf("请输入杨辉三角的行数\n");
    scanf("%d",&n);
    InitQueue(&Q);
    Enqueue(&Q,1);//入队
    printf("1\n");//输出第一行
    for(i = 0;i < n-1 ; i++)
    {
        for(j = 0 ; j < i ;j++)
        {
            Outqueue(&Q,&A);//队头元素出队,并且其值保存在A中
            GetHeadElem(&Q,&B);//当前队头元素的值保存在B中
            
            Enqueue(&Q,A+B);//A+B入队
        }
        Enqueue(&Q,1);//每行的最后一个元素入队
        PrintQueue(&Q);//输出队列(也就是最后一行元素)
    }
    return 0;
}

 

 

在C语言中,我们可以利用队列数据结构实现杨辉三角的打印。杨辉三角是一个二维数组,每一行的第一个和最后一个数字都是1,其他位置的数字是它上面两行对应位置数字之和。为了表示这种动态生成的结构,队列在这里可以用来存储当前行的数据,并在需要的时候将其打印。 首先,我们需要创建一个队列来存储行数据,然后初始化队列为空。接下来,我们遍历每一行: 1. 对于第0行和第1行,直接放入队列,它们始终是[1]。 2. 对于大于1的行,将前一行的每个元素(除了两端的1)加入到新行的列表中,然后把新的行添加到队列。 3. 当队列的长度达到当前行数时,我们就有了完整的杨辉三角一行。打印这个队列,然后更新队列以开始下一行的计算。 下面是一个简单的示例代码片段: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct { int* data; int size; } Queue; // 队列操作函数... void init_queue(Queue* q); void enqueue(Queue* q, int val); bool is_empty(Queue* q); int dequeue(Queue* q); Queue queue = {NULL, 0}; // ... 其他队列操作函数 void print_pascal_triangle(int rows) { for (int i = 0; i <= rows; i++) { // 初始化队列 init_queue(&queue); // 将第i行的值放入队列 for (int j = 0; j <= i; j++) { if (j == 0 || j == i) enqueue(&queue, 1); // 两边都是1 else enqueue(&queue, dequeue(&queue) + dequeue(&queue)); // 其他位置的值等于上面两个 } // 打印队列的内容 while (!is_empty(&queue)) { printf("%d ", dequeue(&queue)); } printf("\n"); } } int main() { int rows = 5; print_pascal_triangle(rows); return 0; } ``` 注意,上述代码中省略了队列的具体实现细节,如初始化、入队、出队等函数,你需要自行编写这些辅助函数。运行这个程序将会打印出前五行的杨辉三角
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cles8it

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值