Home > Java > Javagetting Started > How to implement queue through array in java

How to implement queue through array in java

王林
Release: 2020-02-08 17:38:27
forward
2601 people have browsed it

How to implement queue through array in java

The array implementation queue method is as follows:

How to implement queue through array in java

#1. The queue itself is an ordered list. If the array structure is used to store the queue data, the declaration of the queue array is as shown above, where maxSize is the maximum capacity of the queue;

2. The input and output of the queue are processed from the front and back ends respectively, so two variables, front and rear, are needed to record the front and rear of the queue respectively. The subscript of the end, which will change with data input;

3. The queue where the data is stored is "queue":

①. For empty: rear==front;

②. The queue is full: rear=maxSize-1;

③. When the tail pointer rear

Video tutorial sharing: java video tutorial

The specific implementation code is as follows:

(1), determine whether the queue is full

(2) Determine whether the queue is empty

(3) Add data to the queue

(4) Get the data from the queue and dequeue it;

(5 ), display the current queue data;

(6), display the header data of the queue, be careful not to take out the data;

The example is as follows:

package com.ycx.queue;
import java.util.Scanner;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        //测试
        //创建一个队列
        ArrayQueue queue = new ArrayQueue(3);
        char key = ' ';//接受用户输入
        Scanner input = new Scanner(System.in);
        boolean flag = true; //控制循环 默认死循环

        //输出菜单
        while (flag){
            System.out.println("s(show),显示队列");
            System.out.println("e(exit),退出队列");
            System.out.println("a(add),添加数据到队列");
            System.out.println("g(get),从队列取出数据");
            System.out.println("h(head),查看队列头的数据");

            key=input.next().charAt(0);//接受收一个字符
            switch (key){
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("输一个数");
                    int val=input.nextInt();
                    queue.addQueue(val);
                    break;
                case 'g':   //取出数据  因为方法里面抛出了异常 所以这里需要捕获
                    try{
                        int res=queue.getQueue();
                        System.out.printf("取出的数据为%d\n",res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                     try{
                         int res=queue.headQueue();
                         System.out.printf("队列头的数据为%d\n",res);
                     }catch (Exception e){
                         System.out.println(e.getMessage());
                     }
                     break;
                case 'e':    //退出程序
                    input.close();//关闭
                    flag=false;
                    break;

                    default:
                        break;
            }
        }
        System.out.println("程序退出");
    }
}

//一、使用数组模拟队列-编写一个ArrayQueue类

class ArrayQueue{
    private  int maxSize;//表示数组的最大容量
    private  int front; //队列头
    private  int rear; // 队列尾
    private  int[] arr; // 该数组用于存放数据,模拟队列


    //创建队列的构造器
    public ArrayQueue(int arrMaxsize){
        maxSize=arrMaxsize;
        arr = new int[maxSize];// 初始化数组
        front=-1;//指向队列头部,分析出front是指向队列头的前一个位置 有效数据的位置
        rear=-1;//指向队列尾,指向队列尾的数据(即就是队列最后一个位置 )
    }

    //1.判断队列是否满
    public boolean isFull(){
        return rear==maxSize-1;//因为rear是从-1开始的(如果不理解就看笔记上的图)
    }

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

    //3.添加数据到队列中
    public void addQueue(int n){
        //判断队列是否为满
        if(isFull()){
            System.out.println("队列已满,不能加入数据");
        }
        rear++;//rear 后移
        arr[rear]=n; //也可以直接写成 arr[++rear]:rear先加再取值
    }

    //4.获取队列的数据,出队列
    public  int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列空,不能取数据");
        }
        front++; //front 后移 因为front指向的是前一个元素(front=-1)
        return arr[front];
    }

    //5.显示当前队列数据
    public void showQueue() {
         //遍历
         if(isEmpty()){
             System.out.println("当前队列为空");
             return;
         }
        for (int i = 0; i <arr.length ; i++) {
            System.out.printf("arr[%d]=%d\n",i,arr[i]); //格式化输出
        }
    }

    //6.显示队列的头数据,注意不是取出数据
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return arr[front+1]; //注意:front需要加1
    }
}
Copy after login

Problem analysis and Optimization

(1) Disadvantages: The array can only be used once, and code reuse cannot be achieved.
(When all the elements in the array are taken out, the array will be empty, but elements cannot be added into the array)

(2) Optimization: It can be changed to a circular array (remainder taken) .

Recommended related articles and tutorials: java quick start

The above is the detailed content of How to implement queue through array in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template