队列--基于简单循环数组实现(Java)

基于简单循环数组实现队列

class ArrayQueue{
    private int font;
    private int rear;
    private int capacity;
    int[] array;
    public ArrayQueue(int size){
        capacity=size;
        font=-1;
        rear=-1;
        array=new int[size];
    }

    public static ArrayQueue createQueue(int size){
        return new ArrayQueue(size);
    }

    public boolean isEmpty(){
        return (font==-1);
    }

    public boolean isFull(){
        return ((rear+1)%capacity==(font%capacity));
    }

    public int getQueuesize(){
        return ((capacity+rear-font+1)%capacity);
    }

    public void enQueue(int date){
        if(isFull()){
            System.out.println("Queue Overflow");
        }else {
            rear=(rear+1)%capacity;
            array[rear]=date;
            if(font==-1){
                font=rear;
            }
        }
    }

    public int deQueue(){
        int date=0;
        if(isEmpty()){
            System.out.println("Queue Empty");
        }else {
            date=array[font];
            if(font==rear){
                font=rear-1;
            }else {
                font=(font+1)%capacity;
            }
        }
        return date;
    }

}

public class Main {

    public static void main(String[] args) {
        // write your code here
        ArrayQueue arrayQueue=new ArrayQueue(5);
        arrayQueue.enQueue(1);
        arrayQueue.enQueue(2);
        arrayQueue.enQueue(3);
        arrayQueue.enQueue(4);
        System.out.println(arrayQueue.deQueue());
        System.out.println(arrayQueue.deQueue());
        System.out.println(arrayQueue.deQueue());
        System.out.println(arrayQueue.deQueue());
    }
}


你可能感兴趣的:(数据结构,Java)