【Java--数据结构】队列

欢迎关注个人主页:逸狼


创造不易,可以点点赞吗~

如有错误,欢迎指出~



目录

队列

队列的方法

队列方法使用举例

模拟实现队列

使用链表实现队列

使用数组实现队列

设计循环队列

双端队列

用队列实现栈


队列

只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出的特点

入队列:进行插入操作的一端称为队尾

出队列:进行删除操作的一端称为队头

队列的方法

注意:Queue是个接口,在实例化时必须实例化LinkedList的对象,因为LinkedList实现了Queue接口。

队列方法使用举例

复制代码
    public static void main(String[] args) {
        Queue<Integer> queue=new LinkedList<>();
        //入队列
        queue.offer(1);//尾插
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        //出队列
        System.out.println(queue.poll());//头删
        //瞄一眼
        System.out.println(queue.peek());
        System.out.println(queue.peek());
        //判空
        System.out.println(queue.isEmpty());

模拟实现队列

使用链表实现队列

入队列offer:使用链表的尾插法

出队列poll:使用头删法;

瞄一眼peek:直接返回头节点的值;

复制代码
public class MyQueue {
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val){
            this.val=val;
        }
    }
    public ListNode head;
    public ListNode last;

    public void offer(int val){
        ListNode node=new ListNode(val);
        if(head==null){
            head=last=node;
        }else{
            //尾插法
            last.next=node;
            node.prev=last;
            last=last.next;
        }
    }
    public int poll(){
        if(head==null){
            return -1;
        }
        int ret=head.val;
        if(head.next==null){//只有一个节点
            head=last=null;
        }else{
            //头删
            head=head.next;
            head.prev=null;
        }
        return ret;
    }
    public int peek(){
        if(head==null){
            return -1;
        }
        return head.val;
    }
    public boolean isEmpty(){
        return head==null;
    }
}

使用数组实现队列

循环队列

last下标代表可以存放数据元素的下标

满和空的时候不一定是在0位置,也可能在其他位置

如何判断数组空还是满?

  1. 可以使用usedSize 表示数组中元素个数,数组空时:usedSize==0 ,满时:usedSize==数组长度
  2. 以标记的方式处理:boolean flag=false;
  3. 浪费一个空间,空时:last和first相遇,满时:last的下一个是first

设计循环队列

oj链接

下面代码是使用上面方法中的:第三种 ,浪费一个空间。

要注意的是这里的last和first往前走使用的公式是:last=(last+1)%数组长度,而不是last++

复制代码
class MyCircularQueue {

    public int[] elem;
    public int first;//first和last默认是0位置,不用初始化
    public int last;

    public MyCircularQueue(int k) {
        elem=new int[k+1];
    }
    //入队列
    public boolean enQueue(int value) {
        if (isFull()){
            return false;
        }
        elem[last]=value;
        last=(last+1)%elem.length;
        return true;
    }
    //出队列
    public boolean deQueue() {
        if (isEmpty()){
            return false;
        }
        first=(first+1)% elem.length;
        return true;
    }
    //得到对头元素,但不删除
    public int Front() {
        if (isEmpty()){
            return -1;
        }
        return elem[first];
    }
    //得到队尾元素,但不删除
    public int Rear() {
        if (isEmpty()){
            return -1;
        }
        int index=(last==0)?
                elem.length-1 :last-1;
        return elem[index];//这里不能直接用last-1,可能会越界
    }
    
    public boolean isEmpty() {
        return first==last;
    }
    
    public boolean isFull() {
        return (last+1)% elem.length==first;
    }
}

双端队列

双端队列(deque)是指允许两端都可以进行入队和出队操作的队列,deque 是 "double ended queue" 的简称。 那就说明元素可以从队头出队和入队,也可以从队尾出队和入队

两边都可以进出

Deque是一个接口,使用时必须创建LinkedList的对象。

复制代码
    public static void main(String[] args) {
        Deque<Integer> queue= new LinkedList<>();//链式实现
        queue.offerFirst(1);//头插
        queue.offer(2);//默认是尾插
        queue.pollFirst();//头删
        
        Deque<Integer> queue2=new ArrayDeque<>();//线性实现
    }

上面两个类可以当作链表 使用,也可以当作 队列 使用

相关推荐
chuxinweihui3 小时前
数据结构——二叉树,堆
c语言·开发语言·数据结构·学习·算法·链表
陈大大陈3 小时前
基于 C++ 的用户认证系统开发:从注册登录到Redis 缓存优化
java·linux·开发语言·数据结构·c++·算法·缓存
看到我,请让我去学习3 小时前
C语言基础(day0424)
c语言·开发语言·数据结构
小李独爱秋3 小时前
动态哈希映射深度指南:从基础到高阶实现与优化
数据结构·算法·哈希算法
周杰伦_Jay5 小时前
continue插件实现IDEA接入本地离线部署的deepseek等大模型
java·数据结构·ide·人工智能·算法·数据挖掘·intellij-idea
岩中竹6 小时前
力扣热题100题解(c++)—矩阵
数据结构·c++·程序人生·算法·leetcode·矩阵
啊阿狸不会拉杆6 小时前
数据结构-图
java·c语言·数据结构·c++·python·算法·图论
稻草猫.7 小时前
【Java 数据结构】泛型
java·数据结构
愚润求学7 小时前
【数据结构】哈希表
数据结构·c++·笔记·散列表
丶Darling.8 小时前
26考研 | 王道 | 数据结构 | 第七章 查找
前端·数据结构·考研