队(queue),C++模板实现
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++模板实现的更多相关文章
- POJ 3481 Double Queue (treap模板)
Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...
- STL学习系列五:Queue容器
Queue简介 queue是队列容器,是一种“先进先出”的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> 1.queue对象的默认构造 ...
- C++STL学习笔记_(4)queue
10.2.5Queue容器 Queue简介 ² queue是队列容器,是一种"先进先出"的容器. ² queue是简单地装饰deque容器而成为另外的一种容器. ² #inc ...
- STL - queue(队列)
Queue简介 queue是队列容器,是一种"先进先出"的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> queu ...
- 04--STL序列容器(Stack和Queue)
总括: stack和queue不支持迭代 一:栈Stack (一)栈的简介 stack是堆栈容器,是一种“先进后出”的容器. stack是简单地装饰deque容器而成为另外的一种容器. (二)栈的默认 ...
- STL之Queue容器
1.Queue容器 1)queue是队列容器,是一种“先进先出”的容器. 2)queue是简单地装饰deque容器而成为另外的一种容器. 3)头文件.#include <queue> 2. ...
- STL Queue 容器
STL Queue 容器 Queue简介 queue是队列容器,是一种“先进先出”的容器. queue是简单地装饰deque容器而成为另外的一种容器. # ...
- C++ STL 之 queue
queue 是一种先进先出(first in first out, FIFO)的数据类型,他有两个口,数据元素只能从一个口进,从另一个口出.队列只允许从队尾加入元素,队头删除元素,必须符合先进先出的原 ...
- C++queue的使用
C++队列是一种容器适配器,提供了一种先进先出的数据结构. 队列(queue)模板类定义在<queue>头文件中 基本操作: 定义一个queue变量:queue<Type> q ...
随机推荐
- git源码阅读
https://github.com/git-for-windows/git/issues/1854 https://github.com/git-for-windows/git/pull/1902/ ...
- HBase底层存储原理——我靠,和cassandra本质上没有区别啊!都是kv 列存储,只是一个是p2p另一个是集中式而已!
理解HBase(一个开源的Google的BigTable实际应用)最大的困难是HBase的数据结构概念究竟是什么?首先HBase不同于一般的关系数据库,它是一个适合于非结构化数据存储的数据库.另一个不 ...
- java中集合格式及json格式的特点和转换
作者原创:转载请注明出处 今天在写代码,遇到一个难点,由于要调用webservice接口,返回的为一个list集合内容,从webservice调用接口返回的为一个string的io流, 在调用接口的地 ...
- xtu 1242 Yada Number 打表
Yada Number Time Limit : 2000 MS Memory Limit : 65536 KB Yada Number Problem Description: ...
- python ros 关闭节点
def myhook(): print "shutdown time!" rospy.on_shutdown(myhook) 或 rospy.signal_shutdown(rea ...
- shell逻辑运算符
逻辑运算符 以下介绍 Shell 的逻辑运算符,假定变量 a 为 10,变量 b 为 20: 运算符 说明 举例 && 逻辑的 AND [[ $a -lt 100 && ...
- Ubuntu16.04 安装 Django
pip2 install django==1.11 或者手动安装: 链接:https://pan.baidu.com/s/1uQJD-pON7gELoCC2TwYnEw 提取码:flgg cd Dja ...
- python 基数排序
def radix_sort(array): bucket, digit = [[]], 0 while len(bucket[0]) != len(array): bucket = [[], [], ...
- 使用 rlist 包处理嵌套数据结构
在前面的章节中,我们已经学习了存储表的关系型数据库,支持嵌套数据结构的非关系型数据库.在 R 中,最常见的嵌套数据结构就是列表对象.之前的章节都关注操作表格数据.本节,我们一起玩转作者开发的 rlis ...
- _proto_和prototype区别
推荐一篇阅读:http://cometosay.com/2016/08/31/js-proto.html es中创建对象的方法 (1)对象字面量的方式 (2)new 的方式 (3)ES5中的`Obje ...