{"id":26740,"date":"2017-12-22T20:32:52","date_gmt":"2017-12-22T15:02:52","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26740"},"modified":"2017-12-22T20:32:52","modified_gmt":"2017-12-22T15:02:52","slug":"java-algorithm-introduction-stack","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-introduction-stack\/","title":{"rendered":"Java Algorithm &#8211; Introduction to Stack"},"content":{"rendered":"<p>Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).<span id=\"more-2713\"><\/span><\/p>\n<p>Mainly the following three basic operations are performed in the stack:<\/p>\n<ul>\n<li><strong>Push: <\/strong>Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.<\/li>\n<li><strong>Pop:<\/strong> Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.<\/li>\n<li>Peek or Top: Returns top element of stack.<\/li>\n<li><strong>isEmpty: <\/strong>Returns true if stack is empty, else fals.<\/li>\n<li><strong>How to understand a stack practically?<\/strong><br \/>\nThere are many real life examples of stack. Consider the simple example of plates stacked over one another in canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it can be simply seen to follow LIFO\/FILO order.<strong>Time Complexities of operations on stack:<\/strong>push(), pop(), esEmpty() and peek() all take O(1) time. We do not run any loop in any of these operations.<strong>Applications of stack:<\/strong><\/p>\n<ul>\n<li>Balancing of symbols<\/li>\n<li>Infix to Postfix \/Prefix conversion<\/li>\n<li>Redo-undo features at many places like editors, photoshop.<\/li>\n<li>Forward and backward feature in web browsers<\/li>\n<li>Used in many algorithms like Tower of Hanoi, tree traversals, stock span problem, histogram problem.<\/li>\n<li>Other applications can be Backtracking, Knight tour problem, rat in a maze, N queen problem and sudoku solver<\/li>\n<\/ul>\n<p><strong>Implementation:<\/strong><br \/>\nThere are two ways to implement a stack:<\/p>\n<ul>\n<li>Using array<\/li>\n<li>Using linked list<\/li>\n<\/ul>\n<p><strong>Implementing Stack using Arrays<\/strong><\/li>\n<\/ul>\n<p><strong>Java Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/* Java program to implement basic stack<br\/>   operations *\/<br\/>class Stack<br\/>{<br\/>    static final int MAX = 1000;<br\/>    int top;<br\/>    int a[] = new int[MAX]; \/\/ Maximum size of Stack<br\/> <br\/>    boolean isEmpty()<br\/>    {<br\/>        return (top &lt; 0);<br\/>    }<br\/>    Stack()<br\/>    {<br\/>        top = -1;<br\/>    }<br\/> <br\/>    boolean push(int x)<br\/>    {<br\/>        if (top &gt;= MAX)<br\/>        {<br\/>            System.out.println(&quot;Stack Overflow&quot;);<br\/>            return false;<br\/>        }<br\/>        else<br\/>        {<br\/>            a[++top] = x;<br\/>            return true;<br\/>        }<br\/>    }<br\/> <br\/>    int pop()<br\/>    {<br\/>        if (top &lt; 0)<br\/>        {<br\/>            System.out.println(&quot;Stack Underflow&quot;);<br\/>            return 0;<br\/>        }<br\/>        else<br\/>        {<br\/>            int x = a[top--];<br\/>            return x;<br\/>        }<br\/>    }<br\/>}<br\/> <br\/>\/\/ Driver code<br\/>class Main<br\/>{<br\/>    public static void main(String args[])<br\/>    {<br\/>        Stack s = new Stack();<br\/>        s.push(10);<br\/>        s.push(20);<br\/>        s.push(30);<br\/>        System.out.println(s.pop() + &quot; Popped from stack&quot;);<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Pros:<\/strong> Easy to implement. Memory is saved as pointers are not involved.<br \/>\n<strong>Cons:<\/strong> It is not dynamic. It doesn\u2019t grow and shrink depending on needs at runtime.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>10 pushed to stack\r\n20 pushed to stack\r\n30 pushed to stack\r\n30 popped from stack\r\nTop item is 20<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Java Algorithm -Introduction to Stack &#8211; Data Structure -Stack is a linear data structure which follows a particular order in which the operations<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,2139,79607],"tags":[79774,79769,79771,79770,79765,79767,79777,79768,79775,79773,79778,79779,79772,79764,79766,79776],"class_list":["post-26740","post","type-post","status-publish","format-standard","hentry","category-coding","category-java","category-stack","tag-implement-queue-in-java","tag-java-8-stack","tag-java-deque","tag-java-queue-example","tag-java-stack-implementation","tag-queue-in-java","tag-queue-program-in-java","tag-stack-and-queue-in-data-structure","tag-stack-class-in-java","tag-stack-implementation-in-java-using-array","tag-stack-implementation-in-java-using-arraylist","tag-stack-implementation-in-java-using-collections","tag-stack-implementation-in-java-using-linked-list","tag-stack-in-java-example","tag-stack-program-in-java","tag-stack-program-in-java-source-code"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26740"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26740\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}