队列练习——杨辉三角

简介: 队列练习——杨辉三角

杨辉三角

  • 杨辉三角,是二项式系数在三角形中的一种几何排列。

C++代码实现

/*
队列————杨辉三角
*/
#include<iostream>
#include<stdlib.h>
using namespace std;

#define OK 1
#define ERROR -1
#define OVERFLOW -2

typedef int Status;
typedef int QElemType;

#define MAXSIZE 100

typedef struct Qnode {
    QElemType data;
    struct Qnode* next;
}Qnode, * QueuePtr;

typedef struct {
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;

Status InitLQueue(LinkQueue& Q) {
    Q.front = new Qnode;
    if (!Q.front) exit(OVERFLOW);
    Q.rear = Q.front;
    Q.front->next = NULL;
    return OK;
}

// 判断链队列是否为空
bool LQueueEmpty(LinkQueue Q) {
    return (Q.front == Q.rear);
}

// 入队
Status PushLQueue(LinkQueue& Q, QElemType e) {
    QueuePtr q;
    q = new Qnode;
    if (!q)  exit(OVERFLOW);
    q->data = e;
    q->next = NULL;
    Q.rear->next = q;
    Q.rear = q;
    return OK;
}

// 出队
Status PopLQueue(LinkQueue& Q, QElemType& e) {
    QueuePtr q;
    if (LQueueEmpty(Q)) return ERROR;
    q = Q.front->next;
    e = q->data;
    Q.front->next = q->next;
    /*
    if (Q.rear == q) Q.rear = Q.front;
    */
    if (Q.rear == q) Q.rear = Q.front;
    delete q;
    return OK;
}

// 销毁链队列
Status DestroyQueue(LinkQueue& Q) {
    while (Q.front) {
        Q.rear = Q.front->next;
        delete Q.front;
        Q.front = Q.rear;
    }
    return OK;
}

// 获取队头元素
int GetHead(LinkQueue Q) {
    QElemType e;
    if (LQueueEmpty(Q)) return 0;
    e = Q.front->next->data;
    return e;
}

// 创建链队列
void CreateLQueue(LinkQueue& Q, int m) {
    QElemType e;
    for (int i = 1; i <= m; i++) {
        cout << "请输入第" << i << "个元素: ";
        cin >> e;
        PushLQueue(Q, e);
    }
}

// 输出链队列
void OutPut(LinkQueue Q) {
    QueuePtr q;
    q = new Qnode;
    q = Q.front->next;
    while (q) {
        cout << q->data << " ";
        q = q->next;
    }
    cout << endl;
}

void f() {
    cout << "请输入杨辉三角的阶数: ";
    int num;
    cin >> num;
    if (num == 1)  // 行数为1
        cout << 1 << endl;
    else {
        cout << '1' << endl;
        cout << "1 1" << endl;
        LinkQueue q1;  // 存储第i层数据
        InitLQueue(q1);
        QElemType e, q;
        // 第二行两个1
        for (int i = 0; i < 2; i++)
            PushLQueue(q1, 1);
        for (int i = 0; i < num - 2; i++) {
            LinkQueue q2;  // 存储第 i + 1 层
            InitLQueue(q2);
            PushLQueue(q2, 1);  // 第一个数是1
            while (!LQueueEmpty(q1)) {
                PopLQueue(q1, q);
                if (LQueueEmpty(q1)) PushLQueue(q2, 1);
                else PushLQueue(q2, q + GetHead(q1));
            }
            q1 = q2;
            OutPut(q1);
        }
    }
}

int main()
{
    f();
    return 0;
}
请输入杨辉三角的阶数: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
目录
相关文章
|
SQL 移动开发 监控
Sentinel控制台规则配置
Sentinel控制台规则配置
Sentinel控制台规则配置
|
前端开发 程序员
循环队列的使用·数据结构
在讲循环队列之前,先来聊聊队列吧 队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。 而循环队列是什么呢?由于队列存在着假溢出(即队列未满,但是尾指针到了末尾的位置,使队列无法再进行入队的操作)。为了解决假溢出,程序员发明了循环队列,使头尾指针的使用更加的灵活,可以有效地解决假溢出的问题。
213 0
循环队列的使用·数据结构
|
存储 弹性计算 分布式计算
|
存储 机器学习/深度学习 JSON
web前端技能方法总结(css、js、jquery、html)(10)
web前端技能方法总结(css、js、jquery、html)
221 0
|
4天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
14天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
8天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
559 210