Know How to Implement Stack in Java From Our Free Sample

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.

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 do my Java assignment help. Our experts are able to handle any task, including issues on how to implement stack in Java. If you need assignment help 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!

Fixed Division of the Array for the Stack Implementation

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.

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 [] indicate that the limit values are included in the range, and parentheses mean the values are not included.

  • Stack 1: [0, n / 3).
  • Stack 2: [n / 3, 2n / 3).
  • Stack 3: [2n / 3, n].

Here is the code of the implementation with detailed comments:

int size = 100;

int[] buf = new int [size * 3]; //buffer for three stacks

int[] ptr = {0,0,0};    //pointers for tracking the top elements

 

void push(int num, int val) throws Exception { //push operation

   /* check for the free space */

   if (ptr[num] >= size){

       throw new Exception(“The space is not enough”);

   }

   /* find index of the top element +1

   and increase the pointer of the stack */

   int ind = num * size + ptr[num] + 1; //index of the element

   ptr[num]++; //increase the size of the stack

   buf[ind] = val; //write the value to the stack

}

 

int pop(int num) throws Exception { //pop operation

   if (ptr[num] == 0) {//check if the stack is empty

       throw new Exception(“The stack is empty”);

   }

   int ind = num * size + ptr[num];//index of the element

   ptr[num]–; //decrease the size of the stack

   int val = buf[ind]; //temporary value

   buf[ind] = 0; //delete the value of the stack element

   return val; //return the tmp value

}

 

int peek(int num) { //peek operation

   int ind = num * size + ptr[num]; //index of the element

   return buf[ind]; //return the element

}

 

boolean isEmpty(int num) { //check if the stack is empty

   return ptr[num] ==0;

}

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.

 

Thanks for your attention!

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.