继续浏览c++ primer 看到模板与泛型编程这章。就顺便把这几节的代码综合了下,对一个Queue队列模板的实现

贴一下代码(看完书。自己敲,忘记了哪再看下书)

#include <ostream>
using std::ostream; //声明Queue的模板类
template <class Type> class Queue;
//声明模板函数
template <class T> ostream& operator<<(ostream& , const Queue<T>&); //定义QueueItem的模板类
template <class Type> class QueueItem
{
//定义友元模板类和友元模板函数
friend class Queue<Type>;
friend ostream& operator<< <Type>(ostream& , const Queue<Type>&);
//QueueItem构造函数
QueueItem(const Type &t):item(t),next(0){}
QueueItem *next;
Type item;
}; //定义Queue模板类
template <class Type> class Queue
{
//定义友元模板函数
friend ostream& operator<< <Type>(ostream& , const Queue<Type>&);
public:
//构造函数
Queue():head(0),tail(0){}
template <class It> Queue(It beg, It end):head(0),tail(0){copy_elems(beg,end);}
template <class Iter> void assign(Iter , Iter);
//复制构造函数
Queue(const Queue &object){head(0);tail(0);copy_elems(object);}
//赋值操作符
Queue& operator=(const Queue&);
//析构函数
~Queue(){destroy();}
//push操作
void push(const Type&);
//pop操作
void pop();
//取队列头元素的操作front
Type& front();
//推断是否为空的操作
bool empty(){return head==0;}
private:
QueueItem *head;
QueueItem *tail;
void destroy();
void copy_elems(const Queue&);
template <class Iter> void copy_elems(Iter , Iter);
};
//重载输出操作符
template <class T> ostream& operator<<(ostream &os , const Queue<T> &object)
{
os << "<";
QueueItem *p;
for(p=object.head;p!=object.tail;p=p->next)
{
os <<p->item << " ";
}
os << ">" << endl;
}
//定义Queue模板类中的模板成员函数
template<class Type> template <class Iter> void Queue<Type>::assign(Iter beg, Iter end)
{
destroy();
copy_elems(beg , end);
}
//定义Queue模板类中的copy_elems模板成员函数
template <class Type> template <class Iter> void Queue<Type>::copy_elems(Iter beg, Iter end)
{
while(beg != end)
{
push(*beg);
++beg;
}
}
//Queue模板类中的copy_elems成员函数
template <class Type> void Queue<Type>::copy_elems(const Queue &object)
{
QueueItem<Type> *p;
for(p=object.head;p&&p!=object.tail;p=p->next)
{
push(p->item);
}
}
//赋值操作符
template <class Type> Queue<Type>& Queue<Type>::operator=(const Queue &rhs)
{
if(&rhs != this)
{
destroy();
copy_elems(rhs);
}
return *this;
}
/*
//第二种用链表直接实现赋值操作符
template <class Type> Queue<Type>& Queue<Type>::operator=(const Queue &rhs)
{
QueueItem<Type> *p = rhs.head;
while(p)
{
QueueItem<Type> *q = new QueueItem<Type>(p->item);
if(p == rhs.head)
{
head = tail = q;
}else{
tail->next = q;
tail = q;
p=p->next;
}
}
return *this;
}
*/
//push操作
template <class Type> void Queue<Type>::push(const Type &value)
{
QueueItem<Type> *p = new QueueItem<Type>(value);
if(this->empty())
{
head = p;
tail = p;
}else{
tail->next = p;
tail = p;
}
}
//pop操作
template <class Type> void Queue<Type>::pop()
{
QueueItem<Type> *p;
p=head;
head = head->next;
delete p;
}
//front操作
template <class Type> Type& Queue<Type>::front()
{
return head->item;
}
//destory操作
template <class Type> void Queue<Type>::destroy()
{
while(!empty())
{
pop();
}
}

最新文章

  1. 生物信息大数据&amp;数据库(NCBI、EBI、UCSC、TCGA)
  2. C++ 排序函数 sort(),qsort()的含义与用法 ,字符串string 的逆序排序等
  3. 用PHP实现一个高效安全的ftp服务器(一)
  4. 【转】ArrayList遍历的同时删除----不错
  5. angular 选中切换面板
  6. ASP.NET 初识Cookie
  7. 洛谷U19464 山村游历(Wander)(LCT,Splay)
  8. 从a文件判断是否删除b文件中的行(sed示例)
  9. A1024. Palindromic Number
  10. PCB封装步骤教程
  11. 使用 JdbcTemplate 查询数据时报错:列名无效(已解决)
  12. 在js中保存数据
  13. SDOI2018一轮NOI培训 题目整理
  14. ScreenCapturePro2 for Joomla_3.4.7-tinymce4x
  15. 地精排序Gnome Sort
  16. 互联网公司面试必问的mysql题目(下)
  17. 1136 A Delayed Palindrome (20 分)
  18. 「2017 山东三轮集训 Day7」Easy
  19. python标准库:Configparser模块
  20. NS3网络仿真(7): Wifi节点

热门文章

  1. Maven运行报错:-Dmaven.multiModuleProjectDirectory system propery is not set.
  2. HDU 3506 DP 四边形不等式优化 Monkey Party
  3. luogu1131 [ZJOI2007]时态同步
  4. How To Configure VMware fencing using fence
  5. C#Windows服务安装
  6. SQL server 数据库备份至服务器本地磁盘和其他服务器磁盘
  7. PHP优化之批量操作MySQL
  8. TOJ 4008 The Leaf Eaters
  9. C#中类的实例是不能 获取到类中的静态方法和静态变量(Static)的,及原因
  10. BZOJ 4810 [Ynoi2017]由乃的玉米田 ——Bitset 莫队算法