Голосование за лучший ответ
Новая_Личность
Знаток
(492)
7 лет назад
package jss2;
import jss2.exceptions.*;
public class ArrayStack implements StackADT
{
private final int DEFAULT_CAPACITY = 100;
private int top;
private T[] stack;
public ArrayStack(){
top = 0;
stack = (T[])(new Object[DEFAULT_CAPACITY]);
}
public ArrayStack (int initialCapacity){
top = 0;
stack = (T[])(new Object[initialCapacity]);
}
public void push (T element){
if (size() == stack.length)
expandCapacity();
stack[top] = element;
top++;
}
public T pop() throws EmptyCollectionException{
if (isEmpty())
throw new EmptyCollectionException("Stack");
top--;
T result = stack[top];
stack[top] = null;
return result;
}
public T peek() throws EmptyCollectionException{
if (isEmpty())
throw new EmptyCollectionException("Stack");
return stack[top-1];
}
public boolean isEmpty(){
return (top == 0);
}
public int size(){
return top;
}
public String toString(){
String result = "";
for (int scan=0; scan < top; scan++)
result = result + stack[scan].toString() + "\n";
return result;
}
private void expandCapacity(){
T[] larger = (T[])(new Object[stack.length*2]);
for (int index=0; index < stack.length; index++)
larger[index] = stack[index];
stack = larger;
}
}