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 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 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 2 1 1 3 3 1 1 4 6 4 1
当下 域名 投资市场处于“冰点”,域名投资行情不景气,在这样的大环境下,投资...
作者:叶永杰 来源:金融级分布式架构公众号 本文将介绍 WebAssembly 技术在 MOS...
比较稳定的外贸 企业邮箱 ,推荐使用 TOP云 企业 邮箱 。TOP云英文版企业邮箱,...
本示例使用的工具及驱动版本 Kettle 8.2.0.0-342 MaxCompute JDBC驱动 v3.2.8 Se...
6月10日,《中华人民共和国数据安全法》正式颁发,自2021年9月1日起施行。 《数...
过去的一年蜀山区培育了市级认定大数据企业101家,近日,蜀山区数据资源局举办了...
图片来自 Pexels NO.1.第 1 关 我查了一天啥是工作流,然后做出了如上图版本: ...
1.我喝了那么多次优乐美奶茶,也没见周杰伦问我是他的谁 2.以后谁敢欺负我,就...
服务器租用 的线路选择问题 服务器租用 是企业建站常用的方式之一,很多站长在进...
TOP云 (west.cn)12月20日消息,Sedo榜刚刚公布了73个成交价在2000美元以上的 ...