Generic Stack using Arraylist
Mon May 27 2024 17:28:17 GMT+0000 (Coordinated Universal Time)
Saved by
@Asadullah69
class GenericStackArray<T> {
private T[] stackArray;
private int top;
private int maxSize;
@SuppressWarnings("unchecked")
public GenericStackArray(int size) {
this.maxSize = size;
this.stackArray = (T[]) new Object[maxSize];
this.top = -1;
}
public void push(T item) {
if (top < maxSize - 1) {
stackArray[++top] = item;
} else {
System.out.println("Stack Overflow");
}
}
public T pop() {
if (top >= 0) {
return stackArray[top--];
} else {
System.out.println("Stack Underflow");
return null;
}
}
public boolean isEmpty() {
return top == -1;
}
}
content_copyCOPY
Comments