04 Queue of data structure and algorithm (2)

Use an array to simulate a circular queue

  • The meaning of the front variable is adjusted, front points to the first element of the queue, that is, arr[front] is the first element of the queue. The initial value of front is 0
  • The meaning of the rear variable is adjusted, and rear points to a position after the last element of the queue. Because I want to make a space. The initial value of rear is 0
  • Judgment that the queue is full: (rear + 1)% maxSize == front
  • Judgment of empty queue: rear == front
  • The number of valid values ​​in the queue: (rear-front + maxSize)% maxSize

Code

import java.util.Scanner;

public class CircleQueueDemo {
    
    
    public static void main(String[] args) {
    
    
        CircleQueue circleQueue = new CircleQueue(3);
        Scanner scanner = new Scanner(System.in);
        char key = ' ';
        boolean loop = true;
        while (loop) {
    
    
            System.out.println("s(show)");
            System.out.println("a(add)");
            System.out.println("g(get)");
            System.out.println("e(exit)");
            System.out.println("h(head)");
            key = scanner.next().charAt(0);  //接受字符
            switch (key) {
    
    
                case 's':
                    circleQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    try {
    
    
                        int temp1 = scanner.nextInt();
                        circleQueue.addQueue(temp1);
                    } catch (Exception e) {
    
    
                        e.printStackTrace();
                    }
                    break;
                case 'g':
                    try {
    
    
                        System.out.println(circleQueue.getQueue());
                    } catch (Exception e) {
    
    
                        e.printStackTrace();
                    }
                    break;
                case 'e':
                    loop = false;
                    break;
                case 'h':
                    System.out.println(circleQueue.headQueue());
                    break;
                default:
                    break;
            }
        }
        System.out.println("退出程序");
    }
}


//模拟队列
class CircleQueue {
    
    
    private int maxSize;     //定义队列最大长度
    private int front;       //定义队列前端
    private int rear;        //定义队列后端
    private int[] arr;       //定义模拟队列的数组

    public CircleQueue(int maxSize){
    
    
        this.maxSize = maxSize;
        front = 0;
        rear = 0;
        arr = new int[maxSize];
    }

    //判断是否满
    public boolean isFull(){
    
    
        return (rear + 1) % maxSize == front;
    }

    //判断是否为空
    public boolean isEmpty(){
    
    
        return rear == front;
    }

    //当前队列有效数据的个数
    public int size(){
    
    
        return (rear + maxSize - front) % maxSize;
    }

    //数据入队
    public 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 value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    //获取队首元素
    public int headQueue(){
    
    
        if(isEmpty()){
    
    
            throw new RuntimeException("队列空");
        }
        return arr[front];
    }

    //展示队列
    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]);
            System.out.println();
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_37054755/article/details/110881610