{"id":148,"date":"2020-03-16T06:59:00","date_gmt":"2020-03-16T06:59:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=148"},"modified":"2024-02-02T17:27:23","modified_gmt":"2024-02-02T22:27:23","slug":"java-stack","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/java-stack\/","title":{"rendered":"Java Stack"},"content":{"rendered":"\n<p>Stack is a LIFO ADT (Last in first out abstract data type) and a linear data structure. In a jar the items on top (i.e. items that went in last) come our first. In a LIFO ADT, items that go in last come our first.<\/p>\n\n\n\n<p>Stacks are a very useful data structure. Although you have the possibility of creating your own stack data structure from Java code, it is a better idea to simply use the stack provided by Java.<\/p>\n\n\n\n<p><strong>Code<\/strong>&nbsp;Following code shows how to use Java&#8217;s stack:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\npublic class UseStack{\n  public static void main(String&#91;] args) {\n    Stack st = new Stack();\n    st.push(new Integer(100));\n    st.push(\"kcats\");\n    System.out.println(\"Stack contents: \" + st);\n    System.out.println(st.pop());\n    st.pop();\n    if (st.empty()) {\n      st.push(\"stack\");\n    }\n    System.out.println(\"Stack size: \" + st.size());\n    System.out.println(st.peek());\n    System.out.println(\"Stack size: \" + st.size());\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Stack contents: &#91;100, kcats]\nkcats\nStack size: 1\nstack\nStack size: 1\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>&nbsp;Stack is created by instantiating the Stack class. It accepts any object. Here I have added Integer and String. When you call System.out.print() on the stack object, its contents are printed in string format.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>push() adds an item<\/li><li>pop() removes the top item<\/li><li>peek() simply shows the first item without removing it<\/li><li>empty() returns true if the stack is empty<\/li><li>size() returns the size of the stack i.e. how many items does it contain<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Stack is a LIFO ADT (Last in first out abstract data type) and a linear data structure. In a jar the items on top (i.e. items that went in last) come our first. In a LIFO ADT, items that go in last come our first. Stacks are a very useful data structure. Although you have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[38],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":149,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/148\/revisions\/149"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}