LeetCode队列

队列

请参照课程内容,用循环数组或者链表实现一个队列,包括:

push(int n) - 在队尾插入新元素 n,无需返回
pop() - 队首出队
front() - 获取队首元素
size() - 获取队列大小

题目保证所给操作均合法。
示例:

输入:
["LCQueue", "size", "push", "front", "push", "front", "size", "pop", "front", "pop", "size"]
[[], [], [52], [], [22], [], [], [], [], [], []]
输出:
[null,0,null,52,null,52,2,null,22,null,0]
解释:
LCStack l = LCQueue()
l.size()        // 获取队列长度,此时栈为空,返回 0
l.push(52) // 放入新元素 52,返回 null
l.front()    // 当前队首为 52,返回 52
l.push(22)        // 放入新元素 22,返回 null
l.front() // 当前队首仍然为 52,返回 52
l.size()        // 获取队列长度,返回 2
l.pop()         // 52 出队
l.front()      // 当前队首为 22,返回 22
l.pop()     // 22 出队
l.size()    // 当前队列长度为 0


java双向链表实现:

class LCQueue {
    private int size;
    private Node head;
    private Node tail;

    public LCQueue() {
        size = 0;
        head = tail = new Node(0);
    }

    public void push(int x) {
        tail.next = new Node(x);
        tail.next.prev = tail;
        tail = tail.next;
        size++;
    }

    public void pop() {
        if(head.next.next != null) {
            head.next.next.prev = head;
            head.next = head.next.next;
        } else {
            // 删除前只剩1个元素
            tail = head;
            head.next = null;
        }
        size--;
    }

    public int size() {
        return size;
    }

    public int front() {
        return head.next.value;
    }

    private static class Node {
        int value;
        Node prev;
        Node next;

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

/**
 * Your LCQueue object will be instantiated and called as such:
 * LCQueue obj = new LCQueue();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.size();
 * int param_4 = obj.front();
 */

数组实现:

    class LCQueue {
        int capital = 10;
        int[] arr = new int[capital];
        int start = -1;
        int end = -1;
        int size = 0;
        public LCQueue() {

        }

        public void push(int x) {
            if(size == arr.length){
                int[] copy = new int[capital*2];
                System.arraycopy(arr,start,copy,0,capital-start);
                System.arraycopy(arr,0,copy,capital-start,start);
                arr = copy;
                capital *= 2;
                start = 0;
                end = size-1;
            }

            if(start == -1){
                start = 0;
            }
            end = (end+1)%capital;
            arr[end] = x;
            ++size;
        }

        public void pop() {
            if(size == 0)
                return;
            --size;
            start = (start+1)%capital;
            if(size == 0){
                start = -1;
                end = -1;
            }
        }

        public int size() {
            return size;
        }

        public int front() {
            if(start == -1) return 0;
            return arr[start];
        }
    }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m78探索者

谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值