普通队列:

class queue {public:queue();bool        empty()const;bool         full() const;int   get_front(int& x)const;int  append(const int x);int  serve();int len() { return count; }
private:int         count;int          front, rear;int data[maxn];
};
queue::queue()
{count = 0;  front = rear = 0;
}bool  queue::empty()const
{if (count == 0)    return true;return false;//等价于:return front == rear;
}
bool    queue::full()const
{if (count == maxlen - 1)    return true;return  false;//等价于:return ( rear + 1 ) % maxlen == front ;
}int  queue::get_front(int& x)const//取队头
{if (empty())      return 0;x = data[(front + 1) % maxn];return x;
}
int queue::append(const int x)//入队列
{if (full())    return 0;rear = (rear + 1) % maxlen;data[rear] = x;count++;return 1;
}
int   queue::serve()//出队列
{if (empty())    return 0;front = (front + 1) % maxlen;count--;return 1;
}void yang(int n) {queue q;int i = 2, j = 1, x, p,iff;if (n == 1) { cout << 1 << " "; return; }q.append(1);if (n == 2) {cout << 1 << " " << endl;cout << 1 << " " << 1 << endl;return;}for (int k = 0; k < n - 0; k++)cout << " ";cout << 1 << " "<<endl;for (int k = 0; k < n - 1; k++)cout << " ";cout << 1 << " " << 1 << endl;q.serve();q.append(1);q.append(1);// cout << q.get_front(x) << " ";while (i < n-1 ){q.append(1);for (int k = 0; k < n - i; k++)cout << " ";cout << 1 << " ";//     q.serve();//    while (!q.empty()) {for (j = 1; j < i; j++) {p = q.get_front(x);q.serve();if (!q.empty()) {p += q.get_front(x);q.append(p);cout << p << " "; }/* else {iff = 0;break;}*/}q.serve();cout << 1 << " ";  q.append(1);cout << endl;//  }i++;}while (!q.empty()) {cout << q.get_front(x) << " ";q.serve();}cout << 1;
}int main()
{int n;while (1) {cout << "请输入数字: 按0退出";cin >> n;if (n == 0) break;cout << endl;cout << n << "行的杨辉三角:";cout << endl;yang(n);cout << endl;}
}

链队列

struct node {int data;node* next;};
class queue {public:queue();~queue() {while (!empty()) serve();delete front;} //释放头;bool        empty()const;bool         full() const;int   get_front(int& x)const;int  append(const int x);int  serve();int len() { return count; }
private:int         count;node* front, * rear;};
queue::queue()
{front = new node;count = 0;  front->next = NULL;rear = front;
}bool  queue::empty()const
{if (count == 0)    return true;return false;//等价于:return front == rear;
}
bool    queue::full()const
{if (count == maxlen - 1)    return true;return  false;//等价于:return ( rear + 1 ) % maxlen == front ;
}int  queue::get_front(int& x)const
{if (empty())      return 0;x =front->next->data;return x;
}
int queue::append(const int x)
{node* s = new node;s->next = NULL;s->data = x;rear->next = s;rear = s;count++;return 1;
}
int   queue::serve()
{if (empty())    return 0;node* u = front->next;front->next=u->next;delete u;if (front->next == NULL) rear = front;count--;return 1;
}void yang(int n) {queue q;int i = 2, j = 1, x, p,iff;if (n == 1) { cout << 1 << " "; return; }q.append(1);if (n == 2) {cout << 1 << " " << endl;cout << 1 << " " << 1 << endl;return;}for (int k = 0; k < n - 0; k++)cout << " ";cout << 1 << " "<<endl;for (int k = 0; k < n - 1; k++)cout << " ";cout << 1 << " " << 1 << endl;q.serve();q.append(1);q.append(1);// cout << q.get_front(x) << " ";while (i < n ){q.append(1);for (int k = 0; k < n - i; k++)cout << " ";cout << 1 << " ";//     q.serve();//    while (!q.empty()) {for (j = 1; j < i; j++) {p = q.get_front(x);q.serve();if (!q.empty()) {p += q.get_front(x);q.append(p);cout << p << " ";}/* else {iff = 0;break;}*/}q.serve();cout << 1 << " ";  q.append(1);cout << endl;//  }i++;}//   cout << 1;
}int main()
{int n;while (1) {cout << "请输入数字: 按0退出(链队列实现)";cin >> n;if (n == 0) break;cout << endl;cout << n << "行的杨辉三角:";cout << endl;yang(n);cout << endl;}
}

注意两种方式的区别:1.类中是否有析构函数和析构函数中释放结点的写法
2.在循环中,普通队列是 while (i < n-1 ) 链队列是while (i < n)且可以直接在循环中输出,而普通队列还进行了单独输出最后一层(当然,这个也可能是需要优化的代码)
3.链队列的构造函数:
queue::queue()
{
front = new node;
count = 0; front->next = NULL;
rear = front;
}
可以理解为:

结果:

【练习】c++分别用链队列和普通队列输出杨辉三角相关推荐

  1. 利用队列输出杨辉三角 C语言

    #include<stdio.h> #include<stdlib.h>#define MAXQSIZE 200typedef int QElemType;typedef st ...

  2. 杨辉三角队列c语言程序,C语言完整队列 与链式队列实现杨辉三角

    贵州商专/// 编译环境VS2010 #include "stdafx.h" #include "stdlib.h" #define M 100 int max ...

  3. 队列实现杨辉三角(附详细图解)

    之前我已经写了两篇关于队列的基本功能,今天我们使用循环顺序队来实现输出杨辉三角. 这是我们最终想要的效果:* 目录 一.算法思路 二.代码实现 三. 思考总结 一.算法思路 这里我将使用图示法配合文字 ...

  4. 20162303 队列加分项-杨辉三角

    要求 用实现循环队列 参考PPT用循环队列打印杨辉三角 用JDB或IDEA单步跟踪排队情况,画出队列变化图,包含自己的学号信息 把代码推送到代码托管平台 把完成过程写一篇博客:重点是单步跟踪过程和遇到 ...

  5. 杨辉三角c语言程序jian,杨辉三角C语言程序队列实现(带源码+解析)

    杨辉三角,即如下 通过学习数据结构,解决杨辉三角,可以使用循环来实现:在循环队列中依次存放第 i-1 行上的元素,然后逐个出队并打印,同时生成第 i 行上的元素并入队. 如果要求计算并输出杨辉三角前 ...

  6. 【数据结构(C++)】用链队列计算杨辉三角

    目录 第一节 概述 第二节 开源代码 第一节 概述 杨辉三角是二项式系数在三角形中的一种几何排列,是中国古代数学的杰出研究成果之一.它把二项式系数图形化,把组合数内在的一些代数性质直观地从图形中体现出 ...

  7. java数据结构队列杨辉三角_数据结构之队列——输出杨辉三角形

    定义 队列是一种操作受限的线性表,只允许在一端进行插入,另一端进行删除.插入的一端称为队尾,删除的一端称为队头,由于这样的限制,所以队列具有先进先出的特性,因此队列也是一种先进先出的线性表. 顺序存储 ...

  8. 数据结构--队列Queue--打印杨辉三角

    杨辉三角大家很熟悉,不做介绍了,第n行的首末两元素均为1,中间n-2个元素由n-1行相邻两元素相加得到. 将第1行数据入队(1) -------队列表示(队头,- 队尾)------- 第2行数据1入 ...

  9. 杨辉三角循环队列实现(数据结构c语言版)

    [问题描述]杨辉三角形是由[(a+b)]n二项式展开的各项系数形成的,当n=0,系数为1,生成第一行的元素:当a=1,a+b的各项系数组成第二行的元素:当n=2,a2+2ab+b^2的各项系数组成第三 ...

最新文章

  1. HashMap 的 7 种遍历方式与性能分析!(强烈推荐)
  2. Oracle误删除数据的恢复方法
  3. Javascript类的写法
  4. chrome调试的JavaScript官方技巧
  5. python第三方库安装-多种方式
  6. windows下安装使用couchdb
  7. python竖排文本_Calibre 5.0:高亮、Python 3、竖排文字
  8. python包的中 _init _.py文件介绍
  9. 利用扩展欧几里得算法编程求逆元
  10. 江南大学计算机科学esi排名,喜忧参半!2021年5月ESI世界大学排行榜,21个学科排名数据分析!...
  11. 基于spring+quartz的分布式定时任务框架
  12. orabbix监控oracle11g,orabbix 监控oracle
  13. SpringMVC框架搭建
  14. NLP自然语言处理库系列教程——gensim库
  15. tomcat开启远程调试
  16. jszip 解压压缩包_JavaScript 实现的 zip 压缩和解压缩工具包Zip.js使用详解
  17. p6spy监测mysql_Spring使用p6spy监控sql
  18. html 小喇叭图标,小喇叭不见了怎么办(小喇叭图标不见了的原因及解决办法)...
  19. 计算机应用专业的简历自我介绍,计算机应用个人简历模板
  20. 燕十八 Mysql 笔记 68 课

热门文章

  1. php 单元测试 麻烦,php – 正确的单元测试
  2. mysql redis qps_Redis QPS测试
  3. linux里的挂载错误无法开机怎么办,Linux基础知识 - 开机挂载错误
  4. 【Python金融量化 10- 100 】十、怎样的收益率预测模型才是好的模型?
  5. 三十九、Java集合中的HashSet和TreeSet
  6. 三十一、深入Python中的正则表达式
  7. 后门怎么写隐蔽java_用Java写黑软-后门篇
  8. 北京内推 | 微软亚洲研究院MSRA STCA招聘多模态算法实习生
  9. ICLR 2020 | 多模态下使用图片信息显著增强机器翻译效果
  10. 积分梯度:一种新颖的神经网络可视化方法