0
点赞
收藏
分享

微信扫一扫

学习笔记-算法-2-队列

sullay 2022-05-05 阅读 54
算法java

队列

  • 先进后出
    • 数组
    • 链表

数组简易队列

// 数组模拟队列
public class ArrayQueue{
    private int maxSize;    // 数组容量
    private int front;      // 队列头
    private int rear;       // 队列尾
    private int[] arr;      // 模拟队列
    
    // 创建队列
    public ArrayQueue(int arrMaxSize){
        this.maxSize = arrMaxSize;
        this.arr = new int[this.maxSize];
        front = -1;         // 队列头前一位
        rear = -1;          // 队列尾
    }
    
    // 判断队列是否满
    public boolean isFull(){
        return rear == maxSize -1;
    }
    
    // 判断队列是否为空
    public boolean isEmpty(){
        return rear == front;
    }
    
    // 添加数据
    pulic void addQueue(int n){
        // 判断队列是否满
        if(isFull()){
            System.out.println("队列满,不能加入");
            return;
        }
        rear++;             // 后移
        arr[rear] = n;
    }
    
    // 取队列
    public int getQueue() {
        if(isEmpty()){
            throw new RuntimeException("队列空,不能取");
        }
        front++;            // 后移
        return arr[front];
    }
    
    // 显示所有数据
    public void showQueue(){
        if(isEmpty()){
            System.out.println("队列空的");
            return;
        }
        for(int i=0;i<arr.length;i++){
            System.out.printf("arr[%d]=%d",i,arr[i]);
        }
    }
    
    // 显示队列头
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列空");
        }
        return arr[front+1];
    }
}

数组环形队列

  • front
    • 队列第一个元素
    • 初始值0
  • rear
    • 队列最后一个元素后一个位置
    • 初始值0
  • 队列满条件
    • (rear+1)%maxSize = front
  • 队列空条件
    • rear == font
  • 队列有效值个数
    • (rear+maxSize-front)%maxSize

public class CircleArray{
    private int maxSize;    // 数组容量
    private int front;      // 队列头
    private int rear;       // 队列尾
    private int[] arr;      // 模拟队列
    
    // 创建队列
    public CircleArray(int arrMaxSize){
        this.maxSize = arrMaxSize;
        this.arr = new int[this.maxSize];
        this.front = 0;         // 队列头
        this.rear = 0;          // 队列尾后一位
    }
    
    // 判断队列是否满
    public boolean isFull(){
        return (rear+1)%maxSize == front;
    }
    
    // 判断队列是否为空
    public boolean isEmpty(){
        return rear == front;
    }
    
    // 添加数据
    pulic void addQueue(int n){
        // 判断队列是否满
        if(isFull()){
            System.out.println("队列满,不能加入");
            return;
        }
        arr[rear] = n;
        rear = (rear+1)%maxSize;   
    }
    
    // 取队列
    public int getQueue() {
        if(isEmpty()){
            throw new RuntimeException("队列空,不能取");
        }
        int temp =  arr[front];
        front = (front+1)%maxSize;
        return temp;
    }
    
    // 显示所有数据
    public void showQueue(){
        if(isEmpty()){
            System.out.println("队列空的");
            return;
        }
        //从front开始遍历
        for(int i=front;i<front+size();i++){
            System.out.printf("arr[%d]=%d",i%maxSize,arr[i%maxSize]);
        }
    }
    // 求出当前数据有效数据
    public int size(){
        return (rear+maxSize-front)%maxSize;
    }
    
    // 显示队列头
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列空");
        }
        return arr[front];
    }
}
举报

相关推荐

0 条评论