# include
使用名称空间性病;
template<类T>
结构节点{
Node
T, _data;
节点(T数据)
: _next (NULL)
, _data(数据)
{}
};
template<类T>类队列
{公共:
队列()
: _head (NULL)
, _tail (NULL)
{}
队列(const queue
: _head (NULL)
, _tail (NULL)
{
,Node
,而(坏蛋)
,{
,这→推(cur→_data);
,坏蛋=cur→_next;
,}}
queue
{
如果(这!=,q)
{
删除[]问;
,Node
,而(坏蛋)
,{
,这→推(cur→_data);
,坏蛋=cur→_next;
,}
,返回*;
}
}
~队列()
{
Node
,如果(坏蛋)
{
Node
cur=cur→_next;
删除del;
del=零;
}
}
空白推(const T&x)
{
Node
如果(_head==NULL)
{
_head=newNode;
_tail=_head;
}其他
{
_tail→_next=newNode;
_tail=newNode;
}
}
空白pop ()
{
如果(_head !=NULL)
{
,,Node
,_head=_head→_next;
,,删除del;
}
}
//打印队列元素
空白打印()
{
Node
如果(_head==NULL)
{
,返回;
}
其他{
,(坏蛋)
{
cout<& lt; cur→_data<& lt;””;
cur=cur→_next;
}
cout<& lt;“结束”& lt; & lt; endl;
}
}
//
{
如果(空)
{
,返回_head→_data;
}
}
T&回()
{
如果(空)
{
,返回_tail→_data;
}
}
bool空()
{
返回(_head==NULL);
}
保护:
Node
Node
};
。cpp
空白TestQueue ()
{
queue
q1.push (1),
q1.push (2);
q1.push (3);
q1.push (4),
queue
queue
q1.print ();
q2.print ();
q3.print ();
}
int主要()
{
TestQueue ();
系统(“暂停”);
返回0;
}