Java--循环队列

队列的特点:先进先出。
1、循环队列完整代码:

package ShunXuZhan;
/*
 * 循环队列
 * 先进先出
 * 浪费一个数据单元  来判断是否为满
 *
 * */
class QueueLink{
    int elem[];//创建一个数组变量
    int front;//队头
    int rear;//队尾

    int usedSize=0;//当前队列有效长度
    int allSize=8;//队列的总长度为8

    public QueueLink(){//构造函数
        this(10);
        }

    public QueueLink(int size){//构造函数
        this.elem=new int[size];
        this.front=0;//头和尾都为零
        this.rear=0;//头和为相等为零
    }

    //判断队列是否为满
    public boolean isFull(){
        if((this.rear+1)%(this.allSize)==this.front){
            return true;
        }
        return false;
    }

    //入队,先判断队是否已满
    public void push(int val){
        if(isFull()){
            return;
        }
        this.elem[this.rear]=val;
        this.rear=(this.rear+1)%this.allSize;
        this.usedSize++;
    }

    //判断是否为空
    public boolean isEmpty(){
        if(this.front==this.rear){
            return true;
        }
        return false;
        //return this.front==this.rear;
    }

    //出队,判断队列是否为空
    public void pop(){
        if(isEmpty()){
            return;
        }
        this.elem[this.front]=-1;
        this.front=(this.front+1)%this.allSize;
        this.usedSize--;
    }

    //得到队头的元素
    public int getTop(){
        if(isEmpty()){//判断队列是否为空
            return -1;
        }
        return this.elem[this.front];
    }

    //打印
    public void show(){
        for(int i=this.front;i<rear;i=(i+1)%this.allSize){
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }

}


public class XunHuanDuiLie {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        QueueLink q1=new QueueLink();
        for(int i=0;i<8;i++){
            q1.push(i);
        }
        q1.show();
    }

}

输出:
这里写图片描述
2、创建一个循环队列:

int elem[];//创建一个数组变量
    int front;//队头
    int rear;//队尾

    int usedSize=0;//当前队列有效长度
    int allSize=8;//队列的总长度为8

    public QueueLink(){//构造函数
        this(10);
        }

    public QueueLink(int size){//构造函数
        this.elem=new int[size];
        this.front=0;//头和尾都为零
        this.rear=0;//头和为相等为零
    }

这里写图片描述
3、元素入队

//判断队列是否为满
    public boolean isFull(){
        if((this.rear+1)%(this.allSize)==this.front){
            return true;
        }
        return false;
    }

    //入队,先判断队是否已满
    public void push(int val){
        if(isFull()){
            return;
        }
        this.elem[this.rear]=val;
        this.rear=(this.rear+1)%this.allSize;
        this.usedSize++;
    }

视图分析:
这里写图片描述
4、元素出队:

//判断是否为空
    public boolean isEmpty(){
        if(this.front==this.rear){
            return true;
        }
        return false;
        //return this.front==this.rear;
    }

    //出队,判断队列是否为空
    public void pop(){
        if(isEmpty()){
            return;
        }
        this.elem[this.front]=-1;
        this.front=(this.front+1)%this.allSize;
        this.usedSize--;
    }

这里写图片描述
6、求栈顶元素:

//得到队头的元素
    public int getTop(){
        if(isEmpty()){//判断队列是否为空
            return -1;
        }
        return this.elem[this.front];
    }

猜你喜欢

转载自blog.csdn.net/xyxy66/article/details/80253526