Data structure - queue (Java implementation)

Data structure - queue (Java implementation)

Blog Description

Information in the article involved from the Internet and organize personal conclusion, meaning that the individual learning experience and summary, what if infringement, please contact me to delete, thank you!

Brief introduction

A queue is a special linear form, is special in that it only allows deletion at the front end of the table (Front), while the rear end of insertion, the table (REAR), and stack as a queue by the operating linear restriction table. Referred to as the tail end of the end, delete operation will be referred to as head-of-insertion operation .

A queue is an ordered list, you can use an array or a linked list to achieve, follow the FIFO principle

Array queue

Diagram

Here Insert Picture Description

Think

1, front, real initial value of -1 and the maximum value MaxSize

2, the air column conditions: rear = front

3, a queue full condition: real = MaxSize-1

Code

package queue;

import com.sun.source.tree.BreakTree;

import java.util.Iterator;
import java.util.Scanner;

public class ArrayQueue {
    public static void main(String[] args) {
        //创建一个对象
        Array queue = new Array(3);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("请输入字符操作");
            System.out.println("输入s:显示队列");
            System.out.println("输入a:添加数据");
            System.out.println("输入g:取出数据");
            System.out.println("输入h:查看队列头");
            System.out.println("输入e:退出程序");
            key = scanner.next().charAt(0); //接收一个字符
            switch (key){
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    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':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}

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

    //创建队列的构造器
    public Array(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        front = -1;  //指向队列头
        rear = -1;   //指向队列尾
    }

    //判断队列是否满了
    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++;  //rear后移
        arr[rear] = n;
    }

    //获取队列的数据
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,不能取数据");
        }
        front++;  //front后移
        return arr[front];
    }

    //显示队列的所有数据
    public void showQueue(){
        //遍历
        if (isEmpty()){
            throw new RuntimeException("队列为空,不能取数据");
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n",i,arr[i]);
        }
    }

    //显示队列的头数据
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,不能取数据");
        }
        return arr[front + 1];
    }
}

test

[Pictures of foreign chains dumping fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-VHiKUAIr-1584705260920) (/ Users / tanglei / Library / Application Support / typora-user-images / image-20200320195326718 .png)]

Note that you need to add data will be accessed yo.

defect

Array can only be used once

Analog annular array using an array

Diagram

Here Insert Picture Description

Think

1, font initial value is 0, the first element is the queue

2, real initial value is 0, the element to a last element of the queue, (a vacated position as will convention)

3, the conditions for the full column: (real +1) mod MaxSize = front

4, the queue empty conditions: real = front

5, the number of valid data queues: (real + MaxSize-front) mod MaxSize

Code

package queue;

import java.util.Scanner;

public class CricleArrayQueue {
    public static void main(String[] args) {
        //创建一个对象
        CricleArray queue = new CricleArray(4);  //有效数据为3
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("请输入字符操作");
            System.out.println("输入s:显示队列");
            System.out.println("输入a:添加数据");
            System.out.println("输入g:取出数据");
            System.out.println("输入h:查看队列头");
            System.out.println("输入e:退出程序");
            key = scanner.next().charAt(0); //接收一个字符
            switch (key){
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    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':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}

//使用数组模拟环形队列
class CricleArray{
    private int maxSize; //表示最大的容量
    private int front;  //对列头
    private int rear;   //队列尾
    private int[] arr;    //存放数据

    //创建队列的构造器
    public CricleArray(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }

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

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

    //添加数据到队列
    public void addQueue(int n){
        if (isFull()){
            System.out.println("队列满了,不能加数据了");
            return;
        }
        arr[rear] = n;
        rear = (rear + 1) % maxSize;  //rear后移
    }

    //获取队列的数据
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,不能取数据");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;//front后移
        return value;
    }

    //显示队列的所有数据
    public void showQueue(){
        //遍历
        if (isEmpty()){
            System.out.println("");
            throw new RuntimeException("");
        }
        for (int i = front; i < front+size(); i++) {
            System.out.printf("arr[%d]=%d\n",i % maxSize,arr[i % maxSize]);  //格式化输出
        }
    }

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

    //显示队列的头数据
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空,不能取数据");
        }
        return arr[front];
    }
}


test

Here Insert Picture Description

thank

Baidu Encyclopedia

Universal Network

And a hardworking own

Published 171 original articles · won praise 414 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_45163122/article/details/104997271