import java.util.EmptyStackException;
public class Stack {
private int top = -1;
private int capacity = 10;
private int[] array;
private int size;
public Stack() {
array = new int[capacity];
}
boolean isEmpty() {
return size == 0;
}
int size() {
return size;
}
void push(int data) {
if (array.length == size) {
ensureCapacity(size * 2);
}
array[++top] = data;
size++;
}
int pop() {
if (top == -1) {
throw new EmptyStackException();
}
size--;
return array[top--];
}
int peek() {
if (top <= -1) {
throw new EmptyStackException();
}
return array[top];
}
public void ensureCapacity(int capacity) {
int[] newArr = new int[capacity];
System.arraycopy(array, 0, newArr, 0, array.length);
this.array = newArr;
}
public static void main(String[] args) {
Stack s=new Stack();
s.push(1);
s.push(2);
s.push(3);
System.out.println("size->"+s.size());
System.out.println("s.pop->"+s.pop());
System.out.println("s.pop->"+s.pop());
s.push(4);
System.out.println("s.pop->"+s.pop());
System.out.println("s.isEmpty->"+s.isEmpty());
System.out.println("s.peek->"+s.peek());
System.out.println("s.pop->"+s.pop());
System.out.println("s.isEmpty->"+s.isEmpty());
}
}