Java隊列的操作

package yanhaochen;

public class hello{
    public static void main(String[] args) {
    }
}
class ArrayQueue{
    private int maxSize; //表示數組的最大容量
    private int front;  //隊列頭
    private int rear;//隊列尾
    private int[]arr; //該數據用於存放數據,模擬隊列
    public ArrayQueue(int arrmaxsize){
        maxSize=arrmaxsize;
        arr = new int[maxSize];
        front = -1;rear = -1;
    }
public boolean isFull(){
    return rear == maxSize-1;
}
public boolean isEmpyt(){
    return rear == front;
}
public void addQueue(int n){
        if(isFull()){
            System.out.println("隊列已滿,無法插入");
            return;
        }
        rear++;//rear後移
      arr[rear]=n;
    }
    //出隊列
    public int getQueue(){
        if(isEmpyt()){
            throw  new RuntimeException("隊列爲空");
        }
        front++;
        return arr[front];
    }
    public void show(){
        if(isEmpyt()){
            System.out.println("隊列爲空");
            return;
        }
        for(int i=0;i<arr.length;i++){
            System.out.printf("arr[%d]=%d\n",arr[i],i);
        }
    }
    public int headQueue(){
        if(isEmpyt()){
            throw new RuntimeException("隊列爲空");
        }
        return arr[front+1];
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章