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