<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shubham Chaudhary on Medium]]></title>
        <description><![CDATA[Stories by Shubham Chaudhary on Medium]]></description>
        <link>https://medium.com/@chaudhary__jr?source=rss-a15253b73850------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gLUQsE13vpZuOvQwmLnyVA.jpeg</url>
            <title>Stories by Shubham Chaudhary on Medium</title>
            <link>https://medium.com/@chaudhary__jr?source=rss-a15253b73850------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 16:19:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chaudhary__jr/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Stack : Implementation in Java]]></title>
            <link>https://medium.com/@chaudhary__jr/stacks-implementaion-in-java-c24430bdde57?source=rss-a15253b73850------2</link>
            <guid isPermaLink="false">https://medium.com/p/c24430bdde57</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[stack]]></category>
            <dc:creator><![CDATA[Shubham Chaudhary]]></dc:creator>
            <pubDate>Tue, 26 Dec 2017 07:05:24 GMT</pubDate>
            <atom:updated>2018-01-02T11:56:59.365Z</atom:updated>
            <content:encoded><![CDATA[<p>A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. A stack is a limited access data structure — elements can be added and removed from the stack only at the top. A helpful analogy is to think of a stack of towels or books; you can remove only the top book, also you can add a new book on the top.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fw0Rp2sClii5OqFc0xSSzA.jpeg" /><figcaption>And this is why it’s a limited access Data Structure — Try removing from else where!</figcaption></figure><h3><strong>Stack API</strong></h3><p>Let us have a look at the methods we need to provide for a stack of strings.</p><ol><li><strong>StackOfStrings()</strong> — create empty stack</li><li><strong>void push(String <em>item</em>)</strong> — insert new string onto the stack</li><li><strong>String pop()</strong> — remove and return the string most recently added</li><li><strong>boolean isEmpty() </strong>— Is Stack empty?</li><li><strong>int size()</strong> — number of strings in the Stack</li></ol><h3>Linked List Implementation of Stack</h3><p>Each stack has a Variable of type Node here named top, which holds the refernce to the top of the stack. Stack is made up of nodes and each node has two instance variables</p><ol><li>String item — it stores the content of that node.</li><li>Node next —it has a reference to the node just below it.</li></ol><pre>public class LinkedStackOfStrings<br>{<br>     private Node top = null;<br>     private int size=0;<br>     private class Node<br>     {<br>          String item;<br>          Node next;<br>     }<br>     public boolean isEmpty()<br>     {<br>          return top==null;<br>     }<br>     public void push(String item)<br>     {<br>          Node oldTop=top;<br>          top=new Node();<br>          top.item=item;<br>          top.next=oldTop();<br>          size++;<br>     }<br>     public String pop()<br>     {<br>          String item=top.item;<br>          top=top.next;<br>          return top;<br>          size--;<br>     }<br>}</pre><p><strong>Performance</strong></p><ol><li><em>Time Analysis</em> : Every operation takes constant time in worst case.</li><li><em>Space Analysis</em> : Stack with N Nodes uses ~40N bytes.</li></ol><h3><strong>Array Implementation of Stack</strong></h3><ul><li>Uses array s[] to store n items on stack.</li><li>push(): Add new items at s[N]</li><li>pop(): Remove items from s[N-1]</li></ul><pre>public class FixedCapacityStackOfStrings<br>{<br>     private String []s;<br>     private int size=0;<br>     public FixedCapacityStackOfStrings(int n)<br>     {     <br>          s=new String[n];<br>     }<br>     public boolean isEmpty()<br>     {<br>          return size==0;<br>     }<br>     public void push(String item)<br>     {<br>          s[size++]=item;<br>     }<br>     public String pop()<br>     {<br>          String item=s[--size];<br>          s[size]=null;   // To prevent Loitering<br>          return item;<br>     }<br>}</pre><p><strong>Loitering </strong>: Holding a reference to an object when it is no longer needed.</p><p>Garbage collector can reclaim memory only if no outstanding reference.</p><p><strong>Defect or Problem : </strong>Stack Overflows when size exceeds capacity. So to overcome this problem lets have a look at resizing array implementation of Stack.</p><h3>Resizing Array Implementation of Stack</h3><p>Here if array is full, we create a new array of twice the size and copy items in new array.</p><pre>public class ResizingArrayStackOfStrings<br>{<br>     private String []s;<br>     private int size=0;<br>     public ResizingArrayStackOfStrings()<br>     {     <br>          s=new String[1];<br>     }<br>     public boolean isEmpty()<br>     {<br>          return size==0;<br>     }<br>     public void push(String item)<br>     {    <br>          if(size==s.length)<br>               resize(2*s.length);<br>          s[size++]=item;<br>     }<br>     private void resize(int capacity)<br>     {<br>          String []copy=new String[capacity];<br>          for(int i=0;i&lt;size;i++)<br>               copy[i]=s[i];<br>          s=copy;<br>     }</pre><pre>     public String pop()<br>     {<br>          String item=s[--size];<br>          s[size]=null;   // To prevent Loitering<br>          if((size&gt;0)&amp;&amp;(size==s.length/4))<br>               resize(s.length/2);<br>          return item;<br>     }<br>}</pre><p><strong>Analysis : LinkedList Implementation Vs ResizingArray Implementation</strong></p><p>Worst Case in Resizing Array Implementation is worst then the Linked List but overall faster. This is beacause Linked List Implementation uses extra space and time to deal with links. Every operation takes constant time in worst case in Linked List Implementation.</p><p>To understand the resizing array analysis better check out the given link <a href="https://www.geeksforgeeks.org/analysis-algorithm-set-5-amortized-analysis-introduction/">Resizing Array Analysis</a></p><p>Clap if you enjoyed studying Stacks. Next up Queues!</p><p>Thank you!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c24430bdde57" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>