数据结构note2:队列的实现——用java数组方式

本文深入探讨了队列数据结构的实现方式,包括使用数组实现队列的基本操作如入队、出队、遍历等,以及队列在达到最大容量后的局限性。文章还介绍了如何通过环形数组改进队列,解决复用性问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 * 用数组实现数据结构:队列(头指针,尾指针,最大容量)
 * 添加数据(入队),删除数据(出队),遍历数据(查看所有数据),得到队头队尾元素。
 * 缺点:数组使用一次就不能用了,复用性差。
 * 改进:使用环形数组队列。
 */

//总结:1.队列的逻辑结构是,线性表,FIFO,因此用front指向第一个元素之前,rear指向最后一个元素。当front=rear时,队列为空。
//用数组实现队列这种逻辑结构的时候,会受到数组最大容量的限制,有改进方法:循环数组队列。
public class ArrayQueue {

    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(10);
        System.out.println(queue.isEmpty());
        System.out.println(queue.isFull());
//        int first = queue.getFirst();       //获取不到队头
//        int last = queue.getLast();         //获取不到队尾
        queue.add(1);
        queue.add(2);
        queue.add(3);
        queue.add(4);
        queue.add(5);
        queue.traverse();
        System.out.println("队头元素:"+queue.getFirst());
        System.out.println("队尾元素:"+queue.getLast());
        queue.delete();
        queue.traverse();
        queue.add(6);
        queue.add(7);
        queue.add(8);
        queue.add(9);
        queue.add(10);
        queue.add(11);//添加到第11个元素时,尾指针已经到达数组最大下标,无法继续添加。
    }

    int[] arr;//存放数据的数组
    int MAX_SIZE;//最大容量
    int front;//队头,指向第一个元素前一个位置
    int rear;//队尾,指向最后一个元素

    //队列构造
    public ArrayQueue(int max_size){
        this.MAX_SIZE = max_size;
        front = -1;//
        rear = -1;
        arr = new int[max_size];
    }

    //获得队头元素
    public int getFirst(){
        if (isEmpty()){
            //没有元素的话,就返回一个异常,无法获取到这个元素
            throw new RuntimeException("队列是空的,获取不到第一个元素!");
        }
        return arr[front+1];
    }

    //获得队尾元素
    public int getLast(){
        if (isEmpty()){
            throw new RuntimeException("队列是空的,获取不到最后一个元素!");
        }
        return arr[rear];
    }

    //判断是否为空
    public boolean isEmpty(){
        if (front == rear){
            //当两个指针相等的时候,队列为空
            return true;
        }
        return false;
    }

    //判断是否满了
    public boolean isFull(){
        if (rear == MAX_SIZE-1){
            return true;
        }
        return false;
    }

    //添加元素
    public boolean add(int element){
        if (isFull()){
            throw new RuntimeException("添加元素失败,队列已满");
        }
        //不满可以加入元素,尾指针后移一位,并给最后一个元素赋值
        rear++;
        arr[rear] = element;
        return true;
    }

    //删除元素:出队
    public boolean delete(){
        if (isEmpty()){
            throw new RuntimeException("出队失败,队列已空");
        }
        //出队,把队首元素删除,把front指针后移一位。
        front++;
        return true;
    }

    //遍历所有队列元素
    public void traverse(){
        int counts = 0;
        for (int i = front+1; i < rear+1; i++) {
            counts ++;
            System.out.println("第"+counts+"个元素是:"+arr[i]);
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值