《C语言 队列实现杨辉三角的打印.docx》由会员分享,可在线阅读,更多相关《C语言 队列实现杨辉三角的打印.docx(5页珍藏版)》请在三一办公上搜索。
1、C语言 队列实现杨辉三角的打印#include<stdio.h>#include<conio.h>#define MAXSIZE 100typedef int QueueElemType;typedef structQueueElemType elemMAXSIZE;int front;int rear;SeqQueue;void InitQueue(SeqQueue *Q)Q->front = Q->rear = 0;int EnterQueue(SeqQueue *Q, QueueElemType x)if (Q->rear + 1) % MAXSIZE = Q
2、->front)return 0;Q->elemQ->rear = x;Q->rear = (Q->rear + 1) % MAXSIZE;return 1;int DeleteQueue(SeqQueue *Q, QueueElemType *x)if (Q->front = Q->rear)return 0;*x = Q->elemQ->front;Q->front = (Q->front + 1) % MAXSIZE;return 1;int GetHead(SeqQueue *Q, QueueElemType *x)if (Q->fron
3、t = Q->rear)return 0;else*x = Q->elemQ->front;return 1;int IsEmpty(SeqQueue *Q)if (Q->front = Q->rear)printf_s(Empty!);return 1;int mainSeqQueue Q;InitQueue(&Q);EnterQueue(&Q, 1);int n, i, temp, N;int x = 1;for (n = 2;n <= 11;n+)EnterQueue(&Q, 1);for (i = 1;i <= n - 2;i+)DeleteQueue(&Q, &temp);printf_s(%5d, temp);GetHead(&Q, &x);temp = temp + x;EnterQueue(&Q, temp);DeleteQueue(&Q, &x);printf_s(%5d, x);printf_s(n);EnterQueue(&Q, 1);while (!IsEmpty(&Q)DeleteQueue(&Q, &x);printf_s(%5d, x);_getch;return 0;