✅ Javascript | Simple Solution | 🏆Queue🏆 | 🔥Beats 100%🔥
\n# Intuition\nOptimal structure for any queue is array and any operation with the queue is basic operations with array (unshift(), shift(), push(), pop()), for
![image.png](https://assets.leetcode.com/users/images/423a22d6-3cd6-4974-b8cb-fe1a163ba818_1711639052.232688.png)\n# Intuition\nOptimal structure for any queue is array and any operation with the queue is basic operations with array (`unshift()`, `shift()`, `push()`, `pop()`), for manipulations in the middle of array it\'s `splice()`.\n\n**P.S** it\'s possible to use another structures like LinkedList but complexity of logic is much higher and performance is much lower.\n\n# Approach\nDefine queue and describe manipulation functionality with it:\n\nFor `pushFront` it\'s `unshift()`;\nFor `pushMiddle` it\'s find middle of array and use `splice(startIndex, endIndex, newItem)`;\nFor `pushBack` it\'s `push()`;\n\nFor `popFront` it\'s `shift()`;\nFor `popMiddle` it\'s find middle of array (with feature of even or odd array length) and use `splice(startIndex, endIndex)` as well;\nFor `popBack` it\'s `pop()`.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\n\nvar FrontMiddleBackQueue = function() {\n    this.queue = [];\n};\n\n/** \n * @param {number} val\n * @return {void}\n */\nFrontMiddleBackQueue.prototype.pushFront = function(val) {\n    this.queue.unshift(val);\n};\n\n/** \n * @param {number} val\n * @return {void}\n */\nFrontMiddleBackQueue.prototype.pushMiddle = function(val) {\n    const middle = Math.floor(this.queue.length / 2);\n    this.queue.splice(middle, 0, val);\n};\n\n/** \n * @param {number} val\n * @return {void}\n */\nFrontMiddleBackQueue.prototype.pushBack = function(val) {\n    this.queue.push(val);\n};\n\n/**\n * @return {number}\n */\nFrontMiddleBackQueue.prototype.popFront = function() {\n    if (this.queue[0]) return this.queue.shift();\n    else return -1;\n};\n\n/**\n * @return {number}\n */\nFrontMiddleBackQueue.prototype.popMiddle = function() {\n    const middle = this.queue.length % 2 !== 0 ? Math.floor(this.queue.length / 2) : Math.floor(this.queue.length / 2) - 1;\n    return this.queue[middle] ? this.queue.splice(middle, 1) : -1;\n};\n\n/**\n * @return {number}\n */\nFrontMiddleBackQueue.prototype.popBack = function() {\n    if (this.queue[0]) return this.queue.pop();\n    else return -1;\n};\n\n/** \n * Your FrontMiddleBackQueue object will be instantiated and called as such:\n * var obj = new FrontMiddleBackQueue()\n * obj.pushFront(val)\n * obj.pushMiddle(val)\n * obj.pushBack(val)\n * var param_4 = obj.popFront()\n * var param_5 = obj.popMiddle()\n * var param_6 = obj.popBack()\n */\n```
['Array', 'Queue', 'JavaScript']