{"id":7348,"date":"2017-11-06T12:10:50","date_gmt":"2017-11-06T12:10:50","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=7348"},"modified":"2023-01-06T10:48:57","modified_gmt":"2023-01-06T10:48:57","slug":"fixed-division-of-the-array-for-the-stack-implementation","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/fixed-division-of-the-array-for-the-stack-implementation\/","title":{"rendered":"Know How to Implement Stack in Java From Our Free Sample"},"content":{"rendered":"<blockquote><p><i><span style=\"font-weight: 400;\">Are you eager to know how to implement stack in Java? Get acquainted with our sample and you will find it easy to cope with your task. Look through our sample to see how similar tasks are solved. Afterward, you should try to apply it for your task. The sample that you will see below was completed by an expert from AssignmentShark. All of our experts specialize in particular disciplines, such as physics, math, programming, etc.<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">Our service was created for students who are willing to complete all of their assignments but are facing problems with some of them and look for <a href=\"https:\/\/assignmentshark.com\/do-my-java-homework.html\" target=\"_blank\" rel=\"noopener\">do my Java assignment<\/a> help. Our experts are able to handle any task, including issues on how to implement stack in Java. If you need <a href=\"https:\/\/assignmentshark.com\/\" target=\"_blank\" rel=\"noopener\">assignment help<\/a> from AssignmentShark, you just need to fill the order form. Our support team will contact you as soon as possible. The service is available 24\/7 so you can count on your academic problems being solved soon! <\/span><\/i><\/p>\n<p><!--more--><\/p><\/blockquote>\n<h2><strong>Fixed Division of the Array for the Stack Implementation<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">What we need to do is to implement three stacks using the arrays in Java. Like in many other tasks, the solution depends on how we are going to solve the problem. If we need to have a separate space for each stack, we can do it using the fixed division.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We can divide the array into three equal parts and place stacks within a limited space. Please note that we will further describe ranges in the array by using brackets: square brackets <\/span><i><span style=\"font-weight: 400;\">[]<\/span><\/i><span style=\"font-weight: 400;\"> indicate that the limit values are included in the range, and parentheses mean the values are not included.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Stack 1: [0, n \/ 3).<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Stack 2: [n \/ 3, 2n \/ 3).<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Stack 3: [2n \/ 3, n].<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Here is the code of the implementation with detailed comments:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int size = 100;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int[] buf = new int [size * 3];<\/span> <span style=\"font-weight: 400;\">\/\/buffer for three stacks<\/span><\/p>\n<p><span style=\"font-weight: 400;\">int[] ptr = {0,0,0}; \u00a0\u00a0\u00a0\/\/pointers for tracking the top elements<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">void push(int num, int val) throws Exception {<\/span> <span style=\"font-weight: 400;\">\/\/push operation<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\/* check for the free space *\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0if (ptr[num] &gt;= size){<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throw new Exception(&#8220;The space is not enough&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\/* find index of the top element +1<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0and increase the pointer of the stack *\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0int ind = num * size + ptr[num] + 1; \/\/index of the element<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0ptr[num]++;<\/span> <span style=\"font-weight: 400;\">\/\/increase the size of the stack<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0buf[ind] = val; \/\/write the value to the stack<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">int pop(int num) throws Exception {<\/span> <span style=\"font-weight: 400;\">\/\/pop operation<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0if (ptr[num] == 0) {\/\/check if the stack is empty<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0throw new Exception(&#8220;The stack is empty&#8221;);<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0int ind = num * size + ptr[num];\/\/index of the element<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0ptr[num]&#8211;;<\/span> <span style=\"font-weight: 400;\">\/\/decrease the size of the stack<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0int val = buf[ind]; \/\/temporary value<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0buf[ind] = 0;<\/span> <span style=\"font-weight: 400;\">\/\/delete the value of the stack element<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0return val;<\/span> <span style=\"font-weight: 400;\">\/\/return the tmp value<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">int peek(int num) { \/\/peek operation<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0int ind = num * size + ptr[num]; \/\/index of the element<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0return buf[ind]; \/\/return the element<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">boolean isEmpty(int num) {<\/span> <span style=\"font-weight: 400;\">\/\/check if the stack is empty<\/span><\/p>\n<p><span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0return ptr[num] ==0;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">}<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If we will have more information about the appointment of the stack, we can modify the algorithm. For example, if we know that stack 1 will be larger than stack 2, we can reallocate the space in favor of the second stack.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Thanks for your attention!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you eager to know how to implement stack in Java? Get acquainted with our sample and you will find it easy to cope with your task. Look through our sample to see how similar tasks are solved. Afterward, you should try to apply it for your task. The sample that you will see below [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-7348","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/7348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=7348"}],"version-history":[{"count":9,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/7348\/revisions"}],"predecessor-version":[{"id":13377,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/7348\/revisions\/13377"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=7348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=7348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=7348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}