{"id":9091,"date":"2022-07-12T09:59:35","date_gmt":"2022-07-12T09:59:35","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=9091"},"modified":"2023-05-11T13:02:41","modified_gmt":"2023-05-11T13:02:41","slug":"reversing-a-queue","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/","title":{"rendered":"Reversing a Queue"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1657608159188-Article.jpg\" alt=\"\" \/><\/p>\n<p>In this article, we&#8217;ll look at how to reverse a queue. We must reverse a queue with only standard operations:<\/p>\n<ul>\n<li><strong>enqueue(element):<\/strong> Add an element to the queue&#8217;s tail.<\/li>\n<li><strong>dequeue():<\/strong> Removes an element from the queue&#8217;s front end.<\/li>\n<li><strong>empty():<\/strong> Returns true if the queue is empty, otherwise false.<\/li>\n<\/ul>\n<p>Before we get there, let&#8217;s go over some fundamental queue data structure concepts.<\/p>\n<h2>What is a Queue?<\/h2>\n<p>Queue follows the principle of FIFO (First in First out) i.e. element which is inserted first will be removed first. The operation for <a href=\"https:\/\/prepbytes.com\/blog\/stl-list\/how-to-insert-elements-in-c-stl-list\/\" title=\"insertion of elements\">insertion of elements<\/a> is known as enqueue operation and the operation for deletion of elements is known as dequeue operation. Like Stack, Queue is also a <a href=\"https:\/\/prepbytes.com\/blog\/stacks\/difference-between-stack-and-queue-data-structures\/\" title=\"linear data structure.\">linear data structure.<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1657606447300-Image-02.png\" alt=\"\" \/><\/p>\n<p><strong>Examples:<\/strong><br \/>\n<strong>Input 1:<\/strong><\/p>\n<pre><code>Queue = [11, 22, 33, 44, 55]<\/code><\/pre>\n<p><strong>Output 1:<\/strong><\/p>\n<pre><code>Queue = [55, 44, 33, 22, 11]<\/code><\/pre>\n<p><strong>Input 2:<\/strong><\/p>\n<pre><code>Queue = [12, 24, 36, 48]<\/code><\/pre>\n<p><strong>Output 2:<\/strong><\/p>\n<pre><code>Queue = [48, 36, 24, 12]<\/code><\/pre>\n<h2>Approach 1: Reverse a Queue<\/h2>\n<p>One method for reversing the queue is to store the queue elements in a stack so that when we re-insert the queue elements, they are inserted in reverse order. We use stack because it has the &#8216;LIFO&#8217; property, which means that the last element inserted into the data structure should be the first element of the reversed queue. This problem is solved using a stack. It will be a two-step process:<\/p>\n<ol>\n<li>Push an element from the front end of the queue into the stack. (The stack&#8217;s top element will be the queue&#8217;s last element.)<\/li>\n<li>Push an element from the top of the stack to the back of the queue. (The last element of the stack will be the first to be inserted into the queue.)<\/li>\n<\/ol>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_9092 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_9092 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_9092 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_9092 .wpsm_nav-tabs > li.active > a, #tab_container_9092 .wpsm_nav-tabs > li.active > a:hover, #tab_container_9092 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_9092 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_9092 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_9092 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_9092 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_9092 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_9092 .wpsm_nav-tabs > li > a:hover , #tab_container_9092 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_9092 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_9092 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_9092 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_9092 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_9092 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_9092 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_9092 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9092 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_9092 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9092 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_9092 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_9092\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_9092\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_9092_1\" aria-controls=\"tabs_desc_9092_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_9092_2\" aria-controls=\"tabs_desc_9092_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_9092_3\" aria-controls=\"tabs_desc_9092_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_9092\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_9092_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\nvoid Print(queue&lt;int&gt;&amp; Queue)\r\n{\r\n  while (!Queue.empty()) {\r\n    cout &lt;&lt; Queue.front() &lt;&lt; &quot; &quot;;\r\n    Queue.pop();\r\n  }\r\n}\r\n\r\nvoid reverseQueue(queue&lt;int&gt;&amp; Queue)\r\n{\r\n  stack&lt;int&gt; Stack;\r\n  while (!Queue.empty()) {\r\n    Stack.push(Queue.front());\r\n    Queue.pop();\r\n  }\r\n  while (!Stack.empty()) {\r\n    Queue.push(Stack.top());\r\n    Stack.pop();\r\n  }\r\n}\r\n\r\nint main()\r\n{\r\n  queue&lt;int&gt; Queue;\r\n  Queue.push(10);\r\n  Queue.push(20);\r\n  Queue.push(30);\r\n  Queue.push(40);\r\n  Queue.push(50);\r\n  Queue.push(60);\r\n  Queue.push(70);\r\n  Queue.push(80);\r\n  Queue.push(90);\r\n  Queue.push(100);\r\n\r\n  reverseQueue(Queue);\r\n  Print(Queue);\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_9092_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.LinkedList;\r\nimport java.util.Queue;\r\nimport java.util.Stack;\r\n\r\npublic class Queue_reverse {\r\n\r\n  static Queue&lt;Integer&gt; queue;\r\n\r\n  static void Print()\r\n  {\r\n    while (!queue.isEmpty()) {\r\n      System.out.print(queue.peek() + &quot;, &quot;);\r\n      queue.remove();\r\n    }\r\n  }\r\n\r\n  static void reversequeue()\r\n  {\r\n    Stack&lt;Integer&gt; stack = new Stack&lt;&gt;();\r\n    while (!queue.isEmpty()) {\r\n      stack.add(queue.peek());\r\n      queue.remove();\r\n    }\r\n    while (!stack.isEmpty()) {\r\n      queue.add(stack.peek());\r\n      stack.pop();\r\n    }\r\n  }\r\n\r\n  public static void main(String args[])\r\n  {\r\n    queue = new LinkedList&lt;Integer&gt;();\r\n    queue.add(10);\r\n    queue.add(20);\r\n    queue.add(30);\r\n    queue.add(40);\r\n    queue.add(50);\r\n    queue.add(60);\r\n    queue.add(70);\r\n    queue.add(80);\r\n    queue.add(90);\r\n    queue.add(100);\r\n\r\n    reversequeue();\r\n    Print();\r\n  }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_9092_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nfrom queue import Queue\r\ndef Print(queue):\r\n  while (not queue.empty()):\r\n    print(queue.queue[0], end=\", \")\r\n    queue.get()\r\n\r\n\r\n\r\ndef reversequeue(queue):\r\n  Stack = []\r\n  while (not queue.empty()):\r\n    Stack.append(queue.queue[0])\r\n    queue.get()\r\n  while (len(Stack) != 0):\r\n    queue.put(Stack[-1])\r\n    Stack.pop()\r\n\r\n\r\nif __name__ == '__main__':\r\n  queue = Queue()\r\n  queue.put(10)\r\n  queue.put(20)\r\n  queue.put(30)\r\n  queue.put(40)\r\n  queue.put(50)\r\n  queue.put(60)\r\n  queue.put(70)\r\n  queue.put(80)\r\n  queue.put(90)\r\n  queue.put(100)\r\n\r\n  reversequeue(queue)\r\n  Print(queue)\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_9092 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_9092 a\"),jQuery(\"#tab-content_9092\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<h3>Complexity Analysis to Reverse a Queue Using a Stack<\/h3>\n<p><strong>Time Complexity:<\/strong> The time complexity of this approach is O(n) as we need to insert all the elements in the stack and then add them to the queue.<\/p>\n<p><strong>Space Complexity:<\/strong> The space complexity of this approach is O(n) as we use the stack to store all the elements of the queue.<\/p>\n<h2>Approach 2: Reverse a Queue Using Recursion<\/h2>\n<p>For reversing the queue, we will use the concept of recursion. The approach will first remove the front element from the queue and recursively call the reverse function for the remaining queue. By following this, we are dividing the larger problem into smaller sub-problems.<\/p>\n<h3>Recursive Algorithm<\/h3>\n<ol>\n<li>Pop an element from the queue if the queue has elements; otherwise, return an empty queue.<\/li>\n<li>Call the revQueue function for the remaining queue.<\/li>\n<li>At last, push the popped element into the resultant reversed queue.<\/li>\n<\/ol>\n<h3>Pseudo Code<\/h3>\n<pre><code>queue revQueue(queue)\n{\n    if (queue is empty)\n       return queue;\n    else {\n       data = queue.front()\n       queue.pop()\n       queue = revQueue(queue);\n       q.push(data);\n       return queue;\n    }\n}<\/code><\/pre>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_9093 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_9093 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_9093 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_9093 .wpsm_nav-tabs > li.active > a, #tab_container_9093 .wpsm_nav-tabs > li.active > a:hover, #tab_container_9093 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_9093 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_9093 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_9093 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_9093 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_9093 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_9093 .wpsm_nav-tabs > li > a:hover , #tab_container_9093 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_9093 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_9093 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_9093 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_9093 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_9093 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_9093 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_9093 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9093 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_9093 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9093 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_9093 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_9093\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_9093\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_9093_1\" aria-controls=\"tabs_desc_9093_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_9093_2\" aria-controls=\"tabs_desc_9093_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_9093_3\" aria-controls=\"tabs_desc_9093_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_9093\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_9093_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include <bits\/stdc++.h>\r\nusing namespace std;\r\n\r\nvoid printQueue(queue<long long int> Queue)\r\n{\r\n  while (!Queue.empty()) {\r\n    cout << Queue.front() << \" \";\r\n    Queue.pop();\r\n  }\r\n}\r\n\r\nvoid revQueue(queue<long long int>& q)\r\n{\r\n  if (q.empty())\r\n    return;\r\n\r\n  long long int data = q.front();\r\n  q.pop();\r\n\r\n  revQueue(q);\r\n\r\n  q.push(data);\r\n}\r\n\r\nint main()\r\n{\r\n  queue<long long int> Queue;\r\n  Queue.push(1);\r\n  Queue.push(2);\r\n  Queue.push(3);\r\n  Queue.push(4);\r\n  Queue.push(5);\r\n  revQueue(Queue);\r\n  printQueue(Queue);\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_9093_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\/\/ Java program to reverse a Queue by recursion\r\nimport java.util.LinkedList;\r\nimport java.util.Queue;\r\nimport java.util.Stack;\r\n\r\n\/\/ Java program to reverse a queue recursively\r\n class Queue_reverse {\r\n\r\n  static Queue<Integer> queue;\r\n\r\n  \/\/ Utility function to print the queue\r\n  static void Print()\r\n  {\r\n    while (!queue.isEmpty())\r\n    {\r\n      System.out.print(queue.peek() + \" \");\r\n      queue.remove();\r\n    }\r\n  }\r\n\r\n\/\/ Recurrsive function to reverse the queue\r\nstatic Queue<Integer> revQueue(Queue<Integer> q)\r\n{\r\n  \/\/ Base case\r\n  if (q.isEmpty())\r\n    return q;\r\n\r\n  \/\/ Dequeue current item (from front)\r\n  int data = q.peek();\r\n  q.remove();\r\n\r\n  \/\/ Reverse remaining queue\r\n  q = revQueue(q);\r\n\r\n  \/\/ Enqueue current item (to rear)\r\n  q.add(data);\r\n    \r\n  return q;\r\n}\r\n\r\n\/\/ Driver code\r\npublic static void main(String args[])\r\n{\r\n  queue = new LinkedList<Integer>();\r\n  queue.add(1);\r\n  queue.add(2);\r\n  queue.add(3);\r\n  queue.add(4);\r\n  queue.add(5);\r\n  queue = revQueue(queue);\r\n  Print();\r\n}\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_9093_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nfrom queue import Queue\r\n\r\n\r\ndef revQueue(queue: Queue):\r\n  \r\n  if queue.empty():\r\n    return\r\n\r\n  item = queue.queue[0]\r\n  queue.get()\r\n\r\n  revQueue(queue)\r\n\r\n  queue.put(item)\r\n\r\n\r\ndef print_queue(queue: Queue):\r\n  while not queue.empty():\r\n    print(queue.queue[0], end=\" \")\r\n    queue.get()\r\n  print()\r\n\r\n\r\nif __name__ == \"__main__\":\r\n  q = Queue()\r\n  q.put(1)\r\n  q.put(2)\r\n  q.put(3)\r\n  q.put(4)\r\n  q.put(5)\r\n\r\n  revQueue(q)\r\n  print_queue(q)\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_9093 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_9093 a\"),jQuery(\"#tab-content_9093\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<h3>Complexity Analysis to Reverse a Queue Using Recursion<\/h3>\n<p><strong>Time Complexity:<\/strong> The time complexity of using a recursive function to reverse a queue is O(n).<\/p>\n<p><strong>Space Complexity:<\/strong> Because recursive functions use stack internally, the auxiliary space for reversing a queue using a recursive function will be O(n). <\/p>\n<p><strong>Conclusion<\/strong><br \/>\nThe article goes over two methods for reversing a queue with standard queue operations. The first method employs a stack to store queue elements in reverse order, while the second employs recursion to divide the problem into smaller sub-problems.  Both approaches have an O(n) time complexity and an O(n) space complexity due to the use of auxiliary space for storing elements in the stack or the internal stack used in the recursive function.<\/p>\n<h2>Frequently Asked Questions (FAQs)<\/h2>\n<p><strong>Q1. In C++, is it possible to reverse a queue?<\/strong><br \/>\n<strong>Ans.<\/strong> A queue is a popular First in First Out linear user-defined data structure that can be reversed because it is a linear data structure.<\/p>\n<p><strong>Q2. How long does it take to reverse a queue?<\/strong><br \/>\n<strong>Ans.<\/strong> So, in this approach to reversing a queue, we will dequeue all of the queue&#8217;s elements and push them into the stack, and then, once the queue is empty, we will pop elements from the stack and insert them into the queue. Time Complexity: O(n), where n is the queue size and we iterate the queue once. <\/p>\n<p><strong>Q3. How do I reverse a two-queue queue?<\/strong><br \/>\n<strong>Ans.<\/strong> Assume q1 contains 1,2,3,4,5 elements, which we will reverse and add to q2 as 5,4,3,2,1. Algorithm: There are two queues, q1 and q2. Add the elements to q1, then iterate until size-2 of q1 and add these elements to the back end of q1, resulting in the last element in the first position.<\/p>\n<p><strong>Q4. What is the complexity of a queue&#8217;s space?<\/strong><br \/>\n<strong>Ans.<\/strong> Because no extra space is required for any operation, the space complexity for each operation in a queue is O(1).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we&#8217;ll look at how to reverse a queue. We must reverse a queue with only standard operations: enqueue(element): Add an element to the queue&#8217;s tail. dequeue(): Removes an element from the queue&#8217;s front end. empty(): Returns true if the queue is empty, otherwise false. Before we get there, let&#8217;s go over some [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[128],"tags":[],"class_list":["post-9091","post","type-post","status-publish","format-standard","hentry","category-queues"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Reversing a Queue | Queues | PrepBytes Blog<\/title>\n<meta name=\"description\" content=\"Understanding how to reverse a queue by using only standard operations: enqueue (element), dequeue (element), empty(). Explanation of reversing a queue in C++, java and Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Reversing a Queue | Queues | PrepBytes Blog\" \/>\n<meta property=\"og:description\" content=\"Understanding how to reverse a queue by using only standard operations: enqueue (element), dequeue (element), empty(). Explanation of reversing a queue in C++, java and Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-12T09:59:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-11T13:02:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1657608159188-Article.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Reversing a Queue\",\"datePublished\":\"2022-07-12T09:59:35+00:00\",\"dateModified\":\"2023-05-11T13:02:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/\"},\"wordCount\":746,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1657608159188-Article.jpg\",\"articleSection\":[\"Queues\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/\",\"name\":\"Reversing a Queue | Queues | PrepBytes Blog\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1657608159188-Article.jpg\",\"datePublished\":\"2022-07-12T09:59:35+00:00\",\"dateModified\":\"2023-05-11T13:02:41+00:00\",\"description\":\"Understanding how to reverse a queue by using only standard operations: enqueue (element), dequeue (element), empty(). Explanation of reversing a queue in C++, java and Python.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1657608159188-Article.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1657608159188-Article.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Queues\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/queues\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Reversing a Queue\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Reversing a Queue | Queues | PrepBytes Blog","description":"Understanding how to reverse a queue by using only standard operations: enqueue (element), dequeue (element), empty(). Explanation of reversing a queue in C++, java and Python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/","og_locale":"en_US","og_type":"article","og_title":"Reversing a Queue | Queues | PrepBytes Blog","og_description":"Understanding how to reverse a queue by using only standard operations: enqueue (element), dequeue (element), empty(). Explanation of reversing a queue in C++, java and Python.","og_url":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-07-12T09:59:35+00:00","article_modified_time":"2023-05-11T13:02:41+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1657608159188-Article.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Reversing a Queue","datePublished":"2022-07-12T09:59:35+00:00","dateModified":"2023-05-11T13:02:41+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/"},"wordCount":746,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1657608159188-Article.jpg","articleSection":["Queues"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/","url":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/","name":"Reversing a Queue | Queues | PrepBytes Blog","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1657608159188-Article.jpg","datePublished":"2022-07-12T09:59:35+00:00","dateModified":"2023-05-11T13:02:41+00:00","description":"Understanding how to reverse a queue by using only standard operations: enqueue (element), dequeue (element), empty(). Explanation of reversing a queue in C++, java and Python.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/reversing-a-queue\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1657608159188-Article.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1657608159188-Article.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/reversing-a-queue\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Queues","item":"https:\/\/prepbytes.com\/blog\/category\/queues\/"},{"@type":"ListItem","position":3,"name":"Reversing a Queue"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=9091"}],"version-history":[{"count":5,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9091\/revisions"}],"predecessor-version":[{"id":16274,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9091\/revisions\/16274"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=9091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=9091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=9091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}