欢迎关注个人主页:逸狼
创造不易,可以点点赞吗~
如有错误,欢迎指出~
目录
队列
队列的方法
队列方法使用举例
模拟实现队列
使用链表实现队列
使用数组实现队列
设计循环队列
双端队列
用队列实现栈
队列
只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出的特点
入队列:进行插入操作的一端称为队尾
出队列:进行删除操作的一端称为队头

队列的方法

注意: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位置,也可能在其他位置
如何判断数组空还是满?
- 可以使用usedSize 表示数组中元素个数,数组空时:usedSize==0,满时:usedSize==数组长度
- 以标记的方式处理:boolean flag=false;
- 浪费一个空间,空时: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<>();//线性实现
    }上面两个类可以当作链表栈 使用,也可以当作 队列 使用




















