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. JavaScript 获取地址栏参数

    1. function a() { console.log(this); } a.call(null); window 如果第一个参数传入的对象调用者是null或者undefined的话,call方法 ...

  2. ZooKeeper与Kafka相关

    Kafka集群搭建: https://www.cnblogs.com/likehua/p/3999538.html https://www.cnblogs.com/mikeguan/p/7079013 ...

  3. 【集群搭建】Zookeeper集群环境配置

    1.下载解压安装文件 2.配置文件:conf/zoo.cfg tickTime=2000 dataDir=/usr/sunny/logs/zookeeper/data dataLogDir=/usr/ ...

  4. 浅谈java中死锁问题

    知识点:死锁的产生.死锁的实例 一:死锁的产生 我们在解决多线程共享资源的线程同步问题时,会使用synchronized关键字修饰方法或者通过Lock加锁方式修饰方法.代码块,防止多个线程访问统一资源 ...

  5. 【Coursera】Fifth week(3)

    Ethernet 在 PARC(Xerox) 发明. 第一个 Local-Area-Network (LAN 局域网). 把 PCs 连接到 激光打印机上. 在夏威夷大学,被早期的无线网络 Aloha ...

  6. shell 余弦值转角度

    范例:余弦值转角度 用 bc -l 计算,可以获得高精度: $ export cos=0.996293; echo "scale=100; a(sqrt(1-$cos^2)/$cos)*18 ...

  7. JS + flash 复制

    js代码ZeroClipboard组件制作复制剪切板复制粘贴文字内容,一键即可复制粘贴文字内容.兼容各大主流浏览器firefox,,Chrome,IE等. 演示代码 如下: <script ty ...

  8. linux下模拟FTP服务器(笔记)

    要在linux下做一个模仿ftp的小型服务器,后来在百度文库中找到一份算是比较完整的实现,就在原代码一些重要部分上备注自己的理解(可能有误,千万不要轻易相信). 客户端: 客户端要从服务器端中读取数据 ...

  9. AtomicLong与LongAdder的区别

    AtomicLong的原理 AtomicLong是通过依靠底层的CAS来保障原子性的更新数据,在要添加或者减少的时候,会使用死循环不断地cas到特定的值,从而达到更新数据的目的. LongAdder的 ...

  10. 移动端视频h5表现问题汇总

    1. 同屏播放视频 <video src="" x-webkit-airplay="true" webkit-playsinline="true ...