#include<iostream>
using namespace std;
/*友元类的前向声明*/
template<typename Type>
class Queue;
/*友元函数的前向声明*/
template<typename Type>
ostream & operator<<(ostream &os,const Queue<Type> &q);
/*结点,包括数据,和指针*/
template<typename Type>
class QueueItem
{
friend class Queue<Type>;
friend ostream& operator<< <Type >(ostream &os,const Queue<Type>&q);
QueueItem(const Type &t):item(t),next(0){}
Type item;
QueueItem *next;
};
/*队列类*/
template<typename Type>
class Queue{
friend ostream& operator<< <Type >(ostream &os,const Queue<Type>&q);
public:
Queue():head(0),tail(0){
cout<<"Queue 构造函数"<<endl;
}
Queue(const Queue& rhs):head(0),tail(0)
{
//*this=rhs;
copy_elems(rhs);
cout<<"Queue 拷贝构造函数"<<endl;
}
void pop();
void push(const Type &);
Queue & operator=(const Queue &rhs);
~Queue(){
destroy();
cout<<"Queue 析构函数"<<endl;
}
bool empty()const
{
return head==0;
}
Type &front()
{
return head->item;
}
const Type &front()const
{
return head->item;
}
int size()
{
int size=0;
QueueItem<Type> *p=head;
while(p)
{
p=p->next;
size++;
}
return size;
}
private:
QueueItem<Type> *head;
QueueItem<Type> *tail;
void destroy();
void copy_elems(const Queue&);
};
/*删除每一项*/
template<typename Type>
void Queue<Type>::destroy()
{
while(!empty())
pop();
}
template<typename Type>
void Queue<Type>::pop()
{
QueueItem<Type> *p=head;
head=head->next;
delete p;
}
template<typename Type>
void Queue<Type>::push(const Type &val)
{
QueueItem<Type> *p=new QueueItem<Type>(val);
if(empty())
head=tail=p;
else
{
tail->next=p;
tail=p;
}
}
/*将参数队列的每项压入队列中*/
template<typename Type>
void Queue<Type>::copy_elems(const Queue<Type> &orig)
{
for(QueueItem<Type> *p=orig.head;p;p=p->next)
push(p->item);
}
template<typename Type>
Queue<Type> & Queue<Type>::operator =(const Queue &rhs)
{
destroy();
copy_elems(rhs);
cout<<"Queue 赋值操作符"<<endl;
return *this;
}
template<typename Type>
ostream & operator<<(ostream &os,const Queue<Type> &q)
{
os<<"< ";
QueueItem<Type> *p;
for(p=q.head;p;p=p->next)
{
os<<p->item<<" ";
}
os<<">";
return os;
}
int main(){
Queue<int> q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
Queue<int> p;
p=q;
Queue<int> r(p);
cout<<p<<"\t"<<p.size()<<endl;
cout<<r<<"\t"<<r.size()<<endl;
cout<<q<<"\t"<<q.size()<<endl;
while(!q.empty())
{
cout<<q.front()<<endl;
q.pop();
}
cout<<q.size()<<endl;
return 0;
}
#include<iostream>
#include<list>
using namespace std;
template<typename Type>
class Queue;
template<typename Type>
ostream & operator<<(ostream &os,const Queue<Type> & val);
template<typename Type>
class Queue{
friend ostream &operator<< <Type> (ostream &os,const Queue<Type> &val);
public:
Queue()
{
if(debug==1)
cout<<"构造函数"<<endl;
}
Queue(const Queue &rhs)
{
*this=rhs;
if(debug==1)
cout<<"复制构造函数"<<endl;
}
Queue &operator=(const Queue &rhs)
{
if(this!=&rhs)
{
this->l=rhs.l;
}
if(debug==1)
cout<<"赋值函数"<<endl;
return *this;
}
void push(const Type & val){l.push_back(val);}
Type &front(){return l.front();}
void pop(){l.pop_front();}
bool empty()
{
return l.empty();
}
virtual ~Queue(){
if(debug==1)
cout<<"析构函数"<<endl;
}
template<typename It>
Queue(It beg,It end);
template<typename It>
void Assign(It beg,It end);
private:
list<Type> l;
const static int debug=0;
};
/*成员模板*/
template<typename Type>
template<typename It>
void Queue<Type>::Assign(It beg,It end)
{
l.assign(beg,end);
}
/*成员模板*/
template<typename Type>
template<typename It>
Queue<Type>::Queue(It beg, It end)
{
l.assign(beg,end);
}
template<typename Type>
ostream &operator<<(ostream &os,const Queue<Type> & q)
{
os<<"<";
typename list<Type>::const_iterator it=q.l.begin();
while(it!=q.l.end())
os<<*it++<<" ";
os<<">";
return os;
}
int main(){
Queue<int> q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
cout<<q.front()<<endl;
Queue<int> p(q);
Queue<int> r;
r=q;
cout<<q<<endl;
cout<<p<<endl;
cout<<r<<endl;
while(!r.empty())
{
cout<<r.front()<<endl;
r.pop();
}
int a[4]={2,3,4,5};
p=Queue<int>(a,a+4);
cout<<p<<endl;
r.Assign(a,a+3);
cout<<r<<endl;
return 0;
}