Use arrays to implement queues

What is a queue?

A queue is a data storage structure, and the instantiated queue is a data container, which uses specific rules to store data. The queue follows the first-in-first-out principle. Generally, arrays and linked lists can be used to implement the queue, and the queue's enqueue only starts from the rear, and the dequeue starts from the front.

What should I pay attention to when implementing queues?

The necessary attributes of the queue need to implement the container for data storage, the capacity of the queue, rear and front. The method needs to have a method of entering the team and a method of leaving the right. In addition, entering the queue needs to determine whether the queue is full, out of the queue to determine whether the queue is empty, etc. This is the basic focus of realizing a queue. In addition, after careful consideration, what is the situation when the queue is full? Initially, the queue is empty, rear and front are both -1, and the value range of rear is -1 to maxSize-1. The value range of front is also -1 to maxSize. When the queue is full, the rear must be maxSize-1 and front! =rear, and when the queue is empty, it must be rear=front.

Record code implementation

class  PQueue{
    
    
    private static PQueue pQueue;
    private int[] array;//声明一个数组,用作这个队列的容器
    private int maxSize;//队列的大小
    private int rear;//队列的后端,初始-1
    private int front;//队列的前端-1(front永远指向队列的前一个数),初始-1

    //初始化队列--禁止外部调用
    private PQueue(int maxSize){
    
    
        this.maxSize = maxSize;
        array = new int[this.maxSize];
        rear = -1;
        front = -1;
    }
    //线程安全的单利模式
    public static  PQueue getInstance(int maxSize){
    
    
        if(null == pQueue){
    
    
            synchronized (PQueue.class){
    
    
                pQueue = new PQueue(maxSize);
            }
        }
        return pQueue;
    }

    //判断队列是否已满
    public boolean isFull(){
    
    
        return rear == maxSize-1?false:true && rear!=front;
    }
    //判断队列是否为空
    public synchronized boolean isEmpty(){
    
    
        return rear == front?false:true;
    }

    //队列中增加元素
    public void addPQueue(int num){
    
    
        //先判断队列已满
        if(isFull()){
    
    
            rear++;
            array[rear]=num;
            return;
        }
        System.out.println("队列已满,添加元素失败");
        throw new RuntimeException("队列已满,添加元素失败");
    }

    //队列中删除元素--可能会有多个线程同时操作,需线程安全
    public synchronized int getPQueue(){
    
    
        //判断队列是否为空
        if(isEmpty()){
    
    
            front++;
            return array[front];

        }
        System.out.println("队列为空,删除元素失败");
        throw new RuntimeException("队列为空,删除元素失败");
    }

    //展示队列中所有元素
    public int[] showPQueue(){
    
    

        //数组长度为rear减去front,因front指向数组的前一个值
        int[] realArray = new int[rear-front];
        int index=0;//初始化realArray的下标。
        for(int i=0;i<array.length;i++){
    
    
            //将有效的数据加入到真实数组中
            if(i>front && i<=rear){
    
    
                realArray[index]=array[i];
                index++;
            }
        }
        return realArray;
    }

}

Where is the application scenario of the queue?

1. The most common is the queuing system. You must ensure that the person who takes the number first is served first. This common one is various banks. Here is the simulation of the bank queuing system (thread communication is not implemented, there is a thread safety risk)
code is as follows

//模拟银行业务的线程A
class OperatorA implements Runnable{
    
    
PQueue pQueue = PQueue.getInstance(20);
    @Override
    public void run() {
    
    
        while (pQueue.isEmpty()){
    
    
            System.out.println("请"+pQueue.getPQueue()+"号客户前往A窗口办理业务!");
            System.out.println("业务处理中。。。");
            try {
    
    
                Thread.currentThread().sleep(3000);//假设A窗口处理业务需要3秒钟
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }


    }
}
//模拟银行业务的线程B
class OperatorB implements Runnable{
    
    
    PQueue pQueue = PQueue.getInstance(20);
    @Override
    public void run() {
    
    
        while(pQueue.isEmpty()){
    
    
            System.out.println("请"+pQueue.getPQueue()+"号客户前往B窗口办理业务!");
            System.out.println("业务处理中。。。");
            try {
    
    
                Thread.currentThread().sleep(4000);//假设B窗口处理业务需要4秒钟
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }


    }
}
//模拟银行业务的线程C
class OperatorC implements Runnable{
    
    
    PQueue pQueue = PQueue.getInstance(20);
    @Override
    public void run() {
    
    
        while(pQueue.isEmpty()){
    
    
            System.out.println("请"+pQueue.getPQueue()+"号客户前往C窗口办理业务!");
            System.out.println("业务处理中。。。");
            try {
    
    
                Thread.currentThread().sleep(5000);//假设C窗口处理业务需要5秒钟
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }


    }
}

Execute code

PQueue pQueue = PQueue.getInstance(13);
        //假设有13个人取了票在排队
        pQueue.addPQueue(1);
        pQueue.addPQueue(2);
        pQueue.addPQueue(3);
        pQueue.addPQueue(4);
        pQueue.addPQueue(5);
        pQueue.addPQueue(6);
        pQueue.addPQueue(7);
        pQueue.addPQueue(8);
        pQueue.addPQueue(9);
        pQueue.addPQueue(10);
        pQueue.addPQueue(11);
        pQueue.addPQueue(12);
        pQueue.addPQueue(13);

        //银行窗口开始工作
        Thread threadA = new Thread(new OperatorA());
        Thread threadB = new Thread(new OperatorB());
        Thread threadC = new Thread(new OperatorC());

        threadA.start();
        threadB.start();
        threadC.start();

Results of the

请1号客户前往A窗口办理业务!
业务处理中。。。
请2号客户前往C窗口办理业务!
业务处理中。。。
请3号客户前往B窗口办理业务!
业务处理中。。。
请4号客户前往A窗口办理业务!
业务处理中。。。
请5号客户前往B窗口办理业务!
业务处理中。。。
请6号客户前往C窗口办理业务!
业务处理中。。。
请7号客户前往A窗口办理业务!
业务处理中。。。
请8号客户前往B窗口办理业务!
业务处理中。。。
请9号客户前往A窗口办理业务!
业务处理中。。。
请10号客户前往C窗口办理业务!
业务处理中。。。
请11号客户前往A窗口办理业务!
业务处理中。。。
请12号客户前往B窗口办理业务!
业务处理中。。。
请13号客户前往C窗口办理业务!
业务处理中。。。

Guess you like

Origin blog.csdn.net/m0_46897923/article/details/107690852