数据结构与算法:队列

1.队列介绍:

队列是一个有序列表可以用数组或是链表来实现

遵循先入先出原则

2. 数组模拟队列

front代表队列头,rear代表队列尾

2.1、将数据存入队列时称为"addQueue",存入需要两个步骤:

        将尾指针后移:rear+1,当front==rear[空]

        若尾指针小于队列的最大下标maxSize-1,则将数据存入rear所指的数组元素中,否者无法存入数据。rear==maxSize-1

代码:

package sample;

import java.util.Scanner;

public class ArrayQueueDemo {
    public static void main(String[] args) {
    ArrayQueue queue=new ArrayQueue(4);
        Scanner sc=new Scanner(System.in);
        char a;
        boolean b=true;
        while (b){
            System.out.println("输入a添加队列元素");
            System.out.println("输入g获取队列元素");
            System.out.println("输入s遍历队列元素");
            System.out.println("输入h显示队列头部");
            System.out.println("输入e退出队列");
            a=sc.next().charAt(0);
            switch (a){
                case 'a':
                    System.out.println("添加数组元素:");
                    int value=sc.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 's':
                    try {
                        queue.showQueue();
                    }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':
                    sc.close();
                    b=false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("退出队列") ;
    }
}
//创建一个数组队列
 class ArrayQueue{
    private int maxSize;
    private int rear;
    private int front;
    private int[] arr;
    public  ArrayQueue(int arrMaxSize){
        maxSize=arrMaxSize;
        arr=new int[maxSize];
        rear=-1;//rear指向数组尾部
        front=-1;//front指向数组头部的前一个元素
    }
    //判断队列是否为满
    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++;
        arr[rear]=n;
    }
    //获取队列元素
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("数组为空");
        }
        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",i,arr[i]);
        }
    }
    //显示队列头部元素
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("数组为空");
        }
        return arr[front+1];
    }
 }

问题分析并优化:

目前数组使用一次就不能用,没有达到服用的要求

将这个数组使用算法,改进成一个环形数组

3.使用数组模拟环形队列思路分析:

3.1、front变量的含义做一个调整:指向队列的第一个元素,就是说arr[front]就是队列的第一个元素front=0

3.2、rear变量的含义做一个调整:指向队列的最后一个元素的后一个位置,空出一个空间做约定,rear=0

3.3、当队列满时:(rear+1)%maxSize==front

3.4、队列中的有效个数:(rear+maxSize-front)%maxSize

例题:

package sample;

import java.util.Scanner;

public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        CircleArrayQueue queue=new CircleArrayQueue(4);
        Scanner sc=new Scanner(System.in);
        char a;
        boolean b=true;
        while (b){
            System.out.println("输入a添加队列元素");
            System.out.println("输入g获取队列元素");
            System.out.println("输入s遍历队列元素");
            System.out.println("输入h显示队列头部");
            System.out.println("输入e退出队列");
            a=sc.next().charAt(0);
            switch (a){
                case 'a':
                    System.out.println("添加数组元素:");
                    int value=sc.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 's':
                    try {
                        queue.showQueue();
                    }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':
                    sc.close();
                    b=false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("退出队列") ;
    }
}
class CircleArrayQueue{
    private int maxSize;
    private int rear;
    private int front;
    private int[] arr;
    public  CircleArrayQueue(int arrMaxSize){
        rear=0;//rear指向数组尾部的后一个数据
        front=0;//front指向数组头部数据
        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;
    }
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("数组为空,无法取出元素");
        }
        int value=arr[front];
        front=(front+1)%maxSize;
        return  value;
    }
    public void showQueue(){
        if(isEmpty()){
            throw new RuntimeException("数组为空,无法取出元素");
        }
        for(int i=front;i<front+size();i++){
            System.out.printf("arr[%d]=%d",front%maxSize,arr[front%maxSize]);
        }
    }
    public int size(){//队列有效值个数
        return (rear+maxSize-front)%maxSize;
    }
    public int  headQueue(){
        if(isEmpty()){
            throw new RuntimeException("数组为空,无法取出元素");
        }
        return  arr[front];
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值