Help with this stack application
Hi!
I have to modify the following code that constructs a stack so it can duplicate its length once it is full. I have also to modify the pop method so it can reduce the stack length by half if the quantity of elements is less than half of the length. The 'new' code has to keep the old elements. So far I changed the push method and it works (duplicates the stack length when is full) but I have problems with the pop. I would appreciate any help.
Thanks in advance
//Original code with the new push
public class SimpleStack{
private Object[] stack;
private int top;
public SimpleStack(int capacity){//método constructor
stack=new Object[capacity];
top=0;
}//end SimpleStack
public boolean isEmpty(){
return top==0;
}//end isEmpty
public boolean isFull(){
return top==stack.length;
}//end isFull
public void push(Object item){
if (isFull()){
System.out.println ("Stack overflow");
stack= new Object [2*stack.length];
}else{
stack[top++]=item;
}
}//termina método push
public Object pop(){
if(isEmpty()){
System.out.println("Stack underflow");
} else{
return stack[--top];
}//end pop
}//end class SimpleStack
// This is my pop method modification
public Object pop(){
if(isEmpty()){
System.out.println("Stack underflow");
}else{
if(top<(stack.length)/2){
stack=new Object[stack.length/2];
} else{
return stack[--top];
}}//end both else
}//end pop
}//end class SimpleStack