#ifndef _QUEUE_H
#define _QUEUE_H
#pragma once
#include < iostream >
using namespace std ;
template< class T >
class Queue
{
public:
Queue( void ) : front( NULL ) , back( NULL ) , size( 0 ) { } ;//构造函数
Queue( const Queue& ) ;//拷贝构造函数
~Queue( ) ;//析构函数
Queue& operator = ( const Queue& ) ;//重载赋值运算符
int Size( void ) const ;//元素数目
bool IsEmpty( void ) const ;//队列是否为空
T& Front( void ) ;//队头元素
T& Back( void ) ;//队尾元素
void PushBack( const T& ) ;//向队尾插入一个元素
void Pop( void ) ;//移除队头元素
void PopBack( void ) ;//移除队尾元素
void Clear( void ) ;//清除队列
protected:
class Node
{
public:
Node( const Node* pt )
: data( pt->data ) , next( pt->next ) , prior( pt->prior ) { }//拷贝构造函数
Node( const T& obj , Node* Next = NULL , Node* Prior = NULL )
: data( obj ) , next( Next ) , prior( Prior ) { }//重载拷贝构造函数
T data ;//节点元素
Node* prior ;//前项指针
Node* next ;//后项指针
} ;
private:
int size ;//队列中元素数目
Node* front ;//队头指针
Node* back ;//队尾指针
} ;
template< class T >
Queue< T >::Queue( const Queue<T> & Q )
{
if( !( size =Q.size ) ) return ;
else
{
Node* pt = NULL ;
for( Node* __ = Q.back ; __ ; __ = __->prior )
if( __ == Q.back ) pt = back = new Node( __ ) ;
else if( __ == Q.front )
{
Node* ___ = pt ;
pt = pt->prior = front = new Node( __ ) ;
pt->next = ___ ;
}
else
{
Node* _ = pt ;
pt = pt->prior = new Node( __ ) ;
pt->next = _ ;
}
}
}
template< class T >
Queue< T >::~Queue( void )
{
while( back )
{
Node* p = back ;
back = back->prior ;
delete p ;
p = NULL ;
--size ;
}
}
template< class T >
Queue< T >& Queue< T >::operator = (const Queue<T> & Q )
{
if( this == &Q ) return *this ;//same queue
else
{
Node* pt = NULL ;
while( back )
{
pt = back ;
back = back->prior ;
delete pt ;
pt = NULL ;
}
if( !( size = Q.size ) ) { front = back = NULL ; return *this ; }
else
{
for( Node* __ = Q.back ; __ ; __ = __->prior )
if( __ == Q.back ) pt = back = new Node( __ ) ;
else if( __ == Q.front )
{
Node* ___ = pt ;
pt = pt->prior = front = new Node( __ ) ;
pt->next = ___ ;
}
else
{
Node* _ = pt ;
pt = pt->prior = new Node( __ ) ;
pt->next = _ ;
}
}
}
return *this ;
}
template< class T >
int Queue< T >::Size( void ) const
{ return size ; }
template< class T >
bool Queue< T >::IsEmpty( void ) const
{ return size == 0 ; }
template< class T >
T& Queue< T >::Front( void )
{ return front->data ; }
template< class T >
T& Queue< T >::Back( void )
{ return back->_ ; }
template< class T >
void Queue< T >::PushBack( const T & obj )
{
if( size == 0 ) front = back = new Node( obj ) ;
else
{
Node* __ = back ;
if( back->next )
{
back->next->data = obj ;
back = back->next ;
back->prior = __ ;
}
else
{
back= back->next = new Node( obj ) ;
back->prior = __ ;
}
}
++size ;
}
template< class T >
void Queue< T >::Pop( void )
{
if( front == back )
{
delete [ ]front ;
front = back = NULL ;
}
else
{
Node* p = front ;
front = front->next ;
delete p ;
front->prior = NULL ;
}
--size ;
}
template< class T >
void Queue< T >::PopBack( void )
{
if( front == back )
{
delete [ ]back ;
front = back = NULL ;
}
else
{
Node* p = back ;
back = back->prior ;
delete p ;
back->next = NULL ;
}
--size ;
}
template< class T >
void Queue< T >::Clear( void )
{
Node* p = NULL ;
while( back )
{
p = new Node( back ) ;
back = back->prior ;
delete p;
p = NULL ;
--size ;
}
}
#endif