You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# ifndef SEQQUEUE_H
# define SEQQUEUE_H
# include <assert.h>
# include <iostream>
using namespace std ;
# include "Queue.h"
template < class T >
class SeqQueue : public Queue < T >
{
public :
SeqQueue ( int sz = 10 ) ;
~ SeqQueue ( )
{
delete [ ] elements ;
}
bool EnQueue ( const T & x ) ; //若队列不满, 则将x进队列, 否则队列溢出处理
bool DeQueue ( T & x ) ; //若队列不空, 则退出队头元素由x返回
bool getFront ( T & x ) ; //若队列不空,则返回对头元素的值
void makeEmpty ( ) //置空操作
{
front = rear = 0 ;
}
bool IsEmpty ( ) const //判断队列空否, 空返回true
{
return ( front = = rear ) ? true : false ;
}
bool IsFull ( ) const //判断队列满否, 满返回true
{
return ( ( rear + 1 ) % maxSize = = front ) ? true : false ; //
}
int getSize ( ) const
{
return ( rear - front + maxSize ) % maxSize ;
}
friend ostream & operator < < ( ostream & os , SeqQueue < T > & Q ) ; //重载输出操作符
T getData ( int i ) const
{
return elements [ i ] ;
}
protected :
T * elements ;
int rear , front ;
int maxSize ;
} ;
template < class T >
SeqQueue < T > : : SeqQueue ( int sz /* = 10 */ ) : front ( 0 ) , rear ( 0 ) , maxSize ( sz )
{
elements = new T [ maxSize ] ;
assert ( elements ! = NULL ) ;
}
template < class T >
bool SeqQueue < T > : : EnQueue ( const T & x )
{
if ( IsFull ( ) = = true )
return false ;
elements [ rear ] = x ;
rear = ( rear + 1 ) % maxSize ;
return true ;
}
template < class T >
bool SeqQueue < T > : : DeQueue ( T & x )
{
if ( IsEmpty ( ) = = true )
return false ;
x = elements [ front ] ;
front = ( front + 1 ) % maxSize ;
return true ;
}
template < class T >
bool SeqQueue < T > : : getFront ( T & x )
{
if ( IsEmpty ( ) = = true )
return false ;
x = elements [ front ] ;
return true ;
}
template < class T >
ostream & operator < < ( ostream & os , SeqQueue < T > & Q )
{
os < < " front = " < < Q . front < < " ,rear = " < < Q . rear < < endl ;
for ( int i = Q . front ; i ! = Q . rear ; i = ( i + 1 ) % Q . maxSize )
{
os < < i < < " : " < < Q . elements [ i ] < < endl ;
}
}
# endif