{"id":272,"date":"2017-06-14T12:09:00","date_gmt":"2017-06-14T10:09:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/2017\/06\/14\/stacks\/"},"modified":"2020-08-22T20:42:45","modified_gmt":"2020-08-22T18:42:45","slug":"stacks","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/stacks\/","title":{"rendered":"Stacks"},"content":{"rendered":"<div>Stacks are very important in the study of Data Structures. But in this article we would examine the basics of Stack Data Structure and how stack is represented.<\/div>\n<div><\/div>\n<div><\/div>\n<div>Below is what is to be presented in this article.<\/div>\n<ol>\n<li>Definition of Stack<\/li>\n<li>Stack Representation<\/li>\n<li>Application of Stacks<\/li>\n<li>Conversion of Decimal to Binary<\/li>\n<li>Expression Syntax Parsing<\/li>\n<\/ol>\n<div><\/div>\n<div><\/div>\n<div><b>Definition<\/b><b>\u00a0of Stack<\/b><\/div>\n<div>A stack is a linear data structure that allows for insertion and deletion of items from one end of the stack called the top of the stack. This means that the last item to be inserted into to stack is always the first item to be retrieved from the stack and vice versa. So we can can say that stack supports Last In First Out(LIFO), data storage and retrieval. Stacks are normally used in the computer system for temporary data storage.<\/div>\n<div><\/div>\n<div><\/div>\n<div><a href=\"https:\/\/4.bp.blogspot.com\/-uYpf2IUBMPk\/WUb-llAmWLI\/AAAAAAAAAGk\/p6g5ZRcnNPUKmt12x4ad5g5tMBdVwBIsACLcBGAs\/s1600\/Stack.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter\" src=\"https:\/\/4.bp.blogspot.com\/-uYpf2IUBMPk\/WUb-llAmWLI\/AAAAAAAAAGk\/p6g5ZRcnNPUKmt12x4ad5g5tMBdVwBIsACLcBGAs\/s320\/Stack.jpg\" width=\"320\" height=\"283\" border=\"0\" data-original-height=\"462\" data-original-width=\"521\" \/><\/a><\/div>\n<div><\/div>\n<div><\/div>\n<div>Take note of the following terms:<\/div>\n<div><b>Push<\/b>: The action of inserting an item on top of the stack<\/div>\n<div><b>Pop<\/b>: The action of retrieving an item from the top of the stack<\/div>\n<div><b>Initialize<\/b>: Start a new stack<\/div>\n<div><\/div>\n<div><\/div>\n<div><b>Linked-List Stack Representation<\/b><\/div>\n<div>The most common representation of stack is by the use of linked list. Before we examine the Linked-List representation, below are operations that can be performed on a stack. These operation are actually methods that can generally be performed on an object of type Stack.<\/div>\n<div><\/div>\n<div><strong>push(new_item:item_type)<\/strong><\/div>\n<div>Adds a new item on top of the stack.<\/div>\n<div><\/div>\n<div><b>top() :<\/b>\u00a0item_type<\/div>\n<div>Returns that last item inserted into the stack<\/div>\n<div><\/div>\n<div><b>pop()<\/b><\/div>\n<div>Retrieves that last item from top of the stack<\/div>\n<div><\/div>\n<div><b>is-empty()<\/b>\u00a0: Boolean<\/div>\n<div>Returns true if the Stack is empty else returns false<\/div>\n<div><\/div>\n<div><b>is-full():<\/b>\u00a0Boolean<\/div>\n<div>Returns True if the stack is full else returns false<\/div>\n<div><\/div>\n<div><b>get-size()<\/b>\u00a0: Integer<\/div>\n<div>Returns an integer representing the size of the stack.<\/div>\n<div><\/div>\n<div><\/div>\n<div><b>Application of Stacks<\/b><\/div>\n<div>Stacks can be applied in many areas of life including: Stack of trays in a restaurant, stack of books, stack of plates etc. But we are going to closely examine the following:<\/div>\n<div><\/div>\n<div><b>1. Converting Decimal to Binary<\/b><\/div>\n<div>Consider the conversion of a decimal number into its binary equivalent. The algorithm goes as follows:<\/div>\n<div><\/div>\n<div>Read a decimal number<\/div>\n<div>While the number is greater than zero, continue<\/div>\n<div>\u00a0 \u00a0 Divide the number by two<\/div>\n<div>\u00a0 \u00a0 Find the remainder<\/div>\n<div>\u00a0\u00a0 \u00a0Write the remainder<\/div>\n<div>End<\/div>\n<div><\/div>\n<div><i><b>Example<\/b><\/i><br \/>\n<i>Implement the algorithm, to covert the decimal number 25 to binary<\/i><\/div>\n<div>If the algorithm above it applied, the program would print out the binary number 10011 and this would be the wrong answer.<\/div>\n<div><\/div>\n<div>To get the right answer, we need to implement this algorithm using a stack data structure. Each of the binary digits is pushed on to a attack as they are generated. At the end of the whole conversion, each digit is retrieved from the stack and printed.<\/div>\n<div>The correct result would be as shown in the figure below.<\/div>\n<div><\/div>\n<div>\n<div><a href=\"https:\/\/1.bp.blogspot.com\/-uHKPw28ygz0\/WUb-liqNW9I\/AAAAAAAAAGo\/U55FepOUoFI-s2vKCHwhEKDh3L5YJoCwQCLcBGAs\/s1600\/Convertion_Decimal_Binary.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter\" src=\"https:\/\/1.bp.blogspot.com\/-uHKPw28ygz0\/WUb-liqNW9I\/AAAAAAAAAGo\/U55FepOUoFI-s2vKCHwhEKDh3L5YJoCwQCLcBGAs\/s320\/Convertion_Decimal_Binary.jpg\" width=\"320\" height=\"263\" border=\"0\" data-original-height=\"402\" data-original-width=\"489\" \/><\/a><\/div>\n<\/div>\n<div><\/div>\n<div>Other applications of stack include, Expression syntax evaluation, Rearranging Railroad cars and the Quicksort Algorithm.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Stacks are very important in the study of Data Structures. But in this article we would examine the basics of Stack Data Structure and how &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[35],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/272"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":713,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/272\/revisions\/713"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}