Stack2540Array Â
import java .io .*;
import java . util .*;
public class Stack2540Array {
int CAPACITY = 128;
int top ;
String [] stack ;
public Stack2540Array () {
stack = new String [ CAPACITY ];
top = -1;
}
1
public int size () { return top + 1; }
public boolean isEmpty () { return (top == -1); }
public String top () {
if ( top == -1)
return null ;
return stack [ top ];
}
public void push ( String element ) {
top ++;
stack [top ] = element ;
}
3.2 Dynamic Array
To save computer memory, we need to use dynamic array. In the rstversion of our stack implementation, the instance variable hasa
xed capacity, which is 128. There will be an error when the stackis bigger than 128. For bigger data, you increased the array size.But
that very long array could be wasted for smaller data.
in java please.