java实现循环队列
public class Queue {
int maxcount;
int count;
Object[ ] a;
int front;
int rear;
public Queue ( int size) {
a= new Object [ size] ;
front= 0 ;
rear= 0 ;
count= 0 ;
maxcount= size;
}
public boolean isEmpty ( ) {
return front== rear;
}
public boolean isfull ( ) {
return ( rear+ 1 ) % maxcount== front;
}
public void enQueue ( Object n) {
if ( isfull ( ) ) {
return ;
} else {
rear= ( rear+ 1 ) % maxcount;
this . a[ rear] = n;
count++ ;
}
}
public Object deQueue ( ) {
if ( isEmpty ( ) ) {
return null;
} else {
front= ( front+ 1 ) % maxcount;
count-- ;
return a[ front] ;
}
}
public int length ( ) {
return this . count;
}
public static void main ( String[ ] args) {
Queue queue= new Queue ( 4 ) ;
queue. enQueue ( 'a' ) ;
queue. enQueue ( 'b' ) ;
queue. enQueue ( 'c' ) ;
System. out. println ( "队列长度:" + queue. length ( ) ) ;
System. out. println ( "是否空:" + queue. isEmpty ( ) ) ;
System. out. println ( "是否满:" + queue. isfull ( ) ) ;
System. out. println ( queue. deQueue ( ) ) ;
}
}