环形队列的基本运算
来源:互联网 发布:拼图软件知乎 编辑:程序博客网 时间:2024/04/28 16:17
/*程序的版权和版本声明部分:*Copyright(c)2014,烟台大学计算机学院学生*All rights reserved.*文件名称:*作者:田成琳*完成日期:2014 年 9 月 30 日*版本号:v1.0*对任务及求解方法的描述部分:*问题描述:队列的基本运算*程序输入:*程序输出:*问题分析:*算法设计:*/#include <iostream>#include <cstdlib>using namespace std;const int MaxSize = 50;struct QuType{ int data[MaxSize];//存放队列中元素 int front;//对头指针 int count;//队尾指针};void InitQueue(QuType *&q);//初始化队列void DestoryQueue(QuType *&q);//销毁队列bool QueueEmpty(QuType *q);//判断队列是否为空bool enQueue(QuType *&q,int e);//进队列bool deQueue(QuType *&q,int &e);//出队列int NumQueue(QuType *&q);//队列的元素个数void DisplayQueue(QuType *&q);//显示队列中元素void InitQueue(QuType *&q){ q=(QuType *)malloc(sizeof(QuType)); q->front=q->count=0;}void DestoryQueue(QuType *&q){ free(q);}bool QueueEmpty(QuType *q){ return (q->count==0);}bool enQueue(QuType *&q,int e){ int rear;//临时队尾指针 if(q->count==MaxSize)//队满上溢 return false; else { rear=(q->front+q->count)%MaxSize;//求队尾位置 q->data[rear]=e; q->count++; return true; }}bool deQueue(QuType *&q,int &e){ if(q->count==0)//队列为空 return false; else { e=q->data[q->front]; q->front=(q->front+1)%MaxSize;//对头循环+1 q->count--; return true; }}int NumQueue(QuType *&q){ return q->count;}void DisplayQueue(QuType *&q){ int head=q->front; while(head<=q->count) { cout<<q->data[head]<<" "; head++; }}int main(){ QuType *q; int n,num; InitQueue(q); if(QueueEmpty(q)) cout<<"队列为空。"<<endl; else cout<<"队列不为空。"<<endl; cout<<"现在将1-5进队"<<endl; for(int i=1;i<=5;i++) enQueue(q,i); cout<<"请再输入一个元素进队:"<<endl; cin>>n; enQueue(q,n); cout<<"队列中的元素个数为:"<<endl; cout<<NumQueue(q)<<endl; cout<<"现在出队一个元素:"; deQueue(q,num); cout<<num<<","<<"出队后剩余元素为:"<<endl; DisplayQueue(q); DestoryQueue(q); return 0;}
运行结果:
0 0
- 环形队列的基本运算
- 顺序环形队列的各种基本运算
- 环形队列各种基本运算的算法
- 环形队列的基本运算实现
- 环形队列中实现队列的基本运算
- 环形队中实现队列的基本运算
- 编写一个程序,实现顺序环形队列的各种基本运算。
- 数据结构第三章实验3.实现环形队列的各种基本运算
- 环形队列的存储及基本操作
- 特殊环形队列基本操作
- 队列的基本运算实现
- 编写一个一个程序实现顺序环形队列各项基本运算(假设队列基本元素类型为Char)
- 环形队列的应用
- 环形队列的实现
- 队列之环形队列基本操作
- 环形消息队列的实现
- 环形队列的一些看法
- 环形队列的c++实现
- 程序 c/c++、Java编写一个统计小程序
- HDOJ 5053 the Sum of Cube
- 微商营销实战技巧分享,轻松月入10W
- 创建一个抽象类A,该类中包含一个求两个数之和抽象方法。创建一个子类B,在B中重写求和方法,且使用方法重载使得
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- 环形队列的基本运算
- 继承
- Vim(gvim)实现多文件的查找和替换
- 获取当前安装的所有App
- visual studio 2013 中的图形
- USACO Musical Themes 解题报告
- OWL Carousel integration with AngularJS
- 猜数字
- 如何编写Socket套接字