Code: Select all
package edy.bsu.cs121.stack;
import javax.swing.JOptionPane;
public class myArrayStack {
private final int DEFAULT_CAPACITY = 100;
private int top;
private T[] stack;
@SuppressWarnings("unchecked")
public myArrayStack(){
top = 0;
stack = (T[])(new Object[DEFAULT_CAPACITY]);
}
@SuppressWarnings("unchecked")
public myArrayStack(int initialCapacity){
top = 0;
stack = (T[])(new Object[initialCapacity]);
}
public void myPush(String first){
stack[top] = (T) first;
top++;
}
public T myPop(){
if (isEmpty()){
JOptionPane.showMessageDialog(null, "Stack is empty!");
} else {
top--;
}
T result = stack[top];
stack[top] = null;
return result;
}
public T myPeek(){
if (isEmpty()){
JOptionPane.showMessageDialog(null, "Stack is empty!");
}
return stack[top-1];
}
public boolean isEmpty(){
if (top == 0){
return true;
}
return false;
}
public int mySize(){
return top;
}
//Searches for a name
public int mySearch (T element) {
{
int spot = -1;
for(int i = 0; i< top; i ++){
if(element.equals(stack[i])){
spot = i;
}
}
return spot;
}
}
public String toString(){
int i=top;
do {
T result = stack[top-i];
i--;
return (String) result;
} while (i>=0);
}
}