{"id":9141,"date":"2022-07-20T10:38:00","date_gmt":"2022-07-20T10:38:00","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=9141"},"modified":"2022-12-14T10:49:30","modified_gmt":"2022-12-14T10:49:30","slug":"implement-stack-queue-using-deque","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/","title":{"rendered":"Implement Stack Queue using Deque"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222231756-Article.jpg\" alt=\"\" \/><\/p>\n<h3>What is Deque?<\/h3>\n<p>Deque is a double ended queue, i.e. a special kind of queue in which insertion and deletion can be done at the both rear as well as front end of the queue.You can implement both stack and queue by using deque.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222272577-Image-02.png\" alt=\"\" \/><\/p>\n<h3>What is stack?<\/h3>\n<p>Stack follows the principle of LIFO (Last in First out) i.e. element which is inserted at last will be removed first. The operation for insertion of elements in stack is known as Push operation and the operation for deletion of element in stack is known as Pop operation.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222249205-Image-01.png\" alt=\"\" \/><\/p>\n<h3>What is Queue?<\/h3>\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 insertion of elements is known as enqueue operation and the operation for deletion of elements is known as dequeue operation.<\/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>Functions of deque that works same as the functions of stack and queue:<\/p>\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\"><strong><strong>Deque<\/strong><\/strong><\/th><th class=\"has-text-align-left\" data-align=\"left\"><strong>Stack<\/strong><\/th><th class=\"has-text-align-left\" data-align=\"left\"><strong><strong>Queue<\/strong><\/strong><\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\">size()<\/td><td class=\"has-text-align-left\" data-align=\"left\">size()<\/td><td class=\"has-text-align-left\" data-align=\"left\">size()<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">isEmpty()<\/td><td class=\"has-text-align-left\" data-align=\"left\">isEmpty()<\/td><td class=\"has-text-align-left\" data-align=\"left\">isEmpty()<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Insert_First()<\/td><td class=\"has-text-align-left\" data-align=\"left\">&#8211;<\/td><td class=\"has-text-align-left\" data-align=\"left\">&#8211;<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Insert_Last()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Push()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Enqueue()<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Remove_First()<\/td><td class=\"has-text-align-left\" data-align=\"left\">&#8211;<\/td><td class=\"has-text-align-left\" data-align=\"left\">Dequeue()<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Remove_Last()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Pop()<\/td><td class=\"has-text-align-left\" data-align=\"left\">&#8211;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n<h3>Operations of Deque<\/h3>\n<ul>\n<li><strong>size():<\/strong> This function returns the size of the deque.<\/li>\n<li><strong>isEmpty():<\/strong> This function returns true if the deque is empty else false.<\/li>\n<li><strong>Insert_First(element):<\/strong> This function will insert an element in the deque at the front end.<\/li>\n<li><strong>Insert_Last(element):<\/strong> This function will insert an element in the deque at the rear end.<\/li>\n<li><strong>Remove_First():<\/strong> This function will remove the element from the deque which is present at the front end.<\/li>\n<li><strong>Remove_Last():<\/strong> This function will remove the element from the deque which is present at the rear end.<\/li>\n<\/ul>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_9142 {\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_9142 .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_9142 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_9142 .wpsm_nav-tabs > li.active > a, #tab_container_9142 .wpsm_nav-tabs > li.active > a:hover, #tab_container_9142 .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_9142 .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_9142 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_9142 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_9142 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_9142 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_9142 .wpsm_nav-tabs > li > a:hover , #tab_container_9142 .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_9142 .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_9142 .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_9142 .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_9142 .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_9142 .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_9142 .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_9142 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9142 .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_9142 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9142 .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_9142 .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_9142\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_9142\">\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_9142_1\" aria-controls=\"tabs_desc_9142_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_9142_2\" aria-controls=\"tabs_desc_9142_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\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_9142\">\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_9142_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\nstruct DQueNode {\r\n\tint value;\r\n\tDQueNode* next;\r\n\tDQueNode* prev;\r\n};\r\n\r\nclass Deque {\r\nprivate:\r\n\r\n\tDQueNode* head;\r\n\tDQueNode* tail;\r\n\r\npublic:\r\n\tDeque()\r\n\t{\r\n\t\thead = tail = NULL;\r\n\t}\r\n\r\n\tbool isEmpty()\r\n\t{\r\n\t\tif (head == NULL)\r\n\t\t\treturn true;\r\n\t\treturn false;\r\n\t}\r\n\r\n\tint size()\r\n\t{\r\n\t\tif (!isEmpty()) {\r\n\t\t\tDQueNode* temp = head;\r\n\t\t\tint len = 0;\r\n\t\t\twhile (temp != NULL) {\r\n\t\t\t\tlen++;\r\n\t\t\t\ttemp = temp-&gt;next;\r\n\t\t\t}\r\n\t\t\treturn len;\r\n\t\t}\r\n\t\treturn 0;\r\n\t}\r\n\r\n\tvoid insert_first(int element)\r\n\t{\r\n\t\tDQueNode* temp = new DQueNode[sizeof(DQueNode)];\r\n\t\ttemp-&gt;value = element;\r\n\r\n\t\tif (head == NULL) {\r\n\t\t\thead = tail = temp;\r\n\t\t\ttemp-&gt;next = temp-&gt;prev = NULL;\r\n\t\t}\r\n\t\telse {\r\n\t\t\thead-&gt;prev = temp;\r\n\t\t\ttemp-&gt;next = head;\r\n\t\t\ttemp-&gt;prev = NULL;\r\n\t\t\thead = temp;\r\n\t\t}\r\n\t}\r\n\r\n\tvoid insert_last(int element)\r\n\t{\r\n\t\tDQueNode* temp = new DQueNode[sizeof(DQueNode)];\r\n\t\ttemp-&gt;value = element;\r\n\r\n\t\tif (head == NULL) {\r\n\t\t\thead = tail = temp;\r\n\t\t\ttemp-&gt;next = temp-&gt;prev = NULL;\r\n\t\t}\r\n\t\telse {\r\n\t\t\ttail-&gt;next = temp;\r\n\t\t\ttemp-&gt;next = NULL;\r\n\t\t\ttemp-&gt;prev = tail;\r\n\t\t\ttail = temp;\r\n\t\t}\r\n\t}\r\n\r\n\tvoid remove_first()\r\n\t{\r\n\t\tif (!isEmpty()) {\r\n\t\t\tDQueNode* temp = head;\r\n\t\t\thead = head-&gt;next;\r\n\t\t\tif(head) head-&gt;prev = NULL;\r\n\t\t\tdelete temp;\r\n\t\t\tif(head == NULL) tail = NULL;\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tcout &lt;&lt; &quot;List is Empty&quot; &lt;&lt; endl;\r\n\t}\r\n\r\n\tvoid remove_last()\r\n\t{\r\n\t\tif (!isEmpty()) {\r\n\t\t\tDQueNode* temp = tail;\r\n\t\t\ttail = tail-&gt;prev;\r\n\t\t\tif(tail) tail-&gt;next = NULL;\r\n\t\t\tdelete temp;\r\n\t\t\tif(tail == NULL) head = NULL;\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tcout &lt;&lt; &quot;List is Empty&quot; &lt;&lt; endl;\r\n\t}\r\n\r\n\tvoid display()\r\n\t{\r\n\t\tif (!isEmpty()) {\r\n\t\t\tDQueNode* temp = head;\r\n\t\t\twhile (temp != NULL) {\r\n\t\t\t\tcout &lt;&lt; temp-&gt;value &lt;&lt; &quot; &quot;;\r\n\t\t\t\ttemp = temp-&gt;next;\r\n\t\t\t}\r\n\t\t\tcout &lt;&lt; endl;\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tcout &lt;&lt; &quot;List is Empty&quot; &lt;&lt; endl;\r\n\t}\r\n};\r\n\r\nclass Stack : public Deque {\r\npublic:\r\n\tvoid push(int element)\r\n\t{\r\n\t\tinsert_last(element);\r\n\t}\r\n\r\n\tvoid pop()\r\n\t{\r\n\t\tremove_last();\r\n\t}\r\n};\r\n\r\nclass Queue : public Deque {\r\npublic:\r\n\tvoid enqueue(int element)\r\n\t{\r\n\t\tinsert_last(element);\r\n\t}\r\n\r\n\tvoid dequeue()\r\n\t{\r\n\t\tremove_first();\r\n\t}\r\n};\r\n\r\nint main()\r\n{\r\n\tStack stk;\r\n\r\n\tstk.push(5);\r\n\tstk.push(10);\r\n\tcout &lt;&lt; &quot;Stack: &quot;;\r\n\tstk.display();\r\n\r\n\tstk.pop();\r\n\tcout &lt;&lt; &quot;Stack: &quot;;\r\n\tstk.display();\r\n\r\n\tQueue que;\r\n\r\n\tque.enqueue(12);\r\n\tque.enqueue(24);\r\n\tcout &lt;&lt; &quot;Queue: &quot;;\r\n\tque.display();\r\n\r\n\tque.dequeue();\r\n\tcout &lt;&lt; &quot;Queue: &quot;;\r\n\tque.display();\r\n\r\n\tcout &lt;&lt; &quot;Size of Stack is &quot; &lt;&lt; stk.size() &lt;&lt; endl;\r\n\tcout &lt;&lt; &quot;Size of Queue is &quot; &lt;&lt; que.size() &lt;&lt; endl;\r\n\treturn 0;\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_9142_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\nclass PrepBytes{\r\n\r\nstatic class DQueNode\r\n{\r\n\tint value;\r\n\tDQueNode next;\r\n\tDQueNode prev;\r\n}\r\n\r\nstatic class deque\r\n{\r\n\t\r\n\tprivate DQueNode head;\r\n\tprivate DQueNode tail;\r\n\r\n\tpublic deque()\r\n\t{\r\n\t\thead = tail = null;\r\n\t}\r\n\t\r\n\tboolean isEmpty()\r\n\t{\r\n\t\tif (head == null)\r\n\t\t\treturn true;\r\n\t\t\t\r\n\t\treturn false;\r\n\t}\r\n\r\n\tint size()\r\n\t{\r\n\t\t\r\n\t\tif (!isEmpty())\r\n\t\t{\r\n\t\t\tDQueNode temp = head;\r\n\t\t\tint len = 0;\r\n\t\t\t\r\n\t\t\twhile (temp != null)\r\n\t\t\t{\r\n\t\t\t\tlen++;\r\n\t\t\t\ttemp = temp.next;\r\n\t\t\t}\r\n\t\t\treturn len;\r\n\t\t}\r\n\t\treturn 0;\r\n\t}\r\n\r\n\tvoid insert_first(int element)\r\n\t{\r\n\t\t\r\n\t\tDQueNode temp = new DQueNode();\r\n\t\ttemp.value = element;\r\n\r\n\t\tif (head == null)\r\n\t\t{\r\n\t\t\thead = tail = temp;\r\n\t\t\ttemp.next = temp.prev = null;\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\thead.prev = temp;\r\n\t\t\ttemp.next = head;\r\n\t\t\ttemp.prev = null;\r\n\t\t\thead = temp;\r\n\t\t}\r\n\t}\r\n\r\n\tvoid insert_last(int element)\r\n\t{\r\n\t\t\r\n\t\tDQueNode temp = new DQueNode();\r\n\t\ttemp.value = element;\r\n\r\n\t\tif (head == null)\r\n\t\t{\r\n\t\t\thead = tail = temp;\r\n\t\t\ttemp.next = temp.prev = null;\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\ttail.next = temp;\r\n\t\t\ttemp.next = null;\r\n\t\t\ttemp.prev = tail;\r\n\t\t\ttail = temp;\r\n\t\t}\r\n\t}\r\n\r\n\tvoid remove_first()\r\n\t{\r\n\t\t\r\n\t\tif (!isEmpty())\r\n\t\t{\r\n\t\t\tDQueNode temp = head;\r\n\t\t\thead = head.next;\r\n\t\t\thead.prev = null;\r\n\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tSystem.out.print(&quot;List is Empty&quot;);\r\n\t}\r\n\r\n\tvoid remove_last()\r\n\t{\r\n\t\t\r\n\t\tif (!isEmpty())\r\n\t\t{\r\n\t\t\tDQueNode temp = tail;\r\n\t\t\ttail = tail.prev;\r\n\t\t\ttail.next = null;\r\n\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tSystem.out.print(&quot;List is Empty&quot;);\r\n\t}\r\n\r\n\tvoid display()\r\n\t{\r\n\t\t\r\n\t\tif (!isEmpty())\r\n\t\t{\r\n\t\t\tDQueNode temp = head;\r\n\t\t\t\r\n\t\t\twhile (temp != null)\r\n\t\t\t{\r\n\t\t\t\tSystem.out.print(temp.value + &quot; &quot;);\r\n\t\t\t\ttemp = temp.next;\r\n\t\t\t}\r\n\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tSystem.out.print(&quot;List is Empty&quot;);\r\n\t}\r\n}\r\n\r\nstatic class Stack\r\n{\r\n\tdeque d = new deque();\r\n\r\n\tpublic void push(int element)\r\n\t{\r\n\t\td.insert_last(element);\r\n\t}\r\n\r\n\tpublic int size()\r\n\t{\r\n\t\treturn d.size();\r\n\t}\r\n\t\r\n\tpublic void pop()\r\n\t{\r\n\t\td.remove_last();\r\n\t}\r\n\r\n\tpublic void display()\r\n\t{\r\n\t\td.display();\r\n\t}\r\n}\r\n\r\nstatic class Queue\r\n{\r\n\tdeque d = new deque();\r\n\t\r\n\tpublic void enqueue(int element)\r\n\t{\r\n\t\td.insert_last(element);\r\n\t}\r\n\r\n\tpublic void dequeue()\r\n\t{\r\n\t\td.remove_first();\r\n\t}\r\n\r\n\tpublic void display()\r\n\t{\r\n\t\td.display();\r\n\t}\r\n\t\r\n\tpublic int size()\r\n\t{\r\n\t\treturn d.size();\r\n\t}\r\n}\r\n\r\npublic static void main(String[] args)\r\n{\r\n\t\r\n\tStack stk = new Stack();\r\n\r\n\tstk.push(7);\r\n\tstk.push(14);\r\n\tSystem.out.print(&quot;Stack: &quot;);\r\n\tstk.display();\r\n\r\n\tSystem.out.println();\r\n\t\r\n\tstk.pop();\r\n\tSystem.out.print(&quot;Stack: &quot;);\r\n\tstk.display();\r\n\r\n\tSystem.out.println();\r\n\r\n\tQueue que = new Queue();\r\n\r\n\tque.enqueue(12);\r\n\tque.enqueue(24);\r\n\tSystem.out.print(&quot;Queue: &quot;);\r\n\tque.display();\r\n\r\n\tSystem.out.println();\r\n\t\r\n\tque.dequeue();\r\n\tSystem.out.print(&quot;Queue: &quot;);\r\n\tque.display();\r\n\r\n\tSystem.out.println();\r\n\tSystem.out.println(&quot;Size of stack is &quot; +\r\n\t\t\t\t\tstk.size());\r\n\tSystem.out.println(&quot;Size of queue is &quot; +\r\n\t\t\t\t\tque.size());\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\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_9142 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_9142 a\"),jQuery(\"#tab-content_9142\"));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<p>So, in this blog, we have tried to explain how to <strong>Implement Stack Queue using Deque<\/strong>. If you want to strengthen your basic data structures knowledge feel free to check <a href=\"https:\/\/www.prepbytes.com\/prepbytes-courses\">Foundation Courses<\/a> at <a href=\"https:\/\/prepbytes.com\">Prepbytes<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>What is Deque? Deque is a double ended queue, i.e. a special kind of queue in which insertion and deletion can be done at the both rear as well as front end of the queue.You can implement both stack and queue by using deque. What is stack? Stack follows the principle of LIFO (Last in [&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-9141","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>Queues Implement Stack Queue Using Deque | Prepbytes Blog<\/title>\n<meta name=\"description\" content=\"While implementing a queue data structure using stacks, We will have to consider the natural behavior of stack too, which is first in last out.\" \/>\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\/implement-stack-queue-using-deque\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Queues Implement Stack Queue Using Deque | Prepbytes Blog\" \/>\n<meta property=\"og:description\" content=\"While implementing a queue data structure using stacks, We will have to consider the natural behavior of stack too, which is first in last out.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/\" \/>\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-20T10:38:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-14T10:49:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222231756-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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Implement Stack Queue using Deque\",\"datePublished\":\"2022-07-20T10:38:00+00:00\",\"dateModified\":\"2022-12-14T10:49:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/\"},\"wordCount\":317,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222231756-Article.jpg\",\"articleSection\":[\"Queues\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/\",\"name\":\"Queues Implement Stack Queue Using Deque | Prepbytes Blog\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222231756-Article.jpg\",\"datePublished\":\"2022-07-20T10:38:00+00:00\",\"dateModified\":\"2022-12-14T10:49:30+00:00\",\"description\":\"While implementing a queue data structure using stacks, We will have to consider the natural behavior of stack too, which is first in last out.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222231756-Article.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222231756-Article.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#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\":\"Implement Stack Queue using Deque\"}]},{\"@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":"Queues Implement Stack Queue Using Deque | Prepbytes Blog","description":"While implementing a queue data structure using stacks, We will have to consider the natural behavior of stack too, which is first in last out.","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\/implement-stack-queue-using-deque\/","og_locale":"en_US","og_type":"article","og_title":"Queues Implement Stack Queue Using Deque | Prepbytes Blog","og_description":"While implementing a queue data structure using stacks, We will have to consider the natural behavior of stack too, which is first in last out.","og_url":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-07-20T10:38:00+00:00","article_modified_time":"2022-12-14T10:49:30+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222231756-Article.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Implement Stack Queue using Deque","datePublished":"2022-07-20T10:38:00+00:00","dateModified":"2022-12-14T10:49:30+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/"},"wordCount":317,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222231756-Article.jpg","articleSection":["Queues"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/","url":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/","name":"Queues Implement Stack Queue Using Deque | Prepbytes Blog","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222231756-Article.jpg","datePublished":"2022-07-20T10:38:00+00:00","dateModified":"2022-12-14T10:49:30+00:00","description":"While implementing a queue data structure using stacks, We will have to consider the natural behavior of stack too, which is first in last out.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222231756-Article.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1658222231756-Article.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/implement-stack-queue-using-deque\/#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":"Implement Stack Queue using Deque"}]},{"@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\/9141","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=9141"}],"version-history":[{"count":2,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9141\/revisions"}],"predecessor-version":[{"id":9169,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9141\/revisions\/9169"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=9141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=9141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=9141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}