3. 队列

1.队列介绍

1)队列是一个有序列表,可以用数组链表来实现。
2)遵循先入先出的原则。即:先存入队列的数据,要先取出。后存入的要后取出。
3)示意图:maxSize表示队列容量,front表示头指针,rear表示尾指针

在这里插入图片描述

2.数组模拟队列(出现的问题在数组模拟环形队列中可以解决)

  1. 添加数据(addQueue)
    1)将rear(尾指针)后移:rear++,再将data赋值给数组arr[rear]。注意:front == rear表示队列为空
    2)若rear(尾指针)小于队列的最大的下标maxSize - 1,则将数据存入rear(尾指针)所指的数组元素中,否则无法存入数据。注意:rear == maxSize - 1表示队列为满
  2. 取出数据(removeQueue)
    1)将front(头指针)后移:front++,返回数组arr[front]。
  3. 显示队列(showQueue)
    1)遍历数组。
  4. 显示队首元素(showHeadQueue)
    1)返回arr[front + 1]
  5. 出现的问题:数组只能使用一次
  6. 代码实现:
import java.util.Scanner;

public class ArrayQueue {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Queue q = new Queue(6);
        System.out.println("1.显示队列");
        System.out.println("2.添加数据");
        System.out.println("3.取出数据");
        System.out.println("4.显示队首");
        System.out.println("5.退出");

        boolean bool = true;
        while(bool) {
            System.out.println("请输入操作:");
            int cin = scan.nextInt();
            if(cin != 1 && cin != 2 && cin != 3 && cin != 4 && cin != 5){
                System.out.println("输入有误,请重新输入!");
            }else{
                switch (cin) {
                    case 1:
                        try{
                            q.showQueue();
                        }catch(Exception e){
                            System.out.println(e.getMessage());
                        }
                        break;
                    case 2:
                        System.out.println("请输入一个数字:");
                        int value = scan.nextInt();
                        q.addQueue(value);
                        break;
                    case 3:
                        System.out.println("取出的数据为:" + q.removeQueue());
                        break;
                    case 4:
                        try{
                            System.out.println("队首元素是:" + q.showHeadQueue());
                        }catch(Exception e){
                            System.out.println(e.getMessage());
                        }
                        break;
                    case 5:
                        scan.close();
                        bool = false;
                }
            }
        }
        System.out.println("程序退出~~");
    }
}

class Queue{
    private int maxSize; //队列的容量
    private int front; //队列的头指针
    private int rear; //队列的尾指针
    private int arr[]; //创建模拟队列的数组

    //创建构造器
    public Queue(int size) {
        maxSize = size;
        front = -1;
        rear = -1;
        arr = new int[maxSize];
    }

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

    //队列为满
    public boolean isFull(){
        return rear == maxSize - 1;
    }

    //添加数据
    public void addQueue(int data){
        if(isFull()){
            System.out.println("队列已满~~");
        }
        rear++;
        arr[rear] = data;
    }

    //取出数据
    public int removeQueue(){
        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.println("arr[" + i + "] = " + arr[i]);
        }
    }

    //显示队列队首元素
    public int showHeadQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空~~");
        }
        return arr[front + 1];
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值