Queue模板类

#include
using namespace std;

/*友元类的前向声明*/
template
class Queue;

/*友元函数的前向声明*/
template
ostream & operator<<(ostream &os,const Queue &q);

/*结点,包括数据,和指针*/
template
class QueueItem
{
	friend class Queue;
	friend ostream& operator<< (ostream &os,const Queue&q);
	QueueItem(const Type &t):item(t),next(0){}
	Type item;
	QueueItem *next;
};

/*队列类*/
template
class Queue{
	friend ostream& operator<< (ostream &os,const Queue&q);
public:
	Queue():head(0),tail(0){
		cout<<"Queue 构造函数"<item;
	}
	const Type &front()const
	{
		return head->item;
	}
	int size()
	{
		int size=0;
		QueueItem *p=head;
		while(p)
		{
			p=p->next;
			size++;
		}
		return size;
	}
private:
	QueueItem *head;
	QueueItem *tail;
	void destroy();
	void copy_elems(const Queue&);
};

/*删除每一项*/
template
void Queue::destroy()
{
	while(!empty())
		pop();
}

template
void Queue::pop()
{
	QueueItem *p=head;
	head=head->next;
	delete p;
}

template
void Queue::push(const Type &val)
{
	QueueItem *p=new QueueItem(val);
	if(empty())
		head=tail=p;
	else
	{
		tail->next=p;
		tail=p;
	}
}

/*将参数队列的每项压入队列中*/
template
void Queue::copy_elems(const Queue &orig)
{
	for(QueueItem *p=orig.head;p;p=p->next)
		push(p->item);
}

template
Queue & Queue::operator =(const Queue &rhs)
{
	destroy();
	copy_elems(rhs);
	cout<<"Queue 赋值操作符"<
ostream & operator<<(ostream &os,const Queue &q)
{
	os<<"< ";
	QueueItem *p;
	for(p=q.head;p;p=p->next)
	{
		os<item<<" ";
	}
	os<<">";
	return os;
}
int main(){
	Queue q;
	q.push(1);
	q.push(2);
	q.push(3);
	q.push(4);
	
	Queue p;
	p=q;
	Queue r(p);

	cout<


#include
#include
using namespace std;

template
class Queue;

template
ostream & operator<<(ostream &os,const Queue & val);

template
class Queue{
	friend ostream &operator<<  (ostream &os,const Queue &val);
public:
	Queue()
	{
		if(debug==1)
			cout<<"构造函数"<l=rhs.l;
		}
		if(debug==1)
			cout<<"赋值函数"<
	Queue(It beg,It end);

	template
	void Assign(It beg,It end);
private:
	list l;
	const static int debug=0;
};

/*成员模板*/
template
template
void Queue::Assign(It beg,It end)
{
	l.assign(beg,end);
}

/*成员模板*/
template
template
Queue::Queue(It beg, It end)
{
	l.assign(beg,end);
}

template
ostream &operator<<(ostream &os,const Queue & q)
{
	os<<"<";
	typename list::const_iterator it=q.l.begin();
	while(it!=q.l.end())
		os<<*it++<<" ";
	os<<">";
	return os;
}

int main(){
	Queue q;
	q.push(1);
	q.push(2);
	q.push(3);
	q.push(4);
	cout< p(q);
	Queue r;
	r=q;
	cout<(a,a+4);
	cout<


你可能感兴趣的:(C++)