队列基本函数(杨辉三角模型)
来源:互联网 发布:程序员 任务开发流程 编辑:程序博客网 时间:2024/06/05 05:39
头文件:
#ifndef _SEQUEUE_H_#define _SEQUEUE_H_#define MAXSIZE 10#define SUCCESS 10000#define FAILURE 10001typedef int ElemType;struct sequeue{ ElemType data[MAXSIZE]; int front; int rear;};typedef struct sequeue Sequeue;#endif
子函数:
#include <stdio.h>#include "SeQueue.h"int QueueInit(Sequeue *Q){ Q->rear = Q->front = 0; return SUCCESS;}int EnQueue(Sequeue *Q,ElemType e){ if((Q->rear + 1) % MAXSIZE == Q->front) { return FAILURE; } Q->data[Q->rear] = e; Q->rear = (Q->rear + 1) % MAXSIZE; return SUCCESS;}int QueueLength(Sequeue Q){ return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;}ElemType GetFront(Sequeue Q){ return Q.data[Q.front];}ElemType DelQueue(Sequeue *Q){ ElemType e; if(Q->rear == Q->front) { return FAILURE; } e = Q->data[Q->front]; Q->front = (Q->front + 1) % MAXSIZE; return e;}int QueueClear(Sequeue *Q){ while(Q->rear != Q->front) { Q->front = (Q->front + 1) % MAXSIZE; } return SUCCESS;}
主函数:
#include <stdio.h>#include "SeQueue.h"int main(){ int ret,i; Sequeue sq; ret = QueueInit(&sq); if(FAILURE == ret) { printf("Init Failure!\n"); } else { printf("Init Success!\n"); } for(i = 0; i < 5; i++) { ret = EnQueue(&sq); if(FAILURE == ret) { printf("Enter Queue Failure!\n"); } else { printf("Enter %d Success!\n",i + 1); } } printf("Length is %d!\n",QueueLength(sq)); printf("Front Element is %d\n",GetFront(sq)); for(i = 0; i < 3; i++) { printf("Delete %d Success!\n",DelQueue(&sq)); } ret = QueueClear(&sq); if(FAILURE == ret) { printf("Clear Failure!\n"); } else { printf("Clear Success!\n"); } printf("Length is %d!\n",QueueLength(sq)); return 0;}
杨辉三角模型:
#include <stdio.h>#include "SeQueue.h"int main(){ Sequeue a,b; int i,num; int length,j,ret; QueueInit(&a); QueueInit(&b); printf("Please input line:\n"); scanf("%d",&num); for(i = 0; i < num; i++) { if(0 == i) { EnQueue(&a,0); EnQueue(&a,1); EnQueue(&a,0); printf("1"); } else { EnQueue(&b,0); while(QueueLength(a) != 1) { EnQueue(&b,DelQueue(&a) + GetFront(a)); } DelQueue(&a); EnQueue(&b,0); } length = QueueLength(b); for(j = 0; j < length; j++) { ret = DelQueue(&b); if(ret != 0) { printf("%d ",ret); } EnQueue(&a,ret); } printf("\n"); }}
阅读全文
0 0
- 队列基本函数(杨辉三角模型)
- 杨辉三角(队列)
- 队列--杨辉三角
- 队列实现杨辉三角
- 杨辉三角队列方式
- 顺序队列(循环队列)+链式队列+打印杨辉三角
- 杨辉三角c源码(循环队列)
- 用循环队列实现打印杨辉三角(数据结构)
- 打印杨辉三角(STL版队列)
- C队列 输出杨辉三角
- 用队列实现杨辉三角
- 杨辉三角之c++队列实现
- 循环队列实现杨辉三角
- 循环队列实现杨辉三角
- 输出杨辉三角 队列小应用
- 队列实现杨辉三角打印
- 队列实现 杨辉三角的打印
- 利用队列输出杨辉三角
- 18 i2c控制器之应用程序的调用
- mysql的几种常见的模糊查询
- Unable to find method 'org.gradle.api.internal.file.DefaultSourceDirectorySet
- (转)性能测试总结(二)---测试流程篇
- vue中的事件
- 队列基本函数(杨辉三角模型)
- iOS高仿“随便走”APP
- 【Scikit-Learn 中文文档】大规模计算的策略: 更大量的数据
- 利用反射+
- Cannot upgrade/downgrade to Dynamic Web Module 3.0 facet. It is incompatibile with already installed
- xcode新建了.h文件没办法联想其他头文件---(做个记录,公司组里面居然都不知道额)
- 数据库 case when then
- 学习手册:浅析DDoS的攻击及防御
- DNS安全认证的机制之DNSSEC(DNS安全扩展)