{"id":737,"date":"2023-09-03T13:11:17","date_gmt":"2023-09-03T13:11:17","guid":{"rendered":"https:\/\/learnlearn.uk\/ibcs\/?page_id=737"},"modified":"2023-09-04T15:11:14","modified_gmt":"2023-09-04T15:11:14","slug":"stacks","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/ibcs\/stacks\/","title":{"rendered":"Stacks"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Introduction<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Introduction to Stacks<\/h3>\n<p>A stack is a data structure that follows the Last-In-First-Out (LIFO) principle. It&#8217;s essentially a linear collection of elements with two primary operations: push and pop.<\/p>\n<p>Stacks are used to manage data in a way that ensures that the most recently added element is the first one to be removed.<\/p>\n<p>Basic Operations:<\/p>\n<ul>\n<li>Push: This operation adds an element to the top of the stack.<\/li>\n<li>Pop: This operation removes and returns the top element of the stack.<\/li>\n<li>Peek or Top: It retrieves the top element without removing it from the stack.<\/li>\n<li>isEmpty: Checks whether the stack is empty.<\/li>\n<\/ul>\n\n<\/div><h2 class=\"tabtitle\">Applications<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Stack Applications<\/h3>\n<p><strong>Recursive call stack<\/strong><br \/>\nStacks are crucial for managing function calls in programming languages. When a function is called, its execution context is pushed onto the stack, and it&#8217;s popped off when the function returns.<\/p>\n<p><strong>Expression Evaluation<\/strong><br \/>\nStacks are used to evaluate mathematical expressions, particularly converting infix expressions (e.g., 2 + 3) to postfix notation (e.g., 2 3 +) and then evaluating them efficiently.<\/p>\n<p><strong>Undo Functionality<\/strong><br \/>\nStacks can be employed to implement undo and redo functionality in software applications. Each action or state change is pushed onto the stack and popped when the user requests an undo.<\/p>\n<p><strong>Backtracking Algorithms<\/strong><br \/>\nAlgorithms like depth-first search (DFS) use stacks to keep track of choices made during exploration and backtrack when necessary.<\/p>\n<p><strong>Syntax Parsing<\/strong><br \/>\nStacks are used in parsing and interpreting programming languages. They help ensure that the syntax is correctly structured, especially for languages with nested constructs (e.g., parentheses, curly braces).<\/p>\n<p><strong>Memory Management<\/strong><br \/>\nStacks are used in memory management for tracking allocated memory blocks and managing function call frames.<\/p>\n<p><strong>Browser History<\/strong><br \/>\nWeb browsers use stacks to keep track of visited web pages, enabling users to navigate backward and forward through<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Visualization<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Visualization<\/h3>\n<p>Have a play with this interactive stack demonstration.<\/p>\n<p><iframe style=\"width: 100%; height: 700px;\" src=\"https:\/\/learnlearn.uk\/wp-content\/uploads\/2023\/09\/stack_demo.html\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Python Implementation<\/h3>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000aa;\">class<\/span> <span style=\"color: #00aa00; text-decoration: underline;\">Stack<\/span>:\r\n    <span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">__init__<\/span>(<span style=\"color: #00aaaa;\">self<\/span>):\r\n        <span style=\"color: #00aaaa;\">self<\/span>.items = []\r\n    <span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">isEmpty<\/span>(<span style=\"color: #00aaaa;\">self<\/span>):\r\n        <span style=\"color: #0000aa;\">return<\/span> <span style=\"color: #00aaaa;\">len<\/span>(<span style=\"color: #00aaaa;\">self<\/span>.items) == <span style=\"color: #009999;\">0<\/span>\r\n    <span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">push<\/span>(<span style=\"color: #00aaaa;\">self<\/span>, item):\r\n        <span style=\"color: #00aaaa;\">self<\/span>.items.append(item)\r\n    <span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">pop<\/span>(<span style=\"color: #00aaaa;\">self<\/span>):\r\n        <span style=\"color: #0000aa;\">if<\/span> <span style=\"color: #0000aa;\">not<\/span> <span style=\"color: #00aaaa;\">self<\/span>.isEmpty():\r\n            <span style=\"color: #0000aa;\">return<\/span> <span style=\"color: #00aaaa;\">self<\/span>.items.pop()\r\n        <span style=\"color: #0000aa;\">else<\/span>:\r\n            <span style=\"color: #0000aa;\">raise<\/span> IndexError(<span style=\"color: #aa5500;\">\"Pop from an empty stack\"<\/span>)\r\n    <span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">peek<\/span>(<span style=\"color: #00aaaa;\">self<\/span>):\r\n        <span style=\"color: #0000aa;\">if<\/span> <span style=\"color: #0000aa;\">not<\/span> <span style=\"color: #00aaaa;\">self<\/span>.isEmpty():\r\n            <span style=\"color: #0000aa;\">return<\/span> <span style=\"color: #00aaaa;\">self<\/span>.items[-<span style=\"color: #009999;\">1<\/span>]\r\n        <span style=\"color: #0000aa;\">else<\/span>:\r\n            <span style=\"color: #0000aa;\">raise<\/span> IndexError(<span style=\"color: #aa5500;\">\"Peek from an empty stack\"<\/span>)\r\n    <span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">size<\/span>(<span style=\"color: #00aaaa;\">self<\/span>):\r\n        <span style=\"color: #0000aa;\">return<\/span> <span style=\"color: #00aaaa;\">len<\/span>(<span style=\"color: #00aaaa;\">self<\/span>.items)\r\n<\/pre>\n<\/div>\n\n<\/div><h2 class=\"tabtitle\">Resources<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Resources<\/h3>\n<p><a href=\"https:\/\/docs.google.com\/presentation\/d\/1EfZ8DWskKChmuqmyXf9mqgT4mRTVOxbVZaUQ-OL7lcQ\/edit?usp=sharing\">https:\/\/docs.google.com\/presentation\/d\/1EfZ8DWskKChmuqmyXf9mqgT4mRTVOxbVZaUQ-OL7lcQ\/edit?usp=sharing<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Stacks A stack is a data structure that follows the Last-In-First-Out (LIFO) principle. It&#8217;s essentially a linear collection of elements with two primary operations: push and pop. Stacks are used to manage data in a way that ensures that the most recently added element is the first one to be removed. Basic Operations:&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/ibcs\/stacks\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Stacks<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"off","neve_meta_content_width":100,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Stacks - IB Computer Science<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/ibcs\/stacks\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Stacks - IB Computer Science\" \/>\n<meta property=\"og:description\" content=\"Introduction to Stacks A stack is a data structure that follows the Last-In-First-Out (LIFO) principle. It&#8217;s essentially a linear collection of elements with two primary operations: push and pop. Stacks are used to manage data in a way that ensures that the most recently added element is the first one to be removed. Basic Operations:&hellip;&nbsp;Read More &raquo;Stacks\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/ibcs\/stacks\/\" \/>\n<meta property=\"og:site_name\" content=\"IB Computer Science\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-04T15:11:14+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated 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\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/stacks\/\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/stacks\/\",\"name\":\"Stacks - IB Computer Science\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#website\"},\"datePublished\":\"2023-09-03T13:11:17+00:00\",\"dateModified\":\"2023-09-04T15:11:14+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/stacks\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/ibcs\/stacks\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/stacks\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"IB Computer Science\",\"item\":\"https:\/\/learnlearn.uk\/ibcs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Stacks\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#website\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/\",\"name\":\"IB Computer Science\",\"description\":\"- learnlearn..uk\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/ibcs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#organization\",\"name\":\"IB Computer Science\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png\",\"width\":300,\"height\":41,\"caption\":\"IB Computer Science\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Stacks - IB Computer Science","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:\/\/learnlearn.uk\/ibcs\/stacks\/","og_locale":"en_GB","og_type":"article","og_title":"Stacks - IB Computer Science","og_description":"Introduction to Stacks A stack is a data structure that follows the Last-In-First-Out (LIFO) principle. It&#8217;s essentially a linear collection of elements with two primary operations: push and pop. Stacks are used to manage data in a way that ensures that the most recently added element is the first one to be removed. Basic Operations:&hellip;&nbsp;Read More &raquo;Stacks","og_url":"https:\/\/learnlearn.uk\/ibcs\/stacks\/","og_site_name":"IB Computer Science","article_modified_time":"2023-09-04T15:11:14+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/ibcs\/stacks\/","url":"https:\/\/learnlearn.uk\/ibcs\/stacks\/","name":"Stacks - IB Computer Science","isPartOf":{"@id":"https:\/\/learnlearn.uk\/ibcs\/#website"},"datePublished":"2023-09-03T13:11:17+00:00","dateModified":"2023-09-04T15:11:14+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/ibcs\/stacks\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/ibcs\/stacks\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/ibcs\/stacks\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"IB Computer Science","item":"https:\/\/learnlearn.uk\/ibcs\/"},{"@type":"ListItem","position":2,"name":"Stacks"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/ibcs\/#website","url":"https:\/\/learnlearn.uk\/ibcs\/","name":"IB Computer Science","description":"- learnlearn..uk","publisher":{"@id":"https:\/\/learnlearn.uk\/ibcs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/ibcs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/ibcs\/#organization","name":"IB Computer Science","url":"https:\/\/learnlearn.uk\/ibcs\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png","contentUrl":"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png","width":300,"height":41,"caption":"IB Computer Science"},"image":{"@id":"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/ibcs\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"Introduction to Stacks A stack is a data structure that follows the Last-In-First-Out (LIFO) principle. It&#8217;s essentially a linear collection of elements with two primary operations: push and pop. Stacks are used to manage data in a way that ensures that the most recently added element is the first one to be removed. Basic Operations:&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages\/737"}],"collection":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/comments?post=737"}],"version-history":[{"count":8,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages\/737\/revisions"}],"predecessor-version":[{"id":754,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages\/737\/revisions\/754"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/media?parent=737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}