• STL 队列模板实现


    版权声明:本文为博主原创文章。未经博主同意不得转载。 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;
    }

  • 相关阅读:
    通俗易懂----欧几里得算法
    安卓贴图源码--->记录旋转后位置..类似in/百度魔图
    wait、notify、sleep、interrupt对比分析
    安卓内存泄漏及检测内存泄漏
    美团多渠道打包
    高版本api在低版本中的兼容
    常用工具
    双守护进程(不死service)-5.0系统以下
    安卓log4k问题解决
    10、面向对象以及winform的简单运用(isMdicontainer的设置、timer控件进行倒计时的制作)
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10851449.html
  • Copyright © 2020-2023  润新知