顺序表实现循环队列

public class MyQueueCircle {
    public int[] elem;
    public int frout;
    public int rear;

    public MyQueueCircle(){
        elem = new int[10];
        this.frout = 0;
        this.rear = 0;
    }

    public boolean enQueue(int value){
        if((this.rear+1)%this.elem.length == frout){
            return false;
        }
        this.elem[rear] = value;
        this.rear = (this.rear+1)%10;
        return true;
    }

    public boolean isEmpty(){
        if(this.frout == this.rear){
            return true;
        }
        else {
            return false;
        }
    }
    public boolean isFull() {
        if((this.rear+1)%10 == this.frout){
            return true;
        }
        return false;
    }

    public boolean deQueue(){
        if(isEmpty()){
            return false;
        }
        this.frout = (this.frout+1)%this.elem.length;
        return true;
    }

    public int Front(){
        if(isEmpty()){
            return-1;
        }
        return elem[frout];
    }

    public int Rear(){
        if(isEmpty()){
            return -1;
        }
        int index = 0;
        if(rear == 0){
            index = this.elem.length - 1;
        }
        else {
            index = rear-1;
        }
        return elem[index];
    }

}
发布了28 篇原创文章 · 获赞 3 · 访问量 734

猜你喜欢

转载自blog.csdn.net/XDtobaby/article/details/103056621