数据结构与算法-数组模拟队列

简介: 数据结构与算法-数组模拟队列

队列介绍

队列是一个有序列表,可以使用数组或者链表来实现
遵循原则:先进先出

使用数组模拟队列

在这里插入图片描述

思路分析
1、rear时代表队列的尾部,front代表的队列的头部;
2、第二个图是当有数据加入时,front还是-1,rear在增加;
3、第三个图展示的时从队列取出数据的情况,front在往上移动,rear没有变化。

(1)队列本身是有序列表
(2)当front = rear队列为空
(3)当添加数据的时候rear+1

 但是我们的rear<Max

Size-1【队列有空余】,数组的下标是从0开始的。

代码实现:

做一个程序来模拟数组的增加数据,取出数据

import java.util.Scanner;

public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(3);
        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("g(get): 从队列取出数据");
            System.out.println("a(add): 向队列插入数据");
            System.out.println("h(head): 显示队列头");
            key = scanner.next().charAt(0);

            switch (key){
                case 's':
                    arrayQueue.showArray();
                    break;
                case 'a':
                    System.out.println("输入一个数字");
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int getValue = arrayQueue.getQueue();
                        System.out.println(getValue);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println("查看队列头的信息");
                        int headValue = arrayQueue.headQueue();
                        System.out.printf("队列头的信息为:%d\n",headValue);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");

    }
}

class ArrayQueue{
    private int maxsize;
    private int front;
    private int rear;
    private int[] arr;
    //创建队列的构造器
    public ArrayQueue(int arrMaxsize){
        maxsize = arrMaxsize;
        arr = new int[arrMaxsize];
        front = -1; //指向队列头部
        rear = -1;
    }

    public boolean isFull(){
        return rear == maxsize-1;
    }

    public boolean isEmpty(){
        return front == rear;
    }

    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 void showArray(){
        if(isEmpty()){
            System.out.println("队列空,没数据");
            return;
        }
        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];
    }
}

我们可以看出这里的数组模拟队列是有问题的,当我们把所有的数字取完之后,想要再次插入数据,发现已经无法插入数据了,因为我们的队列不是环形队列。

下期预告

下一讲,我给大家讲如何用数组实现一个完整的环形队列
目录
相关文章
|
17天前
|
消息中间件 存储 搜索推荐
深入理解栈和队列(二):队列
深入理解栈和队列(二):队列
29 0
【队列】数据结构队列的实现
【队列】数据结构队列的实现
|
1月前
|
存储
数据结构--栈和队列
数据结构--栈和队列
|
18天前
|
存储 算法 索引
【算法与数据结构】队列的实现详解
【算法与数据结构】队列的实现详解
|
10天前
|
存储 算法 调度
数据结构期末复习(3)栈和队列
数据结构期末复习(3)栈和队列
17 0
|
13天前
|
算法
算法系列--两个数组的dp问题(2)(下)
算法系列--两个数组的dp问题(2)(下)
18 0
|
13天前
|
存储 算法
算法系列--动态规划--⼦数组、⼦串系列(数组中连续的⼀段)(1)(下)
算法系列--动态规划--⼦数组、⼦串系列(数组中连续的⼀段)(1)
16 0
|
13天前
|
算法
算法系列--动态规划--⼦数组、⼦串系列(数组中连续的⼀段)(1)(上)
算法系列--动态规划--⼦数组、⼦串系列(数组中连续的⼀段)(1)
21 0
|
13天前
|
算法 计算机视觉
算法系列--两个数组的dp问题(1)(下)
算法系列--两个数组的dp问题(1)
18 0
|
13天前
|
算法
算法系列--两个数组的dp问题(1)(上)
算法系列--两个数组的dp问题(1)
14 0