标签:
/**************************************** 打印杨辉三角 date: 2016-10-15 writer: liu kun reference: 数据结构 殷人昆 *****************************************/ #include <iostream> #include<iomanip> #include "queue.h" using namespace std; //控制数字间隔 char blank[3+1] = " "; void YANGVI(int n) { Queue q; EnQueue(q,1);EnQueue(q,1); int i,j;QElemType s=0,t; for(i=1;i<=n;i++) { cout<<endl; // 每行起始位置排版 for(int bl_count=0;bl_count<n-i;bl_count++) cout<<blank; EnQueue(q,0); for(j=1;j<=i+2;j++) //第 i 行的 i+2 个系数,包括一个 0 { DeQueue(q,t); EnQueue(q,s+t); //计算下一行系数并入队 s=t; if(j!=i+2)cout<<setw(sizeof(blank)-1)<<s<<blank; } } }; int main() { YANGVI(10); return 0; }
> queue.h
#ifndef QUEUE_H_INCLUDED #define QUEUE_H_INCLUDED #define MAXSIZE 500 typedef int QElemType; typedef struct Queue{ int maxSize=MAXSIZE; QElemType *data=new QElemType[maxSize]; int front=0; int rear=front; }Queue; void InitQueue(Queue &q); int EnQueue(Queue &q, QElemType x) { //check full if((q.rear+1)%q.maxSize==q.front) { return 0; } else{ q.data[q.rear]=x; q.rear = (q.rear+1)%q.maxSize; return 1; } } int DeQueue(Queue &q,QElemType& x) { // check empty, ERROR code 1 if(q.rear==q.front) return 0; else{ x = q.data[q.front]; q.front=(q.front+1)%q.maxSize; return 1; } } int QueueEmpty(Queue &q) { if(q.rear==q.front)return 1; else return 0; } // 引用作为地址传值 int QueueFull(Queue &q) { return (q.rear+1)%q.maxSize==q.front; } int QueueSize(Queue& q) { return (q.rear+q.maxSize-q.front)%q.maxSize; } #endif // QUEUE_H_INCLUDED
标签:
原文地址:http://www.cnblogs.com/learn-to-rock/p/5965581.html