0
点赞
收藏
分享

微信扫一扫

基于两个双端队列实现前中后队列

老王420 2022-01-28 阅读 61

设计前中后队列

题目

(1)请你设计一个队列,支持在前,中,后三个位置的 push 和 pop 操作。
(2)请你完成 FrontMiddleBack 类:

  • FrontMiddleBack() 初始化队列。
  • void pushFront(int val) 将 val 添加到队列的 最前面 。
  • void pushMiddle(int val) 将 val 添加到队列的 正中间 。
  • void pushBack(int val) 将 val 添加到队里的 最后面 。
  • int popFront() 将 最前面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。
  • int popMiddle() 将 正中间 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。
  • int popBack() 将 最后面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。

(3)请注意当有两个中间位置的时候,选择靠前面的位置进行操作。比方说:

  • 将 6 添加到 [1, 2, 3, 4, 5] 的中间位置,结果数组为 [1, 2, 6, 3, 4, 5] 。
  • 从 [1, 2, 3, 4, 5, 6] 的中间位置弹出元素,返回 3 ,数组变为 [1, 2, 4, 5, 6] 。

(4)示例如下:
输入:[“FrontMiddleBackQueue”, “pushFront”, “pushBack”, “pushMiddle”, “pushMiddle”, “popFront”, “popMiddle”, “popMiddle”, “popBack”, “popFront”]
[[], [1], [2], [3], [4], [], [], [], [], []]
输出:[null, null, null, null, null, 1, 3, 4, 2, -1]

解释:
FrontMiddleBackQueue q = new FrontMiddleBackQueue();
q.pushFront(1); // [1]
q.pushBack(2); // [1, 2]
q.pushMiddle(3); // [1, 3, 2]
q.pushMiddle(4); // [1, 4, 3, 2]
q.popFront(); // 返回 1 -> [4, 3, 2]
q.popMiddle(); // 返回 3 -> [4, 2]
q.popMiddle(); // 返回 4 -> [2]
q.popBack(); // 返回 2 -> []
q.popFront(); // 返回 -1 -> [] (队列为空)

解决思路

  • 用两个双端队列q1、q2实现前中后队列q(用链表实现双端队列q1和q2,q1和q2收尾相连实现前中后队列)。
  • 注意:每次操作(删除、插入)后都要对q1和q2中的元素个数进行调整。
  • 设q1中的元素的个数为cnt1,q2中的元素的个数为cnt2,每次操作后都要调整q1和q2的中的元素个数,使cnt2 <= cnt1 <= cnt2 + 1。
  • 从q的头部插入元素时,即从q1的头部插入元素。从q的尾部插入元素时,即从q2的尾部插入元素。
  • 从q的头部取出元素时,即从q1的头部取出元素。从q的尾部取出元素时,即从q2的尾部取出元素。
  • 从q的中间插入元素时,先调整q1和q2的元素个数,使 cnt1 <= cnt2,这样每次都只需要从 q1的尾部插入元素即可。
  • 从q的中间取出元素时,每次都从q1的尾部取出元素(因为每次操作后都对q1和q2的元素个数进行了调整,使得cnt2 <= cnt1 <= cnt2 + 1)。
  • 两个双端队列q1和q2收尾相连,如下所示:
    在这里插入图片描述
  • 细节:
    • 在链表的节点类中,实现在某个节点的 前/后 面插入一个节点的方法;实现删除某节点的 前/后 面一个节点的方法。
    • 在双端队列的类中,实现从 对首/对尾 插入/删除 节点的方法;查看头节点/尾节点的值的方法;判断链表是否为空的方法;获取链表的元素个数的方法。
    • 在前中后队列中,实现从 对首/对尾 插入/删除 节点的方法;查看头节点/尾节点的值的方法;调整q1和q2的元素个数的方法;判断链表是否为空的方法。

代码

  • C++
# include <stdio.h>

class Node {
public:
    int val;
    Node *pre;
    Node *next;

    Node(int val=0, Node *pre=nullptr, Node *next=nullptr): val(val), pre(pre), next(next) {}

    void insert_pre(Node *q) {
        q->pre = this->pre;
        q->next = this;
        if (this->pre) {
            this->pre->next = q;
        }
        this->pre = q;
        return;
    }

    void insert_next(Node *q) {
        q->pre = this;
        q->next = this->next;
        if (this->next) {
            this->next->pre = q;
        }
        this->next = q;
        return;
    }

    void delete_pre() {
        if (nullptr == this->pre) {
            return;
        }

        Node *q = this->pre;
        if (q) {
            this->pre = q->pre;
        }
        if (q->pre) {
            q->pre->next = this;
        }
        delete q;
        return;
    }

    void delete_next() {
        if (nullptr == this->next) {
            return;
        }

        Node *q = this->next;
        if (q) {
            this->next = q->next;
        }
        if (q->next) {
            q->next->pre = this;
        }
        delete q;
        return;
    }
};


class DeQueue {
public:
    Node head;
    Node tail;
    int cnt;

    DeQueue(): cnt(0) {
        head.pre = nullptr;
        head.next = &tail;
        tail.pre = &head;
        tail.next = nullptr;
    }

    void push_front(int value) {
        head.insert_next(new Node(value));
        cnt += 1;
        return;
    }

    void push_back(int value) {
        tail.insert_pre(new Node(value));
        cnt += 1;
        return;
    }

    int pop_front() {
        if (isEmpty()) {
            return -1;
        }
        int ret = head.next->val;
        head.delete_next();
        cnt -= 1;
        return ret;
    }

    int pop_back() {
        if (isEmpty()) {
            return -1;
        }
        int ret = tail.pre->val;
        tail.delete_pre();
        cnt -= 1;
        return ret;
    }

    int front() {
        return head.next->val;
    }

    int back() {
        return tail.pre->val;
    }

    int size() {
        return cnt;
    }

    bool isEmpty() {
        return 0 == cnt;
    }
};


class FrontMiddleBackQueu {
public:
    DeQueue q1;
    DeQueue q2;

    FrontMiddleBackQueu(){}

    void pushFront(int value) {
        q1.push_front(value);
        update_size();
        return;
    }

    void pushMiddle(int value) {
        if (q1.size() > q2.size()) {
            q2.push_front(q1.back());
            q1.pop_back();
        }
        q1.push_back(value);
        update_size();
        return;
    }

    void pushBack(int value) {
        q2.push_back(value);
        update_size();
        return;
    }

    int popFront() {
        if (isEmpty()) {
            return -1;
        }
        int ret = q1.pop_front();
        update_size();
        return ret;
    }

    int popMiddle() {
        if (isEmpty()) {
            return -1;
        }
        int ret = q1.pop_back();
        update_size();
        return ret;
    }

    int popBack() {
        if (isEmpty()) {
            return -1;
        }
        int ret;
        if (q2.isEmpty()) {
            ret = q1.pop_back();
        } else {
            ret = q2.pop_back();
        }
        update_size();
        return ret;
    }

    void update_size() {
        while (q1.size() < q2.size()) {
            q1.push_back(q2.front());
            q2.pop_front();
        }

        while (q1.size() >= q2.size() + 2) {
            q2.push_front(q1.back());
            q1.pop_back();
        }
        return;
    }

    bool isEmpty() {
        return 0 == q1.size() + q2.size();
    }
};


int main() {
    FrontMiddleBackQueu *ret = new FrontMiddleBackQueu();
    ret->pushFront(1);
    ret->pushBack(2);
    ret->pushMiddle(3);
    ret->pushMiddle(4);

    printf("%d\n", ret->popFront());
    printf("%d\n", ret->popMiddle());
    printf("%d\n", ret->popMiddle());
    printf("%d\n", ret->popBack());
    printf("%d\n", ret->popFront());

    return 0;
}

说明

  • 对应LeetCode第1670题。
  • 链接:https://leetcode-cn.com/problems/design-front-middle-back-queue/
举报

相关推荐

0 条评论