03 Queue of data structure and algorithm (1)

Queue introduction

  • A queue is an ordered list, which can be implemented as an array or a linked list.
  • Follow the first-in, first-out rule, that is, data that enters the queue first must be taken out first. What you deposit later must be taken out later.

Array simulation queue

  • As shown below
    Insert picture description here
  • Because the output and input of the queue are processed respectively from the front and back ends, two variables front and rear are required to record the subscripts of the front and back ends of the queue respectively. The front will change with data output, and the rear will change with data input.
  • The fatal problem is that when rear=maxSize-1, no matter where the front is, the program will remind "the queue is full"

Code

import java.util.Scanner;

class ArrayQueueDemo{
    
    
    public static void main(String[] args){
    
    
        ArrayQueue arrayQueue = new ArrayQueue(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':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    try {
    
    
                        int temp1 = scanner.nextInt();
                        arrayQueue.addQueue(temp1);
                    }catch(Exception e){
    
    
                        e.printStackTrace();
                    }
                    break;
                case 'g':
                    try{
    
    
                        System.out.println(arrayQueue.getQueue());
                    }catch(Exception e){
    
    
                        e.printStackTrace();
                    }
                    break;
                case 'e':
                    loop = false;
                    break;
                case 'h':
                    System.out.println(arrayQueue.headQueue());
                    break;
                default:
                    break;
            }
        }
        System.out.println("退出程序");
    }
}

//模拟队列
class ArrayQueue {
    
    
    private int maxSize;     //定义队列最大长度
    private int front;       //定义队列前端
    private int rear;        //定义队列后端
    private int[] arr;       //定义模拟队列的数组
    
    public ArrayQueue(int maxSize){
    
    
        this.maxSize = maxSize;
        front = -1;
        rear = -1;
        arr = new int[maxSize];
    }

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

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

    //数据入队
    public 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 int headQueue(){
    
    
        if(isEmpty()){
    
    
            throw new RuntimeException("队列空");
        }
        return arr[front+1];
    }

    //展示队列
    public void showQueue(){
    
    
        if(isEmpty()){
    
    
            System.out.println("队列空");
            return;
        }
        for(int i = front + 1; i <= rear; i++){
    
    
            System.out.printf("%d\t", arr[i]);
        }
        System.out.println();
    }
}

Guess you like

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