当前位置: 首页 > news >正文

2)队列

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 
 5 enum error{overflow,underflow,success};
 6 const int maxlen=100;
 7 
 8 class queue{
 9 public:
10     queue();//初始化
11     bool empty()const;//判断为空
12     bool full()const;//判断为满
13     int get_front(int &x)const;//取队头元素
14     error append(const int x);//入队
15     error serve();//出队
16 private:
17     int count;//队列中元素计数
18     int rear,front;//队头,队尾
19     int data[maxlen];//存储队列中的数据
20 };
21 queue::queue(){//初始化
22     count=0;
23     rear=front=0;
24 }
25 
26 bool queue::empty()const{//判断为空
27     if(count==0)return true;
28     return false;
29 }
30 
31 bool queue::full()const{//判断为满
32     if(count==maxlen-1)return true;
33     return false;
34 }
35 
36 int queue::get_front(int &x)const{//取队头元素
37     if(empty())return underflow;
38     x=data[(front+1)%maxlen];
39     return success;
40 }
41 error queue::append(const int x){//入队
42     if(full())return overflow;
43     rear=(rear+1)%maxlen;
44     data[rear]=x;
45     count++;
46     return success;
47 }
48 
49 error queue::serve(){//出队
50     if(empty())return underflow;
51     front=(front+1)%maxlen;
52     count--;
53     return success;
54 }
55 
56 int main(){
57     queue q;
58     int n;
59     cout<<"please input 杨辉三角要打印的行数:";
60     cin>>n;
61     int s1,s2;
62     for(int i=1;i<n;i++)cout<<"  ";
63     cout<<1<<endl;//输出第一行上的1
64     q.append(1);//所输出1入队
65     for(int i=2;i<=n;i++){//逐行计算并输出2~N行上的数据
66         s1=0;//存放前一个入队数
67         for(int k=1;k<=n-i;k++ )cout<<"  ";
68         for(int j=1;j<=i-1;j++){//先计算并输出n-1个数
69             q.get_front(s2);//取队头元素并出队
70             q.serve();
71             cout<<s1+s2<<setw(4);
72             q.append(s1+s2);//所输出的当行中的元素入队
73             s1=s2;
74         }
75         cout<<1<<endl;//输出当行中的子最后一个元素1并换行
76         q.append(1);
77     }
78     return 0;
79 }

转载于:https://www.cnblogs.com/minmsy/p/5021926.html

相关文章:

  • 刚开通,记录一下
  • excel怎么固定第一行
  • zsh解决perl: warning: Setting locale failed.
  • svn checkout的时候, 不要将以此目录为工程的netbeans等ide打开
  • Error: ShouldNotReachHere()
  • exports和module.exports
  • apk当安装程序将文件复制到手机自带的指定文件夹
  • 接口测试培训:HTTP协议基础 1
  • 动态计算UITableViewCell高度详解
  • hibernate(四)ID生成策略
  • 【iCore3 双核心板】例程十四:FATFS实验——文件操作
  • 配置Server.xml
  • 工作小记(五)----完工归来
  • jQuery API
  • MIT Introduction to Algorithms 学习笔记(四)
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • Apache Zeppelin在Apache Trafodion上的可视化
  • isset在php5.6-和php7.0+的一些差异
  • Javascript弹出层-初探
  • Linux后台研发超实用命令总结
  • Octave 入门
  • scrapy学习之路4(itemloder的使用)
  • spring + angular 实现导出excel
  • weex踩坑之旅第一弹 ~ 搭建具有入口文件的weex脚手架
  • 测试开发系类之接口自动化测试
  • 从tcpdump抓包看TCP/IP协议
  • 分享一份非常强势的Android面试题
  • - 概述 - 《设计模式(极简c++版)》
  • 前端面试之闭包
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 如何设计一个比特币钱包服务
  • 王永庆:技术创新改变教育未来
  • 新书推荐|Windows黑客编程技术详解
  • 一个项目push到多个远程Git仓库
  • 1.Ext JS 建立web开发工程
  • gunicorn工作原理
  • ​ubuntu下安装kvm虚拟机
  • # 计算机视觉入门
  • #每天一道面试题# 什么是MySQL的回表查询
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (07)Hive——窗口函数详解
  • (3)STL算法之搜索
  • (30)数组元素和与数字和的绝对差
  • (LeetCode) T14. Longest Common Prefix
  • (草履虫都可以看懂的)PyQt子窗口向主窗口传递参数,主窗口接收子窗口信号、参数。
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (一) storm的集群安装与配置
  • (转)http协议
  • (转)项目管理杂谈-我所期望的新人
  • (轉貼)《OOD启思录》:61条面向对象设计的经验原则 (OO)
  • ***测试-HTTP方法
  • .equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
  • .NET 5种线程安全集合