版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/u010016150/article/details/32715801

  C++ Prime确实有点难啊!看了好久都没弄清楚,一点点慢慢来。

#include <iostream>
#include <string>
#include <cstdio> template <class Type> class Queue; //function template declaration must precede friend declaration in QueueItem
template <class T>
std::ostream& operator<<(std::ostream&,const Queue<T>&); template <class Type> class QueueItem{
friend class Queue<Type>;
// needs access to item and next
friend std::ostream&
operator<< <Type> (std::ostream&,const Queue<Type>&);
// private class: no public section
QueueItem(const Type &t):item(t),next(0){}
Type item; // value stored in this element
QueueItem *next; // pointer to next element in the Queue
}; template <class Type> class Queue{
// needs access to head
friend std::ostream&
operator << <Type> (std::ostream&,const Queue<Type>&);
public:
// empty Queue
Queue():head(0),tail(0){}
// construct a Queue from a pair of iterators on some sequence
template <class It>
Queue(It beg,It end):
head(0),tail(0){ copy_elems(beg,end); }
// copy control to manage pointers to QueueItems in the Queue
Queue(const Queue &Q)
:head(0),tail(0){ copy_elems(Q); }
Queue& operator=(const Queue&); // left as exercise for the reader
~Queue() { destroy(); }
// replace current Queue by contents delimited by a pair of iterators
template <class Iter> void assign(Iter,Iter);
// return element from head of Queue
// unchecked operation:front on an empty Queue is undefined
Type& front() {return head->item; }
const Type &front() const {return head->item;}
void push(const Type &);
void pop();
bool empty()const{ //true if no elements in the Queue
return head == 0;
}
private:
QueueItem<Type> *head;
QueueItem<Type> *tail;
void destroy();
void copy_elems(const Queue&);
// version of copyto be used by assign to copy elements from iterator range
template <class Iter> void copy_elems(Iter,Iter);
}; // push 函数
// push 成员将新项放在队列末尾 template <class Type> void Queue<Type>::push(const Type &val)
{
// allocate a new QueueItem object
QueueItem<Type> *pt = new QueueItem<Type>(val);
// put item onto existing(眼下) queue
if(empty())
head = tail = pt; // the queue now has only one element
else {
tail->next = pt; // add new element to end of the queue
tail = pt;
}
} //copy_element 函数
template <class Type>
void Queue<Type>::copy_elems(const Queue &orig)
{
// copy elements from orig into this Queue
// loop stops when to pt == 0, which happens when we reach orig.tail
for (QueueItem<Type> *pt = orig.head; pt; pt = pt->next)
push(pt->item); // copy element
} // destroy 函数
template <class Type> void Queue<Type>::destroy()
{
while (!empty())
pop();
} //pop函数
template <class Type> void Queue<Type>::pop()
{
// pop is unchecked: Popping off an empty Queue is undefined
QueueItem<Type>* p = head;
head = head->next;
delete p;
} int main()
{
int n,val;
Queue<int> IntQ;
/*Queue<double> DouQ;
Queue<string> StrQ;*/
printf("Please enter you want total number: ");
std::cin>>n;
while(n){
std::cin>>val;
IntQ.push(val);
n--;
}
while(!IntQ.empty()){
std::cout<<IntQ.front()<<std::endl;
IntQ.pop();
}
return 0;
}

最新文章

  1. C++ virtual虚函数
  2. Javascript高级程序设计——函数内部属性与函数属性
  3. Memcached(三)Memcached配置参数初解
  4. android设备连接不上电脑的解决方法
  5. 关于iOS8上本地通知接收不到的问题
  6. 指针运算中的运算符:&amp;和*
  7. [daily] 不让NetworkManger自动接管网络设备
  8. HashMap 的put方法
  9. 解决使用jedis连接是报DENIED Redis is running in protected mode错误
  10. gcc centos 新版本的安装方法
  11. 浅谈css3长度单位rem,以及移动端布局技巧
  12. [UI] 05 - Bootstrap: built-in components
  13. 20170907VS中EF模型文件.edmx文件上下级关系丢失问题
  14. LeetCode——7. Reverse Integer
  15. 如何删除Eclipse里某个工作空间?
  16. Android里面安装windows系统
  17. Java 调用Web service 加入认证头(soapenv:Header)
  18. CSS实现微信对话框
  19. (转) 在PHP中使用全局变量
  20. POJ 2891- Strange Way to Express Integers CRT 除数非互质

热门文章

  1. java 重新学习 (六)
  2. Linux NIO 系列(04-2) poll
  3. Fedora LVM磁盘大小调整
  4. mybatis 教程(mybatis in action)
  5. bzoj4544 椭圆上的整点
  6. spring事务实现的几种方式
  7. ionic node-sass安装或编译失败:MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”
  8. 导入C文件Xcode出现Could not build module &#39;Foundation&#39;错误
  9. stty - 改变并打印终端行设置
  10. html浮动小问题