环形队中实现队列的基本运算
2014-04-02 19:17
465 查看
/*队空条件:front=rear 队满条件:(rear+1)%MaxSize=front 进队e操作:rear=(rear+1)%MaxSize; 将e放在rear处 出队操作:front=(front+1)%MaxSize;取出front处元素e; */ #include <iostream> #include <malloc.h> using namespace std; const int maxn=4; typedef struct { int data[maxn]; int front,rear; }queue; //初始化队列 void init(queue *&q) { q=(queue*)malloc(sizeof(queue)); q->rear=q->front=0; } //销毁队列 void destroy(queue *&q) { free(q); } //判断队列是否为空 bool empty(queue *&q) { return q->front==q->rear; } //进队列 void push(queue *&q,int e) { if((q->rear+1)%maxn==q->front) { cout<<"队满,无法入队!"<<endl; return; } q->rear=(q->rear+1)%maxn; q->data[q->rear]=e; } //出队列 void pop(queue *&q) { if(q->front==q->rear) { cout<<"队空,无元素!"<<endl; return ; } q->front=(q->front+1)%maxn; } int main() { queue *q; init(q); push(q,2); push(q,4); cout<<empty(q); pop(q); pop(q); cout<<empty(q); return 0; }
相关文章推荐
- 环形队中实现队列的基本运算
- MySQL 模拟条件索引
- WinCE 下结构体占用空间的分析
- 【翻译】五步快速使用LINQPad尝鲜StreamInsight
- 手机验证码倒计时
- Java读取property配置文件的方法
- 解决linux的-bash: ./test: Permission denied
- Button的使用(七):RadioGroup、RadioButton
- FAFU-1398 面积 矩形面积并 线段树+扫描线
- STM32片外内存的使用
- 一个男人关心的东西 决定了他的层次
- javaweb问题求解
- oc第一天课程代码(NSString常用函数及初始化方式)
- How to send sms by .net
- 大型网站架构演化
- 提取投影直方图的多个主分量
- 2-sat入门(tarjan)hdu(3062)
- Could not find the main class: org.apache.catalina.startup.Boostrap. Program will exit
- 第五周项目1-4使用有默认参数的构造函数
- 求解继承问题