环形队列 数组实现 介绍与实现

环形队列可以使用数组实现,也可以使用循环链表实现。

package jiegou;

import jdk.internal.org.objectweb.asm.tree.analysis.Value;

import java.time.temporal.ValueRange;
import java.util.Scanner;

public class ArrayQueue {
    public static void main(String[] args) {

        Queue queue = new Queue(4);
        char key = ' ';// 接受用户输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;

        while (loop) {
            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):查看队列头数据");
            System.out.println("r(rear):查看队列尾数据");
            System.out.println("n(number):查看队列数据大小");

            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);
                        break;

                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                case 'h':
                    try {
                        int i = queue.headQueue();
                        System.out.printf("队列头的数据是%d\n", i);
                        break;
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }

                case 'e':
                    scanner.close();
                    loop = false;
                    break;

                case 'r':
                    int rear = queue.rear();
                    System.out.printf("队尾数据%d\n",rear);
                    break;
                case 'n':
                    int n = queue.numbers();
                    System.out.printf("有效数据%d\n",n);
                    break;
            }
        }

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


class Queue {
    private int usedSize; // 数组个数
    private int front; // 队列头
    private int rear; //队列尾巴
    private int[] elem;// 数据

    // 构造函数
    public Queue(int n) {
        this.usedSize = 0;
        this.elem = new int[n+1];//数据
        this.front = 0;// 指向队列头部
        this.rear = 0; // 指向队列尾部的下一个
    }

    public int numbers()
    {
        System.out.printf("有效数据%d\n",this.usedSize);
        return this.usedSize;
    }

    // 队列是否满了
    public boolean isFull() {
        return (this.rear+1 )%this.elem.length == this.front;
    }

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

    // 添加数据到队列
    public void addQueue(int value) {
        if (isFull()) {
            System.out.println("队列满了 不能加入数据");
            return;
        }

        this.elem[this.rear] = value;
        this.usedSize++;
        this.rear = (this.rear+1)%this.elem.length;
        this.showQueue();
    }

    // 出队列
    public int getQueue() {
        if (isEmpty()) {
            System.out.println("队列为空 无数据");
            return -1;
        }
        int value = this.elem[this.front];
        this.front = (this.front+1)%this.elem.length;
        this.usedSize--;
        return value;
    }

    // 显示队列数据
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("空队列 无数据");
            return;
        }
        this.headQueue();
        this.numbers();
        for (int i = 0; i < this.elem.length; i++) {
            System.out.printf("arr[%d]=%d\n", i, this.elem[i]);
        }
    }

    // 显示队列头 不取数据
    public int headQueue() {
        if (isEmpty()) {
            //throw new RuntimeException("队列空 没有数据");
            System.out.println("队列为空 无数据");
        }
        // 队里尾部
        //int index = this.rear == 0 ? this.elem.length-1:this.rear-1;
        System.out.printf("队列头索引尾%d\n",this.front);
        int index = this.rear == 0 ? this.elem.length-1:this.rear-1;
        System.out.printf("队尾索引尾%d\n",index);
        return this.elem[this.front];
    }

    public int rear() {
        if (isEmpty()) {
            //throw new RuntimeException("队列空 没有数据");
            System.out.println("队列为空 无数据");
        }
        // 队里尾部
        int index = this.rear == 0 ? this.elem.length-1:this.rear-1;
        System.out.printf("队尾索引尾%d\n",index);
        return this.elem[index];
    }
}

s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
a
输入一个数
1
队列头索引尾0
队尾索引尾0
arr[0]=1
arr[1]=0
arr[2]=0
arr[3]=0
arr[4]=0
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
a
输入一个数
2
队列头索引尾0
队尾索引尾1
arr[0]=1
arr[1]=2
arr[2]=0
arr[3]=0
arr[4]=0
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
a
输入一个数
3
队列头索引尾0
队尾索引尾2
arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=0
arr[4]=0
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
a
输入一个数
4
队列头索引尾0
队尾索引尾3
arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]=0
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
a
输入一个数
5
队列满了 不能加入数据
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
g
取出的数据是1
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
a
输入一个数
6
队列头索引尾1
队尾索引尾4
arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]=6
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
g
取出的数据是2
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
g
取出的数据是3
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
g
取出的数据是4
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
g
取出的数据是6
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
s
空队列 无数据
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小
a
输入一个数
7
队列头索引尾0
队尾索引尾0
arr[0]=7
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]=6
s(show):显示队列
e(exit):退出程序
a(add):添加数据到队列
g(get):从队列取出数据
h(head):查看队列头数据
r(rear):查看队列尾数据
n(number):查看队列数据大小


package www.bittech;

public class MyCircularQueue {
    private int front;//队列头
    private int rear;//队列尾
    private int usedSize;//数据个数
    private int[] elem;//数组
    public MyCircularQueue(int k){
        this.elem=new int[k];
        this.front=0;
        this.rear=0;
        this.usedSize=0;
    }
    public boolean enQueue(int value){
        if(isFull()){
            return false;
        }
        this.elem[this.rear]=value;
        this.usedSize++;
        this.rear=(this.rear+1)%this.elem.length;
        return true;
    }
    //队尾下标加上1在%
    public boolean isFull(){
        if((this.rear+1)%this.elem.length==this.front){
            return true;
        }
        return false;
    }
    public boolean isEmpty(){
        return this.rear==this.front;
    }
    public boolean deQueue(int value){
        if(isEmpty()){
            return false;
        }
        this.elem[front]=value;
        this.front=(this.front+1)%this.elem.length;
        this.usedSize--;
        return true;
    }
    public int Front(){
        if(isEmpty()){
            throw new UnsupportedOperationException("队列为空");
        }
        return this.elem[this.front];
    }
     public int Rear(){
         if(isEmpty()){
             throw new UnsupportedOperationException("队列为空");
         }
         int index=this.rear == 0 ? this.elem.length-1 : this.rear-1;
         return this.elem[index];
     }

}


posted @ 2021-08-14 10:02  brady-wang  阅读(85)  评论(0编辑  收藏  举报