body, table{font-family: 微软雅黑; font-size: 13.5pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}

队:(queue.h)

#include<iostream>
#include<string>
using namespace std;
//队列判空和判满
//头尾指针相同为空
//尾指针指向下一个可存放数据的单元,如果尾指针偏移一个单元和头指针相同,队列为满

template<class T,int num>
class queue
{
        public:
                queue();
                ~queue();
                bool empty();
                bool full();
                bool push(T elem);
                bool pop(T& tmp);
                int size();
        private:
                int _front;
                int _real;
                T _arr[num];
};
template<class T,int num>
queue<T,num>::queue():_front(0),_real(0){}
template<class T,int num>
queue<T,num>::~queue(){}
template<class T,int num>
bool queue<T,num>::empty()
{
        return _front == _real;
}
template<class T,int num>
bool queue<T,num>::full()
{
        return _front == (_real+1)%num;
}
template<class T,int num>
bool queue<T,num>::push(T elem)
{
        if(!full())
        {
                _arr[_real] = elem;
                _real = (_real+1)%num;
                return true;
        }
        else
                return false;
}
template<class T,int num>
bool queue<T,num>::pop(T &tmp)
{
        if(!empty())
        {
                tmp = _arr[_front];
                _front = (_front+1)%num;
                return true;
        }
        else
                return false;
}
template<class T,int num>
int queue<T,num>::size()
{
        return (_real-_front+num)%num;
}

测试文件(queueTest.cpp)

#include"queue.h"
int main()
{
        queue<int,10> q1;
        q1.push(3);
        q1.push(5);
        int tmp;
        cout<<q1.size()<<endl;
        q1.pop(tmp);
        cout<<tmp<<endl;
        cout<<"----------------------"<<endl;
        queue<string,5> q2;
        q2.push("hello");
        q2.push("world");
        cout<<q2.size()<<endl;
        string tmpString;
        q2.pop(tmpString);
        cout<<q2.size()<<"  "<<tmpString<<endl;
        return 0;
}

最新文章

  1. Python报错UnicodeDecodeError: ascii codec can t decode byte 0xe0 ...解决方法
  2. 《图解HTTP》阅读笔记
  3. TFS源代码管理的8大注意事项
  4. 【nodejs笔记1】配置webstorm + node.js +express + mongodb开发博客的环境
  5. git本地分支
  6. webservice报错Message part refundRequest was not recognized. (Does it exist in service WSDL?)
  7. java中的构造函数
  8. ajax开发框架和XMLhttpRequest、responseText、responseXml和JSON的应用
  9. 笔记一:Python的PyDev插件在eclipse上面安装(新的插件地址 location)
  10. OpenSSL “心脏滴血”漏洞
  11. iOS当该装置是水平屏,frame和bounds分别
  12. c#基于这些,你已经看到了?(一)-----谁才刚刚开始学习使用
  13. TCP/IP 协议族的简介
  14. 分布式架构实战--ActiveMQ的安装与使用(单节点)
  15. VueI18n插件的简单应用于国际化
  16. Spring常用接口和类
  17. [ SSH框架 ] Struts2框架学习之一
  18. SpringBoot日记——ElasticSearch全文检索
  19. oracle 11g中文乱码解决的办法一
  20. [SublimeText] 之 Packages

热门文章

  1. Ansible 入门指南 - ansible-playbook 命令
  2. 最大子段和SP1716GSS3 线段树
  3. CodeCombat多人游戏Greed
  4. Python 逗号的几种作用
  5. Java自学入门新的体会0.2
  6. BZOJ 1009: [HNOI2008]GT考试(kmp+dp+矩阵优化)
  7. 洛谷P2777 [AHOI2016初中组]自行车比赛
  8. Java中Arrays 与 Collections 的简单操作
  9. shell逻辑运算符
  10. C语言专题-基本数据类和占位符