/**
A class of stacks whose entries are stored in an array.
@author Frank M. Carrano
@version 3.0
*/
import java.util.Arrays;
public class ArrayStack2<T> implements StackInterface<T>
{
private T[] stack; // array of stack entries
private int topIndex; // index of top entry
private static final int DEFAULT_INITIAL_CAPACITY = 50;
public ArrayStack2()
{
this(DEFAULT_INITIAL_CAPACITY);
} // end default constructor
public ArrayStack2(int initialCapacity)
{
// the cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempStack = (T[])new Object[initialCapacity];
stack = tempStack;
topIndex = -1;
} // end constructor
public void push(T newEntry)
{
ensureCapacity();
topIndex++;
stack[topIndex] = newEntry;
} // end push
private void ensureCapacity()
{ if (topIndex == stack.length - 1) // if array is full, double size of
array
stack = Arrays.copyOf(stack, 2 * stack.length);
} // end ensureCapacity
public T peek()
{
T top = null;
if (!isEmpty())
top = stack[topIndex];
return top;
} // end peek
public T pop()
{
T top = null;
if (!isEmpty()) {
top = stack[topIndex];
stack[topIndex] = null;
topIndex--;
} // end if
if(isTooBig())
reduceArray();
return top;
} // end pop
public boolean isEmpty()
{
return topIndex < 0;
} // end isEmpty
public void clear()
{
for(int i = 0; i <= topIndex; ++i)
stack[i] = null;
topIndex = -1;
}
public void display()
{
if(isEmpty())
System.out.println("The stack is empty");
else {
for(int i=topIndex;i>=0;i--)
System.out.print(stack[i] + " ");
System.out.print("\n");
}
}
public int remove(int n)
{
int counter = 0;
while(!isEmpty()) {
T x = pop();
if(++counter == n)
break;
}
return counter;
}
private boolean isTooBig()
{
return stack.length>=20 && stack.length>2*(topIndex+1);
}
private void reduceArray()
{
stack = Arrays.copyOf(stack, 3 * stack.length / 4);
}
public void printInfo() // FOR TESTING PURPOSES
{
System.out.println(topIndex+1 + " / " + stack.length);
}
} // end ArrayStack