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;
}

队(queue),C++模板实现的更多相关文章

  1. POJ 3481 Double Queue (treap模板)

    Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...

  2. STL学习系列五:Queue容器

    Queue简介 queue是队列容器,是一种“先进先出”的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> 1.queue对象的默认构造 ...

  3. C++STL学习笔记_(4)queue

    10.2.5Queue容器 Queue简介 ²  queue是队列容器,是一种"先进先出"的容器. ²  queue是简单地装饰deque容器而成为另外的一种容器. ²  #inc ...

  4. STL - queue(队列)

    Queue简介 queue是队列容器,是一种"先进先出"的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> queu ...

  5. 04--STL序列容器(Stack和Queue)

    总括: stack和queue不支持迭代 一:栈Stack (一)栈的简介 stack是堆栈容器,是一种“先进后出”的容器. stack是简单地装饰deque容器而成为另外的一种容器. (二)栈的默认 ...

  6. STL之Queue容器

    1.Queue容器 1)queue是队列容器,是一种“先进先出”的容器. 2)queue是简单地装饰deque容器而成为另外的一种容器. 3)头文件.#include <queue> 2. ...

  7. STL Queue 容器

    STL Queue 容器 Queue简介         queue是队列容器,是一种“先进先出”的容器.         queue是简单地装饰deque容器而成为另外的一种容器.        # ...

  8. C++ STL 之 queue

    queue 是一种先进先出(first in first out, FIFO)的数据类型,他有两个口,数据元素只能从一个口进,从另一个口出.队列只允许从队尾加入元素,队头删除元素,必须符合先进先出的原 ...

  9. C++queue的使用

    C++队列是一种容器适配器,提供了一种先进先出的数据结构. 队列(queue)模板类定义在<queue>头文件中 基本操作: 定义一个queue变量:queue<Type> q ...

随机推荐

  1. Java命令使用 jmap,jps,jstack,jstat,jhat,jinfo

    Jmap:可以获得运行中的jvm的堆的快照,从而可以离线分析堆,以检查内存泄漏,检查一些严重影响性能的大对象的创建,检查系统中什么对象最多,各种对象所占内存的大小等等 Jmap是一个可以输出所有内存中 ...

  2. UVa 1001 奶酪里的老鼠(Dijkstra或Floyd)

    https://vjudge.net/problem/UVA-1001 题意:一个奶酪里有n个洞,老鼠在奶酪里的移动速度为10秒一个单位,但是在洞里可以瞬间移动.计算出老鼠从A点到达O点所需的最短时间 ...

  3. setSupportActionBar()方法报错

    在Android开发中,使用ToolBar控件替代ActionBar控件,需要在java代码中使用setSupportActionBar()方法,如下: Toolbar toolbar = (Tool ...

  4. 使用tk.mybatis快速开发curd

    使用mybatis已经是可以快速开发程序了,对于单表的curd似乎是一种可抽象的结果,下面介绍tk.mybatis的使用方式. maven引用 我使用的是这个版本,所以相关功能介绍也是这个版本. 使用 ...

  5. Linux忘记root登录密码解决方法

    有时候由于长时间米有登录linux系统,等需要用的时候突然忘记root密码,怎么办?下面简单介绍解决方法. redhat 和 centos 6.5 可以,7.0以上未测 在系统重启后,不停地按”e”键 ...

  6. xshell各个版本下载

    官网下载 怎么从官网下载Xshell 5 或者其他版本呢? 下面我们详细步骤说明! 1)首先我们打开netsarang官网, 点击下载Xshell 6 !填写邮箱等信息! http://www.net ...

  7. Spring+Mybatis+Dubbo报错java.lang.reflect.MalformedParameterizedTypeException

    原因是spring的jar文件冲突,排除spring的文件即可 <dependency> <groupId>com.alibaba</groupId> <ar ...

  8. Sergey's problem CodeForces - 1019C (图论,构造,神题)

    链接 大意: 给定有向图, 求选择一个点集$S$, 使得$S$任意两点不相连, 且对于不属于$S$的任意点$x$, 均存在$S$中的点$y$, 使得$d(x,y)<=2$, $d(x,y)$为从 ...

  9. python-day13--装饰器

    1.开放封闭的原则: 1.对扩展是开放的 为什么要对扩展开放呢? 我们说,任何一个程序,不可能在设计之初就已经想好了所有的功能并且未来不做任何更新和修改.所以我们必须允许代码扩展.添加新功能. 2.对 ...

  10. UVA-10539 Almost Prime Numbers

    题目大意:这道题中给了一种数的定义,让求在某个区间内的这种数的个数.这种数的定义是:有且只有一个素因子的合数. 题目分析:这种数的实质是素数的至少两次幂.由此打表——打出最大区间里的所有这种数构成的表 ...