JAVA 用数组实现环型队列

 
/*
 * Author: zhangjian268
 * Created: 20074る11ら と 04:59:23
 * Modified: 20074る11ら と 04:59:23
 * 數組實現環形隊列  
 
*/


import  java.util. * ;

public   class  ArrayCircularQueue  {
    
private int front = 0, rear = 0;
    
private Object[] queue;
    
    
public ArrayCircularQueue(int maxElements) {
        queue 
= new Object[maxElements];
    }

    
    
public void insert(Object o) {
        
int temp = rear;
        rear 
= (rear + 1% queue.length;
        
if (front == rear) {
            rear 
= temp;
            
//throw new FullQueueException();
        }

        
        queue[rear] 
= o;
    }

    
    
public boolean isEmpty() {
        
return front == rear;
    }

    
    
public boolean isFull() {
        
return ((rear + 1% queue.length) == front;
    }

    
    
public Object remove() {
        
if (front == rear)
            
return 0;
            
//throw new EmptyQueueException(); 
        
        front 
= (front + 1% queue.length;
        
return queue[front];
    }

    
    
public static void main(String args[]) {
        ArrayCircularQueue Q 
= new ArrayCircularQueue(16);
        Q.insert(
"aa");
        Q.insert(
"bb");
        Q.insert(
"cc");
        System.out.println(Q.remove());
        Q.insert(
"dd");
        System.out.println(Q.remove());
        System.out.println(Q.remove());
        System.out.println(Q.remove());
        System.out.println(Q.remove());
    }

}
 

你可能感兴趣的:(Java,算法)