队列 Queue

自己实现一个队列

单链表队列

用单链表实现

public class MyQueue {

    static class Node{
        public int val;
        public Node next;

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

    public Node head;
    public Node last;

    //  入队列
    public void offer(int val){
        Node node = new Node(val);
        if(head == null){
            head = node;
            last = node;
        }else {
            last.next = node;
            last = node;
        }
    }

    //  出队列
    public int pool(){
        if(head == null){
            throw new RuntimeException(" 队列为空 ");
        }else {
            int ret = head.val;
            head = head.next;
            return ret;
        }
    }

    //  获取队首元素
    public int peek(){
        if(head == null){
            throw new RuntimeException(" 队列为空 ");
        }else {
            return head.val;
        }
    }


循环队列 

622. 设计循环队列 - 力扣(LeetCode)

用数组实现

public class MyCircularQueue {

    // 循环队列 通过数组实现
    private int[] elem;
    private int front; // 定义一个头指针
    private int rear; //  定义一个尾指针

    public MyCircularQueue(int k) {
        this.elem = new int[k+1];
    }

    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }else {
            elem[rear] = value;
            rear = (rear+1)% elem.length;
        }
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }else {
            front = (front+1)% elem.length;
        }
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return elem[front];
    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        return elem[(rear+ elem.length - 1)%elem.length];
    }

    public boolean isEmpty() {
        if(front == rear){
            return true;
        }
        return false;
    }

    public boolean isFull() {
        return (rear+1)% elem.length == front;
    }

双端队列   Deque

1.用数组实现

2.用双向链表实现

队列的算法题

用队列实现栈

225. 用队列实现栈 - 力扣(LeetCode)

思路:

用两个队列实现栈

保证一直有一个队列为空

进栈:将 值 放到不为空的队列上

出栈:把队列上的size-1个 值 放到另一条空队列上,poll最后一个值u,完成出栈

class MyStack {
    //  用队列实现栈
    private Queue<Integer> queue1;
    private Queue<Integer> queue2;

    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if(empty()){
            queue1.offer(x);
        } else if (queue1.isEmpty()) {
            queue2.offer(x);
        } else if (queue2.isEmpty()) {
            queue1.offer(x);
        }
    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if ( !queue1.isEmpty()) {
            int size = queue1.size();
            for (int i = 0; i < size-1; i++) {
                int val = queue1.poll();
                queue2.offer(val);
            }
            return queue1.poll();
        } else {
            int size = queue2.size();
            for (int i = 0; i < size-1; i++) {
                int val = queue2.poll();
                queue1.offer(val);
            }
            return queue2.poll();
        }
    }

    public int top() {
        if(empty()){
            return -1;
        }
        if ( !queue1.isEmpty()) {
            int size = queue1.size();
            int val = -1;
            for (int i = 0; i < size; i++) {
                val = queue1.poll();
                queue2.offer(val);
            }
            return val;
        } else {
            int size = queue2.size();
            int val = -1;
            for (int i = 0; i < size; i++) {
                val = queue2.poll();
                queue1.offer(val);
            }
            return val;
        }
    }

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


}

用栈实现队列

232. 用栈实现队列 - 力扣(LeetCode)

思路:

用两个栈实现一个队列

栈 一 s1,栈二 s2

要入队列:把值 push到栈1

要出队列: 1. 判断队列是否为空 

                   2.不为空时,判断栈二是否为空 

                        为空则把栈1 元素都push到栈2里在pop栈2 栈顶元素

                        不为空,则直接pop栈2 栈顶元素

class MyQueue {
    // 用两个栈 实现 一个队列

    private Stack<Integer> stack1;
    private Stack<Integer> stack2;

    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }
    
    public void push(int x) {
        stack1.push(x);
    }
    
    public int pop() {
        if(empty()){
            return -1;
        }
        if( !stack2.isEmpty()){
            return stack2.pop();
        }else {
            int size = stack1.size();
            for (int i = 0; i < size; i++) {
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
    }
    
    public int peek() {
        if(empty()){
            return -1;
        }
        if( !stack2.isEmpty()){
            return stack2.peek();
        }else {
            int size = stack1.size();
            for (int i = 0; i < size; i++) {
                stack2.push(stack1.pop());
            }
            return stack2.peek();
        }
    }
    
    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值