/home/caleb/ASDV-Java/Semester 3/Assignments/lab6_generics_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab6_generics_calebfontenot/GenericStack.java |
package edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
import java.util.ArrayList;
import java.util.EmptyStackException;
public class GenericStack<T> {
private T[] elements;
ArrayList<T> elementsList = new ArrayList<T>();
private int top;
static int size = 4;
public GenericStack(int size) {
elements = (T[]) new Object[size];
}
public GenericStack()
{
elements = (T[]) new Object[size];
}
public boolean push (T element) {
if (top == size) {
throw new StackOverflowError();
}
elements[top++] = element;
elementsList.add(element);
return true;
}
public T pop() {
if (top == 0) {
throw new EmptyStackException();
}
--top;
elementsList.remove(top);
return elements[top];
}
public T peek() {
if (top == 0) {
throw new EmptyStackException();
}
return elements[top - 1];
}
public boolean isEmpty() {
boolean returnBoolean = false;
if (top == 0) {
return true;
}
return returnBoolean;
}
@Override
public String toString()
{
String returnString = "";
for (int i = top -1; i >= 0; --i) {
returnString += elements[i].toString() + ", ";
}
return returnString;
}
}