类模板Queue的实现
#include <iostream>
#include <vector>
using namespace std; template <class Type> class Queue;
template <class T > ostream & operator << (ostream &os, const Queue<T> &); template <class Type> class QueueItem {
friend class Queue<Type >;
friend ostream & operator << <Type> (ostream &os, const Queue<Type> &);
//private class
QueueItem(const Type &val) : Item(val), next() {}
Type Item;
QueueItem<Type > *next;
}; template <class Type> class Queue {
friend ostream & operator << <Type> (ostream &os, const Queue<Type> &); public:
Queue() : head(), tail() {}
template <class It> Queue(It _beg, It _end) : head(), tail() { copy_elems(_beg, _end); }
Queue(const Queue<Type> &Q) : head(), tail() { copy_elems(Q); }
Queue& operator = (const Queue &Q);
~Queue() { Destroy(); }
template <class Iter> void _assign(Iter, Iter);
Type &_front() { return head->Item; }
const Type &_front() const { return head->Item; }
void _push(const Type&);
void _pop();
bool _empty() const { return head == ; } private:
QueueItem<Type> *head;
QueueItem<Type> *tail;
void Destroy();
void copy_elems(const Queue<Type> &);
template <class Iter> void copy_elems(Iter, Iter);
}; template <class Type> Queue<Type>& Queue<Type>::operator = (const Queue<Type> &q)
{
if (this != &q) {
head = tail = ;
for (QueueItem<Type> *pt = q.head; pt; pt= pt->next) {
_push(pt->Item);
}
}
return *this;
} template <class Type> void Queue<Type>::copy_elems(const Queue<Type> &q)
{
for (QueueItem<Type> *pt = q.head; pt; pt = pt->next) {
_push(pt->Item);
}
}
template <class Type> template <class Iter> void Queue<Type>::copy_elems(Iter _beg, Iter _end)
{
while (_beg != _end) {
_push(*_beg);
++_beg;
}
} template <class Type> template <class Iter> void Queue<Type>::_assign(Iter _beg, Iter _end)
{
head = tail = ;
copy_elems(_beg, _end);
} template <class Type> void Queue<Type>::_push(const Type& val)
{
QueueItem<Type> *p = new QueueItem<Type>(val);
if (_empty()) {
head = tail = p;
} else {
tail->next = p;
tail = p;
}
} template <class Type> void Queue<Type>::_pop()
{
QueueItem<Type> *p = head;
head = head->next;
delete p;
} template <class Type> ostream & operator << (ostream &os, const Queue<Type> &q)
{
for (QueueItem<Type> *pt = q.head; pt; pt = pt->next) {
os << pt->Item << " ";
}
os << endl;
return os;
} template <class Type> void Queue<Type>::Destroy()
{
while (!_empty()) _pop();
} int main()
{
Queue<int >que;
que._push();
que._push();
que._push();
que._push();
cout << que;
que._pop();
cout << que;
Queue<int >q2, q1(que);
cout << q1;
cout << q2._empty() << endl;
q2 = q1;
cout << q2;
while (!que._empty()) {
cout << que._front() << endl;
que._pop();
}
vector<int > vet(, );
for (int i = ; i < (int)vet.size(); i++) {
cout << vet[i] << ' ';
}
cout << endl;
que._assign(vet.begin(), vet.end());
cout << que;
return ;
}

类模板Queue的实现的更多相关文章
- C++学习笔记50:队列类模板
队列是只能向一端添加元素,从另一端删除元素的线性群体 循环队列 在想象中将数组弯曲成环形,元素出队时,后继元素不移动,每当队尾达到数组最后一个元素时,便再回到数组开头. 队列类模板 //Queue.h ...
- C++标准库类模板(stack)和 队列(queue)
在C++标准库(STL)中有栈和队列的类模板,因此可以直接使用 1.栈(stack):使用栈之前,要先包含头文件 : #include<stack> stack.push(elem); / ...
- 连分数(分数类模板) uva6875
//连分数(分数类模板) uva6875 // 题意:告诉你连分数的定义.求连分数,并逆向表示出来 // 思路:直接上分数类模板.要注意ai可以小于0 #include <iostream> ...
- C++ 模板的编译 以及 类模板内部的实例化
在C++中.编译器在看到模板的定义的时候.并不马上产生代码,仅仅有在看到用到模板时,比方调用了模板函数 或者 定义了类模板的 对象的时候.编译器才产生特定类型的代码. 一般而言,在调用函数的时候,仅仅 ...
- C++解析(26):函数模板与类模板
0.目录 1.函数模板 1.1 函数模板与泛型编程 1.2 多参数函数模板 1.3 函数重载遇上函数模板 2.类模板 2.1 类模板 2.2 多参数类模板与特化 2.3 特化的深度分析 3.小结 1. ...
- c++类模板初探
#include <iostream> #include <string> using namespace std; // 你提交的代码将嵌入到这里 ; template &l ...
- C++STL - 类模板
类的成员变量,成员函数,成员类型,以及基类中如果包含参数化的类型,那么该类就是一个类模板 1.定义 template<typename 类型形参1, typename 类型形参2,...&g ...
- C++ 类模板的使用
从事C++挺久了,在前段时看书时,发现高手,都是在写模板无,泛型编程,顿感差距.自己连模板都没有写,于是就小小的研究了下模板的用法. 模板简而言之就是对某此对象的相同方法,或处理方式,进行归纳,总结, ...
- Xcode6中如何使用自定义的类模板
说到IOS类的模板,有些人感觉很陌生,但是只要有开发过IOS程序的人,其实都用过类的模板,只不过是用的系统自带的类的模板. 例如创建一个ClassTemplateVC继承于UIViewControll ...
随机推荐
- Nginx如何设置拒绝或允许指定ip访问
location ~ /druid/ { #deny 192.168.1.1; allow 192.168.1.1; deny all; proxy_pass http://127.0.0.1:808 ...
- javascript 事件委托,jq,js模拟事件
<!DOCTYPE> <html> <head> <title></title> <script src="Scripts/ ...
- ACM/ICPC 之 BFS范例(ZOJ2913-ZOJ1136(POJ1465))
通过几道经典BFS例题阐述BFS思路 ZOJ2913-Bus Pass 题意:找一个center区域,使得center到所有公交线路最短,有等距的center则输出id最小的. 题解:经典的BFS,由 ...
- Ditto - Windows剪贴板增强小工具,方便复制粘贴多条记录
在平时的工作中,好多地方需要重复的复制粘帖一些重复性的代码,频繁的Ctrl+C.Ctrl+V导致效率很低,而且时间长了会很烦,Windows自带的剪切板功能一次只能进行一条记录操作,单一的不行,与提高 ...
- 从API请求数据的方法(主要适用于tp5)
// 从api获取数据,$data是一个数组,默认为空,请求数据的方法可以通用,但是其它说明只适用于tp5 function postData($url,$data=''){ $ch = curl_i ...
- 【XLL API 函数】 xlGetInst
返回正在调用 DLL 的 Excel 实例的实例句柄. 原型 Excel4(xlGetInst, LPXLOPER pxRes, 0); /* returns low part only */ Exc ...
- 股票交易(洛谷U6084)
题目背景 kkk最近迷上了炒股. 题目描述 kkk炒了N天股,第i天的股价为a[i]元.kkk希望股票每天都上涨1元钱,但是操盘手lzn并不想让kkk赚很多钱导致他亏本,于是a[i]相对a[i-1]就 ...
- 安装Birt方法
安装BIRT 方法: 博客地址:http://www.mamicode.com/info-detail-850588.html 注意:在 Install new Software 中输入地址:http ...
- iosTest
NSString * url = @"http://192.168.0.11:8000/InterfaceApp/Login?UserName=15995858188&Passwor ...
- 有关struts2中用到 js 总结
1.js中取Struts2中的栈里的值 var current = "${currentPage}"; 2.js 如何提交执行提交url连接 ,以及 Struts中的url如何如何 ...