首页 > 代码库 > STL 队列模板实现

STL 队列模板实现

  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;
}