{"id":158,"date":"2026-05-21T07:54:36","date_gmt":"2026-05-21T07:54:36","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=158"},"modified":"2026-05-24T15:49:58","modified_gmt":"2026-05-24T15:49:58","slug":"stacks","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=stacks","title":{"rendered":"Stacks"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Stacks in C++ are part of the Standard Template Library (STL). A stack is a linear data structure that follows the LIFO (Last In, First Out) principle.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Stack?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A stack is a container where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The last element added is the first one removed<\/li>\n\n\n\n<li>Elements are inserted and removed from the top only<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Stacks?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Stacks are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Manage function calls in programs<\/li>\n\n\n\n<li>Support undo and redo operations<\/li>\n\n\n\n<li>Help in expression evaluation<\/li>\n\n\n\n<li>Are used in DFS (Depth First Search)<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Real-Life Example<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a stack of plates:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You add plates on top<\/li>\n\n\n\n<li>You remove the top plate first<\/li>\n\n\n\n<li>You cannot remove a plate from the middle<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is exactly how a stack works.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Stack Header File<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">To use stacks in C++, include the <code>&lt;stack&gt;<\/code> library.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stack&gt;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Declaring a Stack<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>stack&lt;int&gt; s;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Adding Elements (push)<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Elements are added to the top of the stack.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s.push(10);<br>s.push(20);<br>s.push(30);<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Removing Elements (pop)<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Elements are removed from the top.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s.pop();<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Accessing Top Element<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>cout &lt;&lt; s.top();<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Example Program of Stack<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>#include &lt;stack&gt;<br>using namespace std;<br><br>int main() {<br><br>    stack&lt;int&gt; s;<br><br>    s.push(1);<br>    s.push(2);<br>    s.push(3);<br><br>    cout &lt;&lt; \"Top: \" &lt;&lt; s.top() &lt;&lt; endl;<br><br>    s.pop();<br><br>    cout &lt;&lt; \"After pop, Top: \" &lt;&lt; s.top() &lt;&lt; endl;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Top: 3<br>After pop, Top: 2<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Common Stack Functions<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Function<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>push()<\/code><\/td><td>Add element on top<\/td><\/tr><tr><td><code>pop()<\/code><\/td><td>Remove top element<\/td><\/tr><tr><td><code>top()<\/code><\/td><td>Access top element<\/td><\/tr><tr><td><code>size()<\/code><\/td><td>Get number of elements<\/td><\/tr><tr><td><code>empty()<\/code><\/td><td>Check if stack is empty<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">How Stack Works<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Elements are added on top<\/li>\n\n\n\n<li>Last element added is first removed<\/li>\n\n\n\n<li>Only top element is accessible<\/li>\n\n\n\n<li>Works in reverse order<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">Difference Between Stack and Queue<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Stack<\/th><th>Queue<\/th><\/tr><\/thead><tbody><tr><td>LIFO (Last In First Out)<\/td><td>FIFO (First In First Out)<\/td><\/tr><tr><td>Insert\/remove from top<\/td><td>Insert rear, remove front<\/td><\/tr><tr><td>Used in recursion<\/td><td>Used in scheduling<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\">Important Points About Stacks<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Follows LIFO principle<\/li>\n\n\n\n<li>Only top element is accessible<\/li>\n\n\n\n<li>No random access allowed<\/li>\n\n\n\n<li>Simple and efficient structure<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Advantages of Stacks<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Easy to implement<\/li>\n\n\n\n<li>Efficient memory usage<\/li>\n\n\n\n<li>Useful in backtracking<\/li>\n\n\n\n<li>Helps manage program execution<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Common Mistakes with Stacks<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Trying to access middle elements<\/li>\n\n\n\n<li>Calling <code>top()<\/code> on empty stack<\/li>\n\n\n\n<li>Forgetting to check if stack is empty<\/li>\n\n\n\n<li>Confusing stack with queue<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Real-Life Applications<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Stacks are used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Function call management (call stack)<\/li>\n\n\n\n<li>Undo\/redo systems<\/li>\n\n\n\n<li>Expression evaluation<\/li>\n\n\n\n<li>Browser history navigation<\/li>\n\n\n\n<li>DFS algorithm in graphs<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Why Stacks are Important<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Stacks are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Control program execution flow<\/li>\n\n\n\n<li>Help in algorithm design<\/li>\n\n\n\n<li>Manage temporary data efficiently<\/li>\n\n\n\n<li>Are widely used in system operations<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Stacks in C++ are powerful STL containers that follow the LIFO principle. They are widely used in programming, system design, and algorithms where the last inserted element needs to be processed first.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Professional C++ > STL (Standard Template Library) > Stacks<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":55,"template":"","class_list":["post-158","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Stacks - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn stacks in C++ with LIFO concepts, STL stack functions, examples, operations, and real-world applications in data structures.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Stacks - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn stacks in C++ with LIFO concepts, STL stack functions, examples, operations, and real-world applications in data structures.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-24T15:49:58+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=stacks\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Stacks - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T07:54:36+00:00\",\"dateModified\":\"2026-05-24T15:49:58+00:00\",\"description\":\"Learn stacks in C++ with LIFO concepts, STL stack functions, examples, operations, and real-world applications in data structures.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Professional C++ > STL (Standard Template Library) > Stacks\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Stacks - Learn C++Language with GiGz.PK","description":"Learn stacks in C++ with LIFO concepts, STL stack functions, examples, operations, and real-world applications in data structures.","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:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Stacks - Learn C++Language with GiGz.PK","og_description":"Learn stacks in C++ with LIFO concepts, STL stack functions, examples, operations, and real-world applications in data structures.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-24T15:49:58+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=stacks","url":"https:\/\/gigz.pk\/","name":"Stacks - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T07:54:36+00:00","dateModified":"2026-05-24T15:49:58+00:00","description":"Learn stacks in C++ with LIFO concepts, STL stack functions, examples, operations, and real-world applications in data structures.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Professional C++ > STL (Standard Template Library) > Stacks"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}