{"id":13588,"date":"2023-02-22T12:35:02","date_gmt":"2023-02-22T12:35:02","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=13588"},"modified":"2023-10-18T06:08:01","modified_gmt":"2023-10-18T06:08:01","slug":"reverse-a-stack-using-recursion","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/","title":{"rendered":"Reverse A Stack Using Recursion"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130102-Reverse%20A%20Stack%20Using%20Recursion.jpg\" alt=\"\" \/><\/p>\n<p>In this tutorial, we will delve into a well-known problem in data structures: &quot;Reversing a Stack Using Recursion.&quot; If you have a good understanding of both stacks and recursion, tackling this task won&#8217;t be overly complex. Rest assured, we&#8217;ll cover the essentials, including explanations of what a stack is, the concept of recursion, and the step-by-step process of reversing a stack using recursive techniques. Let&#8217;s start by exploring the definition of a stack.<\/p>\n<h2>What is a Stack?<\/h2>\n<p>The Stack, a linear data structure, operates based on the Last-In-First-Out (LIFO) principle. This structure possesses a singular end, known as the rear end, and a lone pointer referred to as the top pointer, which indicates the highest element within the stack. Whenever an element is introduced into the stack, it is consistently placed atop the stack, and the only operation permissible for removal is from the stack&#8217;s uppermost part. In simpler terms, a stack serves as a receptacle permitting the addition and elimination of elements solely from one extremity, which is the uppermost section of the stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130257-Reverse%20A%20Stack%20Using%20Recursion1.png\" alt=\"\" \/><\/p>\n<p>The terms <strong>&quot;Push operation&quot;<\/strong> and <strong>&quot;Pop operation&quot;<\/strong> refer to the operations used for addition and removal of the elements from the stack, respectively.<\/p>\n<p>Let\u2019s see how we can \u201creverse a stack using recursion\u201d.<\/p>\n<h2>How To Reverse a Stack Using Recursion<\/h2>\n<p>The term &quot;recursive solution&quot; is used in the problem name itself.<\/p>\n<p>It&#8217;s important to note that a standard stack primarily involves two key operations at its top: the &quot;push&quot; and &quot;pop&quot; operations. Consequently, when the objective is to reverse a stack, the approach involves systematically performing &quot;pop&quot; operations on each element and transferring them one by one to a separate auxiliary stack. Ultimately, this auxiliary stack will contain the same elements as the original stack but in the reverse order.<\/p>\n<p>Recursion can be used instead of an additional stack as it is not what we are after. A recursive function also functions similarly to a stack. Therefore, what we will do is build a recursive function that pops every item from the stack and saves it in the function call stack. The new item kept in the call stack is pushed to the bottom of the input stack when the input stack is empty.<\/p>\n<p>In order to return to the previous state of recursion, we push the stored element in each recursion stack at the bottom of the input stack. In brief, we remove the top element of the input stack and store it in the recursion stack until the stack becomes empty.<\/p>\n<h3>Logic To Reverse Stack Using Recursion<\/h3>\n<p>The goal of the solution is to keep every value in the Function Call Stack until it becomes empty . Place each held element one by one to the bottom of the stack after it is completely full.<\/p>\n<h3>Dry Run<\/h3>\n<p>The example of the above strategy may be seen below.<\/p>\n<ul>\n<li>\n<p>Let given stack be<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130257-Reverse%20A%20Stack%20Using%20Recursion2.png\" alt=\"\" \/><\/p>\n<\/li>\n<li>\n<p>4 will be supplied to the function insert at the bottom after all calls to reverse, and when the stack is empty, 4 will then be pushed to the stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130262-Reverse%20A%20Stack%20Using%20Recursion3.png\" alt=\"\" \/><\/p>\n<\/li>\n<li>\n<p>The function insert at bottom will then receive 3 and check to see if the stack is empty or not before inserting 3 and pushing the other components back.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130262-Reverse%20A%20Stack%20Using%20Recursion4.png\" alt=\"\" \/><\/p>\n<\/li>\n<li>\n<p>The function insert at bottom will then receive 2 and check to see if the stack is empty or not before inserting 2 and pushing back the other elements if necessary.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130262-Reverse%20A%20Stack%20Using%20Recursion5.png\" alt=\"\" \/><\/p>\n<\/li>\n<li>\n<p>The function insert at bottom will then receive 1 and check to see if the stack is empty or not before inserting 1 and pushing back the other components if necessary.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130263-Reverse%20A%20Stack%20Using%20Recursion6.png\" alt=\"\" \/><\/p>\n<\/li>\n<\/ul>\n<p><strong>Following are the steps mentioned below to implement reverse a stack using recursion:<\/strong><\/p>\n<ol>\n<li>Make a stack and place all the elements inside of it.<\/li>\n<li>Call the function reverse() to remove every piece from the stack, then feed the removed element to the insert at bottom() function.<\/li>\n<li>When the function insert at bottom() is invoked, the provided element is inserted at the bottom of the stack.<\/li>\n<li>Print The Stack Elements.<\/li>\n<\/ol>\n<p><strong>Code Implementation<\/strong><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_13600 {\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_13600 .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_13600 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_13600 .wpsm_nav-tabs > li.active > a, #tab_container_13600 .wpsm_nav-tabs > li.active > a:hover, #tab_container_13600 .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_13600 .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_13600 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_13600 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_13600 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_13600 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_13600 .wpsm_nav-tabs > li > a:hover , #tab_container_13600 .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_13600 .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_13600 .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_13600 .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_13600 .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_13600 .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_13600 .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_13600 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_13600 .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_13600 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_13600 .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_13600 .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_13600\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_13600\">\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_13600_1\" aria-controls=\"tabs_desc_13600_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_13600_2\" aria-controls=\"tabs_desc_13600_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>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_13600_3\" aria-controls=\"tabs_desc_13600_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>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_13600_4\" aria-controls=\"tabs_desc_13600_4\" 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_13600\">\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_13600_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#define bool int\r\n\r\n\/\/ structure of a stack node\r\nstruct sNode {\r\n    char data;\r\n    struct sNode* next;\r\n};\r\n\r\n\/\/ Function Prototypes\r\nvoid push(struct sNode** top_ref, int new_data);\r\nint pop(struct sNode** top_ref);\r\nbool isEmpty(struct sNode* top);\r\nvoid print(struct sNode* top);\r\n\r\n\/\/ Below is a recursive function\r\n\/\/ that inserts an element\r\n\/\/ at the bottom of a stack.\r\nvoid insertAtBottom(struct sNode** top_ref, int item)\r\n{\r\n    if (isEmpty(*top_ref))\r\n        push(top_ref, item);\r\n    else {\r\n\r\n        \/\/ Hold all items in Function Call\r\n        \/\/ Stack until we reach end of the\r\n        \/\/ stack. When the stack becomes\r\n        \/\/ empty, the isEmpty(*top_ref)becomes\r\n        \/\/ true, the above if part is executed\r\n        \/\/ and the item is inserted at the bottom\r\n        int temp = pop(top_ref);\r\n        insertAtBottom(top_ref, item);\r\n\r\n        \/\/ Once the item is inserted\r\n        \/\/ at the bottom, push all\r\n        \/\/ the items held in Function\r\n        \/\/ Call Stack\r\n        push(top_ref, temp);\r\n    }\r\n}\r\n\r\n\/\/ Below is the function that\r\n\/\/ reverses the given stack using\r\n\/\/ insertAtBottom()\r\nvoid reverse(struct sNode** top_ref)\r\n{\r\n    if (!isEmpty(*top_ref)) {\r\n        \/\/ Hold all items in Function\r\n        \/\/ Call Stack until we\r\n        \/\/ reach end of the stack\r\n        int temp = pop(top_ref);\r\n        reverse(top_ref);\r\n\r\n        \/\/ Insert all the items (held in\r\n        \/\/ Function Call Stack)\r\n        \/\/ one by one from the bottom\r\n        \/\/ to top. Every item is\r\n        \/\/ inserted at the bottom\r\n        insertAtBottom(top_ref, temp);\r\n    }\r\n}\r\n\r\n\/\/ Driver Code\r\nint main()\r\n{\r\n    struct sNode* s = NULL;\r\n    push(&amp;s, 4);\r\n    push(&amp;s, 3);\r\n    push(&amp;s, 2);\r\n    push(&amp;s, 1);\r\n\r\n    printf(\"&#92;n Original Stack \");\r\n    print(s);\r\n    reverse(&amp;s);\r\n    printf(\"&#92;n Reversed Stack \");\r\n    print(s);\r\n    return 0;\r\n}\r\n\r\n\/\/ Function to check if\r\n\/\/ the stack is empty\r\nbool isEmpty(struct sNode* top)\r\n{\r\n    return (top == NULL) ? 1 : 0;\r\n}\r\n\r\n\/\/ Function to push an item to stack\r\nvoid push(struct sNode** top_ref, int new_data)\r\n{\r\n\r\n    \/\/ allocate node\r\n    struct sNode* new_node\r\n        = (struct sNode*)malloc(sizeof(struct sNode));\r\n\r\n    if (new_node == NULL) {\r\n        printf(\"Stack overflow &#92;n\");\r\n        exit(0);\r\n    }\r\n\r\n    \/\/ put in the data\r\n    new_node-&gt;data = new_data;\r\n\r\n    \/\/ link the old list\r\n    \/\/ off the new node\r\n    new_node-&gt;next = (*top_ref);\r\n\r\n    \/\/ move the head to\r\n    \/\/ point to the new node\r\n    (*top_ref) = new_node;\r\n}\r\n\r\n\/\/ Function to pop an item from stack\r\nint pop(struct sNode** top_ref)\r\n{\r\n    char res;\r\n    struct sNode* top;\r\n\r\n    \/\/ If stack is empty then error\r\n    if (*top_ref == NULL) {\r\n        printf(\"Stack overflow &#92;n\");\r\n        exit(0);\r\n    }\r\n    else {\r\n        top = *top_ref;\r\n        res = top-&gt;data;\r\n        *top_ref = top-&gt;next;\r\n        free(top);\r\n        return res;\r\n    }\r\n}\r\n\r\n\/\/ Function to print a\r\n\/\/ linked list\r\nvoid print(struct sNode* top)\r\n{\r\n    printf(\"&#92;n\");\r\n    while (top != NULL) {\r\n        printf(\" %d \", top-&gt;data);\r\n        top = top-&gt;next;\r\n    }\r\n}\r\n<\/pre>\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_13600_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\nvoid insert_at_bottom(stack&lt;int&gt;&amp; st, int x)\r\n{\r\n\r\n    if (st.size() == 0) {\r\n        st.push(x);\r\n    }\r\n    else {\r\n\r\n        \/\/ All items are held in Function Call\r\n        \/\/ Stack until we reach end of the stack\r\n        \/\/ When the stack becomes empty, the\r\n        \/\/ st.size() becomes 0, the above if\r\n        \/\/ part is executed and the item is\r\n        \/\/ inserted at the bottom\r\n\r\n        int a = st.top();\r\n        st.pop();\r\n        insert_at_bottom(st, x);\r\n\r\n        \/\/ push allthe items held in\r\n        \/\/ Function Call Stack\r\n        \/\/ once the item is inserted\r\n        \/\/ at the bottom\r\n        st.push(a);\r\n    }\r\n}\r\n\r\n\/\/ Below is the function that\r\n\/\/ reverses the given stack using\r\n\/\/ insert_at_bottom()\r\nvoid reverse(stack&lt;int&gt;&amp; st)\r\n{\r\n    if (st.size() &gt; 0) {\r\n\r\n        \/\/ Hold all items in Function\r\n        \/\/ Call Stack until we\r\n        \/\/ reach end of the stack\r\n        int x = st.top();\r\n        st.pop();\r\n        reverse(st);\r\n\r\n        \/\/ Insert all the items held\r\n        \/\/ in Function Call Stack\r\n        \/\/ one by one from the bottom\r\n        \/\/ to top. Every item is\r\n        \/\/ inserted at the bottom\r\n        insert_at_bottom(st, x);\r\n    }\r\n    return;\r\n}\r\n\r\n\/\/ Driver Code\r\nint main()\r\n{\r\n    stack&lt;int&gt; st, st2;\r\n    \/\/ push elements into\r\n    \/\/ the stack\r\n    for (int i = 1; i &lt;= 4; i++) {\r\n        st.push(i);\r\n    }\r\n\r\n    st2 = st;\r\n\r\n    cout &lt;&lt; \"Original Stack\" &lt;&lt; endl;\r\n    \/\/ printing the stack after reversal\r\n    while (!st2.empty()) {\r\n        cout &lt;&lt; st2.top() &lt;&lt; \" \";\r\n        st2.pop();\r\n    }\r\n    cout&lt;&lt;endl;\r\n\r\n    \/\/ function to reverse\r\n    \/\/ the stack\r\n    reverse(st);\r\n    cout &lt;&lt; \"Reversed Stack\" &lt;&lt; endl;\r\n    \/\/ printing the stack after reversal\r\n    while (!st.empty()) {\r\n        cout &lt;&lt; st.top() &lt;&lt; \" \";\r\n        st.pop();\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\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_13600_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">import java.util.Stack;\r\n\r\nclass Test {\r\n\r\n    \/\/ using Stack class for\r\n    \/\/ stack implementation\r\n    static Stack&lt;Character&gt; st = new Stack&lt;&gt;();\r\n\r\n    \/\/ Below is a recursive function\r\n    \/\/ that inserts an element\r\n    \/\/ at the bottom of a stack.\r\n    static void insert_at_bottom(char x)\r\n    {\r\n\r\n        if (st.isEmpty())\r\n            st.push(x);\r\n\r\n        else {\r\n\r\n            \/\/ All items are held in Function\r\n            \/\/ Call Stack until we reach end\r\n            \/\/ of the stack. When the stack becomes\r\n            \/\/ empty, the st.size() becomes 0, the\r\n            \/\/ above if part is executed and\r\n            \/\/ the item is inserted at the bottom\r\n            char a = st.peek();\r\n            st.pop();\r\n            insert_at_bottom(x);\r\n\r\n            \/\/ push allthe items held\r\n            \/\/ in Function Call Stack\r\n            \/\/ once the item is inserted\r\n            \/\/ at the bottom\r\n            st.push(a);\r\n        }\r\n    }\r\n\r\n    \/\/ Below is the function that\r\n    \/\/ reverses the given stack using\r\n    \/\/ insert_at_bottom()\r\n    static void reverse()\r\n    {\r\n        if (st.size() &gt; 0) {\r\n\r\n            \/\/ Hold all items in Function\r\n            \/\/ Call Stack until we\r\n            \/\/ reach end of the stack\r\n            char x = st.peek();\r\n            st.pop();\r\n            reverse();\r\n\r\n            \/\/ Insert all the items held\r\n            \/\/ in Function Call Stack\r\n            \/\/ one by one from the bottom\r\n            \/\/ to top. Every item is\r\n            \/\/ inserted at the bottom\r\n            insert_at_bottom(x);\r\n        }\r\n    }\r\n\r\n    \/\/ Driver Code\r\n    public static void main(String[] args)\r\n    {\r\n\r\n        \/\/ push elements into\r\n        \/\/ the stack\r\n        st.push('1');\r\n        st.push('2');\r\n        st.push('3');\r\n        st.push('4');\r\n\r\n        System.out.println(\"Original Stack\");\r\n\r\n        System.out.println(st);\r\n\r\n        \/\/ function to reverse\r\n        \/\/ the stack\r\n        reverse();\r\n\r\n        System.out.println(\"Reversed Stack\");\r\n\r\n        System.out.println(st);\r\n    }\r\n}\r\n<\/pre>\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_13600_4\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def insertAtBottom(stack, item):\r\n    if isEmpty(stack):\r\n        push(stack, item)\r\n    else:\r\n        temp = pop(stack)\r\n        insertAtBottom(stack, item)\r\n        push(stack, temp)\r\n\r\n# Below is the function that\r\n# reverses the given stack\r\n# using insertAtBottom()\r\n\r\n\r\ndef reverse(stack):\r\n    if not isEmpty(stack):\r\n        temp = pop(stack)\r\n        reverse(stack)\r\n        insertAtBottom(stack, temp)\r\n\r\n# Below is a complete running\r\n# program for testing above\r\n# functions.\r\n\r\n# Function to create a stack.\r\n# It initializes size of stack\r\n# as 0\r\n\r\n\r\ndef createStack():\r\n    stack = []\r\n    return stack\r\n\r\n# Function to check if\r\n# the stack is empty\r\n\r\n\r\ndef isEmpty(stack):\r\n    return len(stack) == 0\r\n\r\n# Function to push an\r\n# item to stack\r\n\r\n\r\ndef push(stack, item):\r\n    stack.append(item)\r\n\r\n# Function to pop an\r\n# item from stack\r\n\r\n\r\ndef pop(stack):\r\n\r\n    # If stack is empty\r\n    # then error\r\n    if(isEmpty(stack)):\r\n        print(\"Stack Underflow \")\r\n        exit(1)\r\n\r\n    return stack.pop()\r\n\r\n# Function to print the stack\r\n\r\n\r\ndef prints(stack):\r\n    for i in range(len(stack)-1, -1, -1):\r\n        print(stack[i], end=' ')\r\n    print()\r\n\r\n# Driver Code\r\n\r\n\r\nstack = createStack()\r\npush(stack, str(4))\r\npush(stack, str(3))\r\npush(stack, str(2))\r\npush(stack, str(1))\r\nprint(\"Original Stack \")\r\nprints(stack)\r\n\r\nreverse(stack)\r\n\r\nprint(\"Reversed Stack \")\r\nprints(stack)\r\n\r\n<\/pre>\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_13600 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_13600 a\"),jQuery(\"#tab-content_13600\"));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><strong>Output<\/strong><\/p>\n<pre><code>Original Stack\n4 3 2 1 \nReversed Stack\n1 2 3 4<\/code><\/pre>\n<p><strong>Complexity Analysis<\/strong><\/p>\n<p><strong>Time Complexity:<\/strong> The time complexity to reverse stack using recursion is O(N**2).<\/p>\n<p><strong>Auxiliary Space:<\/strong> The space complexity to reverse a stack using recursion is O(N).<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nReversing a stack using recursion is a valuable problem-solving exercise in data structures and algorithms. This process leverages the Last-In-First-Out (LIFO) principle of stacks and the recursive nature of the algorithm to achieve the reversal. It involves systematically removing elements from the original stack and placing them onto an auxiliary stack, resulting in the reversal of the stack&#8217;s order.<\/p>\n<h2>FAQs Related To Reverse A Stack Using Recursion<\/h2>\n<p>Below are some of the FAQs related to Reverse a stack using recursion:<\/p>\n<p><strong>1. What is the purpose of reversing a stack using recursion?<\/strong><br \/>\nThe primary purpose is to reverse the order of elements in a stack. This operation can be useful in various scenarios, such as solving certain algorithmic problems or implementing specific data structure operations.<\/p>\n<p><strong>2. Can you reverse a stack using iteration instead of recursion?<\/strong><br \/>\nYes, it is possible to reverse a stack using iteration, but recursion often provides a more elegant and intuitive solution. The recursive approach mirrors the natural behavior of a stack (LIFO) and is frequently preferred.<\/p>\n<p><strong>3. What is the time complexity of reversing a stack using recursion?<\/strong><br \/>\nThe time complexity of reversing a stack using recursion is O(n), where &quot;n&quot; is the number of elements in the stack. This is because each element is pushed and popped once during the reversal process.<\/p>\n<p><strong>4. Are there any limitations or drawbacks to using recursion for stack reversal?<\/strong><br \/>\nRecursive solutions may consume additional memory due to the recursive function calls, which can be a limitation for very large stacks. However, this is generally not a concern for most practical scenarios.<\/p>\n<p><strong>5. Can you provide a high-level overview of the steps involved in reversing a stack using recursion?<\/strong><br \/>\nCertainly. The process involves recursively removing elements from the original stack while preserving them on an auxiliary stack. Once all elements are moved to the auxiliary stack, they are popped back onto the original stack, effectively reversing the order.<\/p>\n<p><strong>6. Are there any practical applications for reversing a stack in real-world programming?<\/strong><br \/>\nReversing a stack can be a useful component of more complex algorithms or data structure operations. For example, it can be employed in solving problems related to expression evaluation or creating specialized data structures.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will delve into a well-known problem in data structures: &quot;Reversing a Stack Using Recursion.&quot; If you have a good understanding of both stacks and recursion, tackling this task won&#8217;t be overly complex. Rest assured, we&#8217;ll cover the essentials, including explanations of what a stack is, the concept of recursion, and the [&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":[127],"tags":[],"class_list":["post-13588","post","type-post","status-publish","format-standard","hentry","category-stacks"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Reverse A Stack Using Recursion<\/title>\n<meta name=\"description\" content=\"Know how to reverse a stack using recursion and the logic and working behind the reversing a stack using recursion.\" \/>\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\/reverse-a-stack-using-recursion\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Reverse A Stack Using Recursion\" \/>\n<meta property=\"og:description\" content=\"Know how to reverse a stack using recursion and the logic and working behind the reversing a stack using recursion.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/\" \/>\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=\"2023-02-22T12:35:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-18T06:08:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130102-Reverse%20A%20Stack%20Using%20Recursion.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=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Reverse A Stack Using Recursion\",\"datePublished\":\"2023-02-22T12:35:02+00:00\",\"dateModified\":\"2023-10-18T06:08:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/\"},\"wordCount\":1083,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130102-Reverse%20A%20Stack%20Using%20Recursion.jpg\",\"articleSection\":[\"Stacks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/\",\"name\":\"Reverse A Stack Using Recursion\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130102-Reverse%20A%20Stack%20Using%20Recursion.jpg\",\"datePublished\":\"2023-02-22T12:35:02+00:00\",\"dateModified\":\"2023-10-18T06:08:01+00:00\",\"description\":\"Know how to reverse a stack using recursion and the logic and working behind the reversing a stack using recursion.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130102-Reverse%20A%20Stack%20Using%20Recursion.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130102-Reverse%20A%20Stack%20Using%20Recursion.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Stacks\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/stacks\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Reverse A Stack Using Recursion\"}]},{\"@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":"Reverse A Stack Using Recursion","description":"Know how to reverse a stack using recursion and the logic and working behind the reversing a stack using recursion.","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\/reverse-a-stack-using-recursion\/","og_locale":"en_US","og_type":"article","og_title":"Reverse A Stack Using Recursion","og_description":"Know how to reverse a stack using recursion and the logic and working behind the reversing a stack using recursion.","og_url":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-02-22T12:35:02+00:00","article_modified_time":"2023-10-18T06:08:01+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130102-Reverse%20A%20Stack%20Using%20Recursion.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Reverse A Stack Using Recursion","datePublished":"2023-02-22T12:35:02+00:00","dateModified":"2023-10-18T06:08:01+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/"},"wordCount":1083,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130102-Reverse%20A%20Stack%20Using%20Recursion.jpg","articleSection":["Stacks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/","url":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/","name":"Reverse A Stack Using Recursion","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130102-Reverse%20A%20Stack%20Using%20Recursion.jpg","datePublished":"2023-02-22T12:35:02+00:00","dateModified":"2023-10-18T06:08:01+00:00","description":"Know how to reverse a stack using recursion and the logic and working behind the reversing a stack using recursion.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130102-Reverse%20A%20Stack%20Using%20Recursion.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1677069130102-Reverse%20A%20Stack%20Using%20Recursion.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/reverse-a-stack-using-recursion\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Stacks","item":"https:\/\/prepbytes.com\/blog\/category\/stacks\/"},{"@type":"ListItem","position":3,"name":"Reverse A Stack Using Recursion"}]},{"@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\/13588","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=13588"}],"version-history":[{"count":5,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/13588\/revisions"}],"predecessor-version":[{"id":18227,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/13588\/revisions\/18227"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=13588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=13588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=13588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}