Here is an array based stack. I tried to implement this basic stack from memory. I want to make sure it is correct.
import java.util.*;
public class Stack{
private int[] stack;
private int size;
public Stack(){
stack = new int[5];
int size = 0;
}
public Stack(int capacity){
stack = new int[capacity];
int size = 0;
}
public void push(int pushedValue){
if(size == stack.length){
throw new fullStackException();
}
stack[size] = pushedValue;
size++;
}
public int pop(){
if(size == 0){
throw new emptyStackException();
}
int poppedValue = stack[size - 1];
size--;
return poppedValue;
}
public int peek(){
if(size == 0){
throw new emptyStackException();
}
return stack[size - 1];
}
private class emptyStackException extends RuntimeException{
public emptyStackException(){
super();
}
}
private class fullStackException extends RuntimeException{
public fullStackException(){
super();
}
}
}