{"id":903,"date":"2024-10-14T14:06:16","date_gmt":"2024-10-14T08:36:16","guid":{"rendered":"https:\/\/codexplained.in\/?p=903"},"modified":"2025-11-24T15:49:48","modified_gmt":"2025-11-24T10:19:48","slug":"implement-stack-using-arrays","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=903","title":{"rendered":"Implement Using Stack of Arrays"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n#define MAX 5 \/\/ Maximum size of the stack\n\nint stack&#x5B;MAX]; \/\/ Array to store stack elements\nint top = -1;   \/\/ Top is initialized to -1, indicating an empty stack\n\n\/\/ Function to push an element onto the stack\nvoid push(int element) \n{\n    if (top == MAX - 1) \n{\n        printf(&quot;Stack Overflow! Cannot push %d\\n&quot;, element);\n    }\n else \n{\n        top++;\n        stack&#x5B;top] = element;\n        printf(&quot;Pushed %d onto the stack\\n&quot;, element);\n    }\n}\n\n\/\/ Function to pop an element from the stack\nvoid pop() \n{\n    if (top == -1) \n{\n        printf(&quot;Stack Underflow! Cannot pop an element\\n&quot;);\n    } \nelse \n{\n        printf(&quot;Popped %d from the stack\\n&quot;, stack&#x5B;top]);\n        top--; \/\/ Decrease the top position to remove the element\n    }\n}\n\n\/\/ Function to display the stack\nvoid display() \n{\n    if (top == -1) \n{\n        printf(&quot;The stack is empty\\n&quot;);\n    } \nelse \n{\n        printf(&quot;Stack elements are:\\n&quot;);\n        for (int i = top; i &gt;= 0; i--) \n{\n            printf(&quot;%d\\n&quot;, stack&#x5B;i]);\n        }\n    }\n}\n\nint main() \n{\n    int choice, element;\n    while (1) \n{\n        printf(&quot;\\nStack Operations:\\n&quot;);\n        printf(&quot;1. Push\\n2. Pop\\n3. Display\\n4. Exit\\n&quot;);\n        printf(&quot;Enter your choice: &quot;);\n        scanf(&quot;%d&quot;, &amp;choice);\n        \n        switch (choice) \n{\n            case 1:\n                printf(&quot;Enter the element to push: &quot;);\n                scanf(&quot;%d&quot;, &amp;element);\n                push(element);\n                break;\n            case 2:\n                pop();\n                break;\n            case 3:\n                display();\n                break;\n            case 4:\n                exit(0);\n            default:\n                printf(&quot;Invalid choice! Please enter a valid option.\\n&quot;);\n        }\n    }\n    return 0;\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of Code<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Definitions<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>#define MAX 5<\/code>: Defines the maximum size of the stack as 5.<\/li>\n\n\n\n<li><code>int stack[MAX]<\/code>: Declares an array <code>stack<\/code> of size 5, which will be used to store the stack elements.<\/li>\n\n\n\n<li><code>int top = -1<\/code>: Variable <code>top<\/code> keeps track of the topmost element in the stack. Initially, it is set to <code>-1<\/code> indicating the stack is empty.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Functions<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>push()<\/strong>:\n<ul class=\"wp-block-list\">\n<li>This function inserts an element onto the stack. It first checks if the stack is full (<code>top == MAX - 1<\/code>), and if so, it prints &#8220;Stack Overflow!&#8221;.<\/li>\n\n\n\n<li>If the stack is not full, it increments <code>top<\/code> and assigns the new element to <code>stack[top]<\/code>, thus adding it to the top of the stack.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>pop()<\/strong>:\n<ul class=\"wp-block-list\">\n<li>This function removes the top element from the stack. It checks if the stack is empty (<code>top == -1<\/code>), and if so, it prints &#8220;Stack Underflow!&#8221;.<\/li>\n\n\n\n<li>If the stack is not empty, it prints the topmost element and decrements <code>top<\/code>, effectively removing the element from the stack.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>display()<\/strong>:\n<ul class=\"wp-block-list\">\n<li>This function prints all the elements of the stack from top to bottom. It checks if the stack is empty and prints &#8220;The stack is empty&#8221; if there are no elements. If the stack has elements, it prints each element starting from the top.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>main()<\/code> function displays a menu with four options: Push, Pop, Display, and Exit. Based on the user\u2019s input, it calls the respective function to perform the stack operation.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Sample Output<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nStack Operations:\n1. Push\n2. Pop\n3. Display\n4. Exit\nEnter your choice: 1\nEnter the element to push: 10\nPushed 10 onto the stack\n\nStack Operations:\n1. Push\n2. Pop\n3. Display\n4. Exit\nEnter your choice: 1\nEnter the element to push: 20\nPushed 20 onto the stack\n\nStack Operations:\n1. Push\n2. Pop\n3. Display\n4. Exit\nEnter your choice: 3\nStack elements are:\n20\n10\n\nStack Operations:\n1. Push\n2. Pop\n3. Display\n4. Exit\nEnter your choice: 2\nPopped 20 from the stack\n\nStack Operations:\n1. Push\n2. Pop\n3. Display\n4. Exit\nEnter your choice: 3\nStack elements are:\n10\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of Output:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The user first pushes the element <code>10<\/code> onto the stack. The program confirms this with the message: &#8220;Pushed 10 onto the stack&#8221;.<\/li>\n\n\n\n<li>The user then pushes <code>20<\/code>, and it is added to the stack. The program prints the message: &#8220;Pushed 20 onto the stack&#8221;.<\/li>\n\n\n\n<li>The user chooses to display the stack, and the program shows the elements, starting from the top (20 and 10).<\/li>\n\n\n\n<li>The user then pops the top element (20) from the stack, and the program confirms with: &#8220;Popped 20 from the stack&#8221;.<\/li>\n\n\n\n<li>Finally, the user displays the stack again, which now contains only the element <code>10<\/code>.<\/li>\n<\/ol>\n\n\n\n<p>This simple stack implementation using arrays covers the basic operations: push, pop, and display. The stack is a Last In First Out (LIFO) data structure, meaning that the last element inserted is the first to be removed.<\/p>\n\n\n\n<p><\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation of Code Sample Output Explanation of Output: This simple stack implementation using arrays covers the basic operations: push, pop, and display. The stack is a Last In First Out (LIFO) data structure, meaning that the last element inserted is the first to be removed.<\/p>\n","protected":false},"author":45,"featured_media":905,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=903"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/903\/revisions"}],"predecessor-version":[{"id":1438,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/903\/revisions\/1438"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/905"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}