CS计算机代考程序代写 public class ArrayStack implements Stack {

public class ArrayStack implements Stack {

// Instance variables

private E[] elems;
private int top;

// Constructor

public ArrayStack( int capacity ) {
elems = (E[]) new Object[ capacity ];
top = 0;
}

public boolean isEmpty() {
return ( top == 0 );
}

public E peek() {
return elems[ top-1 ];
}

public E pop() {
E saved = elems[ –top ];
elems[ top ] = null; // scrub the memory!
return saved;
}

public void push( E element ) {

if (top == elems.length) { // increase size
E[] newArray;
newArray = (E[]) new Object[ elems.length * 2 ];
for (int i=0; i