#include "stdafx.h"
/*

队列是一种先进先出的线性表
队列的核心是对头部和尾部索引的操作

如上图所示,当对头索引移动到最前面6,队尾又不不再末尾0的位置,那么如果不循环利用此栈,队列就满了,为此采用(f+1%maxSize)的方式进行

当对头索引到6的位置 求余结果恰好为0 又回到对头了。这便实现了循环利用。(注意maxSize=6+1,这是c++数组特性决定了的)
*/
template <class T>
class BaseQueuq {
  //返回对头元素给X
  virtual bool front(T &x) = 0;
  //进队
  virtual bool enQueue(T &x) = 0;
  //出队
  virtual bool deQueue() = 0;
  //清空队列
  virtual void clear() = 0;
};
template <class T>
class Queue :public BaseQueuq<T> {
  int _indexF, _indexR,_maxSize;
  T *_queue;
public:
  Queue(int maxSize){
    _maxSize = maxSize;
    _queue = new T[_maxSize];
    _indexF = _indexR = 0;
  }
  bool front(T &x) {
    if (isEmpty()) {
      return false;
    }
    else {
      x = _queue[_indexF];
      return true;
    }
  }
  bool enQueue(T &x) {
i    f (isFull()) {
      //over flow
      return false;
    }
    else {
      _indexF=(_indexF+1)%_maxSize; //对头指针前进一
      _queue[_indexF] = x;
      return true;
    }
  }
  bool deQueue() {
    //ENTER
    if (isFull()) {
      //over flow
      return false;
    }
    else {
      _indexR = (_indexR+1) % _maxSize; //对尾指针向前移动1
      return true;
    }
  }
  void clear() {
    _indexF = _indexR = 0;
  }
  bool isFull() {
    if ((_indexR + 1) % _maxSize == _indexF) {//因为循环队列 要留一个空间就是要与空队列作为区分
      return true;
    }
    else {
      return false;
    }
  }
  bool isEmpty() {
    return _indexF == _indexR;
  }
};

int main()
{
int i = 3,j;
Queue<int> *test = new Queue<int>(5);
test->enQueue(i);
test->front(j);
printf("%d",j);
while (1);
return 0;
}

最新文章

  1. oracle使用sqlplus创建表空间
  2. 处于同一个域中的两台Sql server 实例无法连接
  3. 对最近的RTP和H264学习进行总结整理-04.20
  4. SqlDateTime 溢出。
  5. LeetCode——Jump Game
  6. linux下svn常用指令
  7. DOS命令生成文件列表
  8. JAXB - The JAXB Context
  9. dede列表标签递增数字生成
  10. Weblogic缓存
  11. ios学习Day3xiawu
  12. Linux内核源代码解析之——我与神童聊Linux内核
  13. 【AllJoyn专题】基于AllJoyn和Yeelink的传感器数据上传与指令下行的研究
  14. HDU 4520 小Q系列故事――最佳裁判(STL)
  15. libprotobuff8.so not found
  16. opencart配置税率
  17. Centos7上安装Kubernetes集群部署docker
  18. Echarts数据可视化series-scatter散点图,开发全解+完美注释
  19. PAT乙级-1043. 输出PATest(20)
  20. linux截取字符串之sort、uniq、cut用法

热门文章

  1. ElasticSearch 7.1.1 集群环境搭建
  2. 读取ClassPath下resource文件的正确姿势
  3. 热度3年猛增20倍,Serverless&amp;云开发的技术架构全解析
  4. php接口数据安全解决方案(二)
  5. spring系列(一):超级经典入门
  6. Atlassian In Action-Jira之核心配置(二)
  7. 20141126-DotNetStack
  8. NET多线程之进程间同步锁Mutex
  9. NOIP2018初赛题解 提高组
  10. Excel催化剂开源第46波-按行列排列多个图形技术要点