Class FrontMiddleBackQueue

java.lang.Object
g1601_1700.s1670_design_front_middle_back_queue.FrontMiddleBackQueue

public class FrontMiddleBackQueue extends Object
1670 - Design Front Middle Back Queue.<p>Medium</p> <p>Design a queue that supports <code>push</code> and <code>pop</code> operations in the front, middle, and back.</p> <p>Implement the <code>FrontMiddleBack</code> class:</p> <ul> <li><code>FrontMiddleBack()</code> Initializes the queue.</li> <li><code>void pushFront(int val)</code> Adds <code>val</code> to the <strong>front</strong> of the queue.</li> <li><code>void pushMiddle(int val)</code> Adds <code>val</code> to the <strong>middle</strong> of the queue.</li> <li><code>void pushBack(int val)</code> Adds <code>val</code> to the <strong>back</strong> of the queue.</li> <li><code>int popFront()</code> Removes the <strong>front</strong> element of the queue and returns it. If the queue is empty, return <code>-1</code>.</li> <li><code>int popMiddle()</code> Removes the <strong>middle</strong> element of the queue and returns it. If the queue is empty, return <code>-1</code>.</li> <li><code>int popBack()</code> Removes the <strong>back</strong> element of the queue and returns it. If the queue is empty, return <code>-1</code>.</li> </ul> <p><strong>Notice</strong> that when there are <strong>two</strong> middle position choices, the operation is performed on the <strong>frontmost</strong> middle position choice. For example:</p> <ul> <li>Pushing <code>6</code> into the middle of <code>[1, 2, 3, 4, 5]</code> results in <code>[1, 2, 6, 3, 4, 5]</code>.</li> <li>Popping the middle from <code>[1, 2, 3, 4, 5, 6]</code> returns <code>3</code> and results in <code>[1, 2, 4, 5, 6]</code>.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> [&ldquo;FrontMiddleBackQueue&rdquo;, &ldquo;pushFront&rdquo;, &ldquo;pushBack&rdquo;, &ldquo;pushMiddle&rdquo;, &ldquo;pushMiddle&rdquo;, &ldquo;popFront&rdquo;, &ldquo;popMiddle&rdquo;, &ldquo;popMiddle&rdquo;, &ldquo;popBack&rdquo;, &ldquo;popFront&rdquo;]</p> <p>[ [], [1], [2], [3], [4], [], [], [], [], []]</p> <p><strong>Output:</strong> [null, null, null, null, null, 1, 3, 4, 2, -1]</p> <p><strong>Explanation:</strong></p> <pre><code> 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(); // return 1 -> [4, 3, 2] q.popMiddle(); // return 3 -> [4, 2] q.popMiddle(); // return 4 -> [2] q.popBack(); // return 2 -> [] q.popFront(); // return -1 -> [] (The queue is empty) </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= val <= 10<sup>9</sup></code></li> <li>At most <code>1000</code> calls will be made to <code>pushFront</code>, <code>pushMiddle</code>, <code>pushBack</code>, <code>popFront</code>, <code>popMiddle</code>, and <code>popBack</code>.</li> </ul>
  • Constructor Details

    • FrontMiddleBackQueue

      public FrontMiddleBackQueue()
  • Method Details

    • pushFront

      public void pushFront(int val)
    • pushMiddle

      public void pushMiddle(int val)
    • pushBack

      public void pushBack(int val)
    • popFront

      public int popFront()
    • popMiddle

      public int popMiddle()
    • popBack

      public int popBack()