public class StackCustom {
int size;
int arr[];
int top;
public StackCustom(int size) {
this.size = size;
this.arr = new int[size];
this.top = -1;
}
public void push(int pushedElement) {
if (!isFull()) {
top++;
arr[top] = pushedElement;
} else {
System.out.println("Stack is full");
}
}
public int pop() {
if (!isEmpty()) {
int returnTop = top;
top--;
System.out.println("Popped element: " + arr[returnTop]);
return arr[returnTop];
} else {
System.out.println("Stack is empty!");
return -1;
}
}
public int peek() {
if (!this.isEmpty()) {
return arr[top];
}else {
System.out.println("Stack is empty!");
return -1;
}
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (size - 1 == top);
}
public static void main(String[] args) {
StackCustom stackCustom = new StackCustom(10);
stackCustom.pop();
System.out.println("=====================");
stackCustom.push(10);
stackCustom.push(30);
stackCustom.push(50);
stackCustom.push(40);
System.out.println("=====================");
stackCustom.pop();
stackCustom.pop();
stackCustom.pop();
System.out.println("=====================");
}
}