c++模板实现队列


# include

使用名称空间性病;

template<类T>

结构节点{

Node * _next;

T, _data;

节点(T数据)

: _next (NULL)

, _data(数据)

{}

};

template<类T>类队列

{公共:

队列()

: _head (NULL)

, _tail (NULL)


{}

队列(const queue和问)

: _head (NULL)

, _tail (NULL)

{

,Node * cur=q._head;

,而(坏蛋)

,{

,这→推(cur→_data);

,坏蛋=cur→_next;

,}}

queue及运算符=(const queue和问)

{

如果(这!=,q)

{

删除[]问;

,Node * cur=q._head;

,而(坏蛋)

,{

,这→推(cur→_data);

,坏蛋=cur→_next;

,}

,返回*;

}

}

~队列()

{

Node * cur=_head;

,如果(坏蛋)

{

Node *▽=坏蛋;

cur=cur→_next;

删除del;

del=零;

}

}

空白推(const T&x)

{

Node * newNode=新Node (x);

如果(_head==NULL)

{

_head=newNode;

_tail=_head;

}其他

{

_tail→_next=newNode;

_tail=newNode;

}

}

空白pop ()

{

如果(_head !=NULL)

{

,,Node *▽=_head;

,_head=_head→_next;

,,删除del;

}

}

//打印队列元素

空白打印()

{

Node * cur=_head;

如果(_head==NULL)

{

,返回;

}

其他{

,(坏蛋)

{

cout<& lt; cur→_data<& lt;””;

cur=cur→_next;

}

cout<& lt;“结束”& lt; & lt; endl;

}

}

//

{

如果(空)

{

,返回_head→_data;

}

}

T&回()

{

如果(空)

{

,返回_tail→_data;

}

}

bool空()

{

返回(_head==NULL);

}

保护:

Node * _head;

Node * _tail;


};


。cpp

空白TestQueue ()

{

queueq1;

q1.push (1),

q1.push (2);

q1.push (3);

q1.push (4),

queueq2 (q1);

queue第三季度=q2;

q1.print ();

q2.print ();

q3.print ();

}

int主要()

{

TestQueue ();

系统(“暂停”);

返回0;

}

c++模板实现队列