C++ Simple Code For DesignFrontMiddleBackQueue
Runtime: 24 ms, faster than 97.52% of C++ online submissions for Design Front Middle Back Queue.\n\nclass FrontMiddleBackQueue {\n    deque a, b;\n    void a2b(
Runtime: 24 ms, faster than 97.52% of C++ online submissions for Design Front Middle Back Queue.\n\nclass FrontMiddleBackQueue {\n    deque<int> a, b;\n    void a2b() {\n        if (a.size() <= b.size()) return;\n        b.push_front(a.back());\n        a.pop_back();\n    }\n    void b2a() {\n        if (b.size() <= a.size() + 1) return;\n        a.push_back(b.front());\n        b.pop_front();\n    }\npublic:\n    FrontMiddleBackQueue() {}\n    void pushFront(int val) {\n        a.push_front(val);\n        a2b();\n    }\n    void pushMiddle(int val) {\n        a.push_back(val);\n        a2b();\n    }\n    void pushBack(int val) {\n        b.push_back(val);\n        b2a();\n    }\n    int popFront() {\n        if (a.empty() && b.empty()) return -1;\n        int ans;\n        if (a.empty()) {\n            ans = b.front();\n            b.pop_front();\n        } else {\n            ans = a.front();\n            a.pop_front();\n            b2a();\n        }\n        return ans;\n    }\n    int popMiddle() {\n        if (a.empty() && b.empty()) return -1;\n        int ans;\n        if (a.size() == b.size()) {\n            ans = a.back();\n            a.pop_back();\n        } else {\n            ans = b.front();\n            b.pop_front();\n        }\n        return ans;\n    }\n    int popBack() {\n        if (a.empty() && b.empty()) return -1;\n        int ans = b.back();\n        b.pop_back();\n        a2b();\n        return ans;\n    }\n};\n\n/**\n * Your FrontMiddleBackQueue object will be instantiated and called as such:\n * FrontMiddleBackQueue* obj = new FrontMiddleBackQueue();\n * obj->pushFront(val);\n * obj->pushMiddle(val);\n * obj->pushBack(val);\n * int param_4 = obj->popFront();\n * int param_5 = obj->popMiddle();\n * int param_6 = obj->popBack();\n */\n
[]