数据结构之队列

1、队列实现的算法思想

  • 先进先出
  • 需要注意的细节在代码注释部分

2、JAVA实现代码

package basicdata;

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


        ArrayQueue arrayQueue = new ArrayQueue(24);
        arrayQueue.enQueue(12);
        arrayQueue.enQueue(14);
        arrayQueue.enQueue(17);
        arrayQueue.enQueue(15);
        arrayQueue.enQueue(18);
   
        int nmu1 = arrayQueue.headQueue();
        int num2 = arrayQueue.tialQueue();
        System.out.println("对头元素为:");
        System.out.println(nmu1);
        System.out.println("对尾元素为:");
        System.out.println(num2);
        System.out.println("打印整个队列");

        int num3 = arrayQueue.deQueue();
        System.out.println("出队数据:"+num3);

        arrayQueue.showQueue();
    }

}
//使用数组模拟队列
class ArrayQueue {
    private int maxSize;//数组最大容量
    private int front;//队列头
    private int tail;//队列尾
    private int[] array;//存放数据,模拟队列


    //创建队列的构造器
    public ArrayQueue(int arrayMaxSize) {
        maxSize = arrayMaxSize;
        array = new int[maxSize];
        front = -1; // 指向队列头部
        tail = -1; // 指向队列尾
    }

    // 判空
    public boolean isEmpty(){
        return tail == front;
    }

    //判满
    public boolean isFull(){
        return tail == maxSize - 1;
    }

    // 入队
    public void enQueue(int vule){
        if (isFull()){
            System.out.println("队列已满,入队失败!");
            return;
        }
        else {
            tail++;             //尾指针先后移
            array[tail] = vule; //添加数据
        }
    }

    //出队
    public int deQueue(){
        if (isEmpty() ) {
            throw new RuntimeException("队列为空,出队失败!");
           // System.out.println("队列为空,出队失败!");
        }
        else {
            front++;             //头指针后移
            return array[front]; // 数组实际存储数据没变化,不需要管。目的只是取出数据
        }
    }

    //显示队列头数据
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        } else {
            return array[front + 1 ]; //弄清楚为什么+1  理解重点

        }
    }

    //显示队尾数据
    public int tialQueue(){
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        else {
            return array[tail]; //弄清楚为什么不+1  理解重点

        }
    }
    public void showQueue(){
        if (isEmpty()) {
            System.out.println("队列为空,没有数据");
            return;
        }
        else {
            for (int i = 0; i < array.length; i++) {
                System.out.printf("array[%d]=%d\n",i,array[i]);
            }
        }
    }

}

运行结果

对头元素为:
12
对尾元素为:
18
打印整个队列
出队数据:12
array[0]=12
array[1]=14
array[2]=17
array[3]=15
array[4]=18
array[5]=0
array[6]=0
array[7]=0
array[8]=0
array[9]=0
array[10]=0
array[11]=0
array[12]=0
array[13]=0
array[14]=0
array[15]=0
array[16]=0
array[17]=0
array[18]=0
array[19]=0
array[20]=0
array[21]=0
array[22]=0
array[23]=0

Process finished with exit code 0

分析:

  • 数组不能重用
  • 入队数等于数组最大容量时,数组无法使用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值