Java 队列

目录

循环队列:

题:用两个队列实现栈


队列:先进先出

Queue<Integer> queue=new LinkedList();    //普通队列
Deque<Integer> queue1=new LinkedList<>();  //双端队列(头和尾都可以进出)

LinkedList既可以作为普通的队列,也可以当作双端队列,也可以当作双向链表,也可以当作栈

ArrayList和linkedList的区别:

①共性:增删查改

②内存的逻辑上来说。链表是没有下标的

class Node{
    public int val;
    public Node next;

    public Node(int val) {
        this.val = val;
    }
}

public class textdemo2 {
    public Node head;
    public Node last;

    public static void main(String[] args) {
        textdemo2 queue=new textdemo2();
        queue.offer(3);
        queue.offer(5);
        queue.offer(7);
        System.out.println(queue.peek());
        queue.poll();
        System.out.println(queue.peek());
    }

    //尾插法
    public void offer(int val){
        Node node=new Node(val);
        if(head==null){
            head=node;
            last=node;
        }else {
            last.next=node;
            last=last.next;
        }
    }

    //出队
    public int poll(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        int oldval=head.val;
        this.head=head.next;
        return oldval;
    }

    public boolean isEmpty(){
        return this.head==null;
    }

    //返回队头元素但不删除
    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return head.val;
    }
}

循环队列:

1.使用usedSize,使用usedSize和数组长度比较,确定满或者空

2.设置标志位:入队列时,每放一个元素,就置为true;出队列时,每出一个元素,就置为false

3.每次存放元素之前,都检查一下rear的下一个是不是front,如果是那就是满的。


 

题:用两个队列实现栈

1.要实现入栈的时候,入到不为空的队列,如果刚开始都为空,则指定入到一个即可

2.要实现出栈的时候,找到不为空的队列,出size-1个元素到另一个队列,剩下的这个元素就是出栈的元素

 

class MyQueue{
    Queue<Integer> queue1=new LinkedList<>();
    Queue<Integer> queue2=new LinkedList<>();

    //入一个空队列,如果都为空,则入queue1
    public void push(int val){
        if(!queue1.isEmpty()){
            queue1.offer(val);
        }else if(!queue2.isEmpty()){
            queue2.offer(val);
        }else {
            queue1.offer(val);
        }
    }

    //出队列的时候,找到不为空的队列,出size-1个元素到另一个队列
    public int pop(){
        if(empty()) return -1;
        if(!queue1.isEmpty()){
            for (int i=0;i<queue1.size()-1;i++){
                int val=queue1.poll();
                queue2.offer(val);
            }
            return queue1.poll(); //返回原始队列中最后一个元素,就是要出栈的元素
        }
        if(!queue2.isEmpty()){
            for (int i=0;i<queue2.size()-1;i++){
                int val=queue2.poll();
                queue1.offer(val);
            }
            return queue2.poll();
        }
        return -1;
    }

    public boolean empty(){
        return queue1.isEmpty()&&queue2.isEmpty();
    }

    //得到队头元素
    public int top(){
        if(empty()) return -1;
        if(!queue1.isEmpty()){
            int val=-1;
            int size=queue1.size();
            for (int i=0;i<size;i++){
                val=queue1.poll();
                queue2.offer(val);
            }
            return val; //返回原始队列中最后一个元素,就是要出栈的元素
        }
        if(!queue2.isEmpty()){
            int size=queue2.size();
            int val=-1;
            for (int i=0;i<size;i++){
                val=queue2.poll();
                queue2.poll();
            }
        }
        return -1;
    }

}

两个栈实现队列:

1.入队的时候,统一入到第一个栈

2.出队的时候,统一出到第二个栈里面的元素,如果第二个栈为空,则把第一个栈里面的所有的元素都倒过来,再出栈顶元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值